简体   繁体   中英

How to create custom error pages with app.yaml for Google Appengine Python

For a Google Appengine project written in Python, I want to have my own error messages when an error occurs. I want to use the error_handlers as described in: http://code.google.com/intl/nl/appengine/docs/python/config/appconfig.html#Custom_Error_Responses

In the application I use webapp with debug = False

When I create an error in my code, I get the standard 500 Server error message from the browser.

I have created a custom error page named default_error.html

The question is: Where to save this custom error page?

BTW This is my app.yaml code:

application: customerrorpage
version: 1
runtime: python
api_version: 1


error_handlers:
- file: default_error.html

handlers:
- url: /good.htm
  static_files: static/good.htm
  upload: static/good.htm

- url: /
  static_files: static/good.htm
  upload: static/good.htm

- url: /.*
  script: main.py

Defining a custom 404 with just the python part works for me:

app.error_handlers[404] = handle_404

def handle_404(request, response, exception):
    c = {'exception': exception.status}
    t = jinja2.get_jinja2(app=app).render_template('404.html', **c)
    response.write(t)
    response.set_status(exception.status_int)

This gives you more control over error messages and other dynamics that you might want to display.

One way to approach this is to define a 'BaseRequestHandler' class, from which your request handlers are subclassed. The BaseRequestHandler class can override the handle_exception() method (see http://code.google.com/appengine/docs/python/tools/webapp/requesthandlerclass.html#RequestHandler_handle_exception ) to render an error page.

Partly found how it works. Thank you all for your answers.

The location of the HTM file could be in the core or in a seperate directory. If that's the case use for example the following code in your app.yaml file:

error_handlers:
- file: error_handlers/default_error.html

The to be shown html file should now be placed in the directory error_handlers

When you import an nonexisting module in main.py the above page is shown.

To fetch a 500 error I used webapp2 to show a custom error page. This page is always shown when an error occurs. Even when there is an over_quota error and app.yaml is correctly configured. I also removed webapp2 and used webapp again. Now no page is displayed at all.

I have another app which is currently over_quote. How to get the over_quota html page displayed? Is it a bug?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM