Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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: static\1/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 dev_appserver.py] 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 dev_appserver.py] "GET /nosuch.html HTTP/1.1" 404 - 
like image 637
ctuffli Avatar asked Oct 10 '08 00:10

ctuffli


People also ask

How do I fix Google error 404?

The simplest and easiest way to fix your 404 error code is to redirect the page to another one. You can perform this task using a 301 redirect. What's 301, you may ask? It's a redirect response code that signals a browser that the content has been transferred to another URL.

What is Google App Engine launcher?

The Launcher is an application for creating, running, uploading, and otherwise managing Google App Engine applications. The Launcher itself is written in Python and uses the wxWidgets toolkit. You can run the Launcher on Linux and Mac OS X as well. Mac developers already have a native Launcher included with its SDK.

What is Google App Engine 4?

App Engine is a fully managed, serverless platform for developing and hosting web applications at scale. You can choose from several popular languages, libraries, and frameworks to develop your apps, and then let App Engine take care of provisioning servers and scaling your app instances based on demand.


2 Answers

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

- url: /.*   script: main.py 

In main.py 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.error(404)         self.response.out.write('<Your 404 error html page>')  application = webapp.WSGIApplication([('/.*', NotFoundPageHandler)],                                      debug=True)  def main():     run_wsgi_app(application)  if __name__ == "__main__":     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.

like image 109
Alexander Kojevnikov Avatar answered Oct 04 '22 00:10

Alexander Kojevnikov


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:

error_handlers:  - file: default_error.html  - error_code: over_quota     file: over_quota.html 
like image 22
jonmiddleton Avatar answered Oct 03 '22 22:10

jonmiddleton