Google App Engine and 404 error

Posted on

Question :

Google App Engine and 404 error

I’ve setup a static website on GAE using hints found elsewhere, but can’t figure out how to return a 404 error. My app.yaml file looks like

- url: (.*)/
  static_files: static1/index.html
  upload: static/index.html

- url: /
  static_dir: static

with all the static html/jpg files stored under the static directory. The above works for files that exist, but returns a null length file if they don’t. The answer is probably to write a python script to return a 404 error, but how do you set things up to serve the static files that exist but run the script for files that don’t?

Here is the log from fetching a non-existent file (nosuch.html) on the development application server:

ERROR    2008-11-25 20:08:34,084] Error encountered reading file "/usr/home/ctuffli/www/tufflinet/static/nosuch.html":
[Errno 2] No such file or directory: '/usr/home/ctuffli/www/tufflinet/static/nosuch.html'
INFO     2008-11-25 20:08:34,088] "GET /nosuch.html HTTP/1.1" 404 -
Asked By: ctuffli


Answer #1:

You need to register a catch-all script handler. Append this at the end of your app.yaml:

- url: /.*

In you will need to put this code:

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class NotFoundPageHandler(webapp.RequestHandler):
    def get(self):
        self.response.out.write('<Your 404 error html page>')

application = webapp.WSGIApplication([('/.*', NotFoundPageHandler)],

def main():

if __name__ == "__main__":

Replace <Your 404 error html page> with something meaningful. Or better use a template, you can read how to do that here.

Please let me know if you have problems setting this up.

Answer #2:

google app engine now has Custom Error Responses

so you can now add an error_handlers section to your app.yaml, as in this example:


- file: default_error.html

- error_code: over_quota
    file: over_quota.html
Answered By: jonmiddleton

Answer #3:

A significantly simpler way to do this without requiring any CPU cycles is to place this handler at the bottom of your app.yaml

- url: /.*
    static_files: views/404.html
    upload: views/404.html

This then allows you to place a static 404.html file in your views directory. No need for a python handler. Anything that isn’t handled in your app.yaml already will hit that.

Answered By: Zee Spencer

Answer #4:

You can create a function to handle your errors for any of the status codes. You’re case being 404, define a function like this:

def Handle404(request, response, exception):
     response.out.write("Your error message") 

You can pass anything – HTML / plain-text / templates in the response.out.write function. Now, add the following declaration after your app declaration.

app.error_handlers[404] = Handle404

This worked for me.

Answered By: TeknasVaruas

Answer #5:

webapp2 provides the error_handlers dictionary that you can use to serve custom error pages.
Example below:

def handle_404(request, response, exception):
    h = YourAppBaseHandler(request, response)

def handle_500(request, response, exception):
    h = YourAppBaseHandler(request, response)

app = webapp2.WSGIApplication([
    webapp2.Route('/', MainHandler, name='home')
    ], debug=True)
app.error_handlers[404] = handle_404
app.error_handlers[500] = handle_500

More details are available on webapp2‘s documentation pages:

Answered By: Romain

Answer #6:

The dev_appserver is already returning 404 responses for anything that doesn’t match the mapping, or does match the mapping but doesn’t exist. The 404 response itself has no body, but it’s still a 404:

$ wget -O -
--2010-10-28 10:54:51--
Connecting to connected.
HTTP request sent, awaiting response... 404 
2010-10-28 10:54:51 ERROR 404: (no description).

$ wget -O -
--2010-10-28 10:54:54--
Connecting to connected.
HTTP request sent, awaiting response... 404 
2010-10-28 10:54:54 ERROR 404: (no description).

If you want to return a more user-friendly error page, follow jonmiddleton’s advice and specify a custom 404 page.

Answered By: Nick Johnson

Answer #7:

I have reviewed all the above given answers and used the following at the end as the most universal 404 solution:

Add this link at the end of app.yaml

- url: /(.*) 

and create with the following content

import webapp2
from google.appengine.ext.webapp import template

class NotFound(webapp2.RequestHandler):
  def get(self):
    self.response.out.write(template.render('404.html', {}))

app = webapp2.WSGIApplication([
    ('/.*', NotFound)
], debug=True)

This will display contents of 404.html file with 404 error code.

The advantage of this solution is simplicity, correctness of bahaviour and flexibility, as it allows to use a static 404.html file as error page content.

I also want to warn against some of the solutions suggested above.

  • Custom Error Responses don not work with 404 error
  • Also don’t use static files, as they would return 200 instead of 404 error. This can cause a lot of headache ahead.
Answered By: husayt

Answer #8:

I can’t comment on jonmiddleton’s answer, but the custom error responses is for App engine specific errors by the look of it.
I don’t see a way to specify a custom 404 page.

Django let’s you specify one though.

Answered By: zahanm

Leave a Reply

Your email address will not be published. Required fields are marked *