I am using GAE, Google App Engine with Webapp using Python.
I am trying to implement my own error handler, or pattern, or along these lines. GAE provides some documentation here , however it does not provide enough in the example for implementation (for me).
I also covered all of these great examples in another StackOverflow question here - but can't figure out how to implement it in my current main.py.
import os from google.appengine.ext import webapp from google.appengine.ext.webapp import util from google.appengine.ext.webapp import template class MainHandler(webapp.RequestHandler): def get (self, q): if q is None: q = 'static/index.html' path = os.path.join (os.path.dirname (__file__), q) self.response.headers ['Content-Type'] = 'text/html' self.response.out.write (template.render (path, {})) def main (): application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=False) util.run_wsgi_app (application) if __name__ == '__main__': main ()
I tried to make another class and implement it before MainHandler , as well as a new def in MainHandler . The only time I displayed 404 is that it is displayed globally, which means that even the index.html file was "404".
The last thing I tried was to implement something like:
if not os.path.exists (_file_): self.redirect(/static/error/404.html)
My app.yaml file :
application: appname version: 1 runtime: python api_version: 1 error_handlers: - file: static/error/404.html - error_code: over_quota file: static/error/404.html handlers: - url: /(.*\.(gif|png|jpg|ico|js|css)) static_files: \1 upload: (.*\.(gif|png|jpg|ico|js|css)) - url: /robots.txt static_files: robots.txt upload: robots.txt - url: /favicon.ico static_files: favicon.ico upload: favicon.ico - url: .* script: main.py
I cannot find manuals / guides for implementing the 404 handler, just code extraction.
Thank you all very much!
Edit: As explained below, Hans, I want to return 404 when a file (or directory) is missing from the file system.
I tried:
class ErrorHandler(webapp.RequestHandler): def get(self): if not os.path.isfile(_file_): self.redirect('/static/error/404.html')
to no avail. What is the correct implementation / syntax?
Edit: In Voscausa
import os from google.appengine.ext import webapp from google.appengine.ext.webapp import util from google.appengine.ext.webapp import template import logging import webapp2 def handle_404(request, response, exception): logging.exception(exception) response.write('Oops! I could swear this page was here!') response.set_status(404) def handle_500(request, response, exception): logging.exception(exception) response.write('A server error occurred!') response.set_status(500) app = webapp2.WSGIApplication([ webapp2.Route('/', handler='handlers.HomeHandler', name='home') ]) app.error_handlers[404] = handle_404 app.error_handlers[500] = handle_500 class MainHandler(webapp.RequestHandler): def get (self, q): if q is None: q = 'static/index.html' path = os.path.join (os.path.dirname (__file__), q) self.response.headers ['Content-Type'] = 'text/html' self.response.out.write (template.render (path, {})) def main (): application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True) util.run_wsgi_app (application) if __name__ == '__main__': main ()