I am using the webapp2 environment in the Google App Engine (Python). The webapp2 exception handling: exceptions in a WSGI application describes how to handle 404 errors in a function:
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
How can I handle 404 error in a class webapp2.RequestHandlerin a method of .get()this class?
Edit:
I want to call RequestHandlerto access the session ( request.session). Otherwise, I cannot transfer the current user to the 404 error page template. That is, on the StackOverflow 404 page you can see your username. I would like to show the username of the current user on page 404 of the page of my site. Is this possible in a function or should it be RequestHandler?
@proppy:
class Webapp2HandlerAdapter(webapp2.BaseHandlerAdapter):
def __call__(self, request, response, exception):
request.route_args = {}
request.route_args['exception'] = exception
handler = self.handler(request, response)
return handler.get()
class Handle404(MyBaseHandler):
def get(self):
self.render(filename="404.html",
page_title="404",
exception=self.request.route_args['exception']
)
app = webapp2.WSGIApplication(urls, debug=True, config=config)
app.error_handlers[404] = Webapp2HandlerAdapter(Handle404)