Overview
Assembly allows you to handle your errors beautifully.
_error_handler
A special method _error_handler
can be added in your view class to capture any HTTPException.
A template with the name error_handler.html
will be used.
Only one _error_handler
can exist in the application. _error_handler
will take precedence over
the other error handlers.
To use specific error handler, omit _error_handler instead use the _error_$errorCode
for example
_error_404
# views/error.py from assembly import Assembly, HTTPError class Error(Assembly): def _error_handler(self, e): return { "e": e }
Template:
<!-- templates/error/Error/error_handler.html --> {% extends 'layouts/base.html' %} {% block title %}Error {{ e.code }} {% endblock %} {% block body %} <h1>Error: {{ e.code }}</h1> <h4>{{ e.description }}</h4> {% endblock %}
_error_$errorCode
Only one _error_handler
can exist in the application. _error_handler
will take precedence over
the other error handlers.
To use specific error handler, omit _error_handler instead use the _error_$errorCode
for example
_error_404
$errorCode is valid HTTP Error Code. Invalid code will throw an error
A template with the name error_$errorCode.html
will be used.
# views/error.py from assembly import Assembly, HTTPError class Error(Assembly): def _error_404(self, e): return { "e": e } def _error_500(self, e): return { "e": e }
Template:
<!-- templates/error/Error/error_404.html --> {% extends 'layouts/base.html' %} {% block title %}Error {{ e.code }} {% endblock %} {% block body %} <h1>Error: {{ e.code }}</h1> <h4>{{ e.description }}</h4> {% endblock %} <!-- templates/error/Error/error_500.html --> {% extends 'layouts/base.html' %} {% block title %}Error {{ e.code }} {% endblock %} {% block body %} <h1>Error: {{ e.code }}</h1> <h4>{{ e.description }}</h4> {% endblock %}
Error Method Usage
from assembly import Assembly, HTTPError class Index(Assembly): def index(self): raise HTTPError.Unauthorized() def trigger_404(self): raise HTTPError.NotFound()
abort
abort
can also be used to trigger error
from assembly import Assembly, HTTPError class Index(Assembly): def index(self): raise HTTPError.abort(401) def trigger_404(self): raise HTTPError.abort(404)