app: Add global error handling
When a controller raises an error, the application now handles it and ensures that the proper representation is returned to the user-agent.master
parent
2cdc613f3a
commit
65f1a46d17
|
@ -5,8 +5,13 @@ from . import (
|
|||
thumbnails,
|
||||
)
|
||||
import functools
|
||||
import logging
|
||||
import milla.util
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
DEFAULT_CONFIG = {
|
||||
|
@ -53,3 +58,21 @@ class Application(milla.Application):
|
|||
request.want = 'xhtml'
|
||||
request.ResponseClass = functools.partial(base.VariedResponse, request)
|
||||
return request
|
||||
|
||||
def handle_error(self, request):
|
||||
response = request.ResponseClass()
|
||||
e = sys.exc_info()[1]
|
||||
err = {}
|
||||
if isinstance(e, milla.HTTPRedirection):
|
||||
return e
|
||||
elif isinstance(e, milla.WSGIHTTPException):
|
||||
response.status_int = e.code
|
||||
err['code'] = getattr(e, 'errno', e.code)
|
||||
err['message'] = '{}'.format(e)
|
||||
else:
|
||||
log.exception('An unhandled exception occurred:')
|
||||
response.status_int = 500
|
||||
err['code'] = -1
|
||||
err['message'] = 'An unknown error has occurred'
|
||||
response.set_payload('error.html.j2', {'error': err})
|
||||
return response
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
{% extends "base.html.j2" %}
|
||||
{% block body -%}
|
||||
<h2>Error</h2>
|
||||
<div>{{ error.message }}</div>
|
||||
{% endblock body -%}
|
Loading…
Reference in New Issue