Django No Exceptions provides middleware to catch certain exceptions that correspond to HTTP response classes. It was inspired by Django's Http404, and is used similarly.
- Add
'no_exceptions.middleware.NoExceptionsMiddleware',
to yourMIDDLEWARE_CLASSES
- Optionally set
LET_HTTP_EXCEPTIONS_500
(True
/False
) to determine whether to catch NoExceptions exceptions or to pass them through as normal exceptions.
This setting defaults toDEBUG
(pass exceptions through in DEBUG mode, turn them into appropriate responses in production).
from no_exceptions import exceptions as x
def my_view(request, \*args, \**kwargs):
...
if request.is_bad():
raise x.Http400("You need to foo your bar!")
...
Exceptions available are:
Http400
- CLIENT ERRORHttp401
- UNAUTHORIZEDHttp403
- FORBIDDENDjango's own Http404 is also available unchanged in no_exceptions.exceptions
You may also use no_exceptions.exceptions.HttpException
to make your own exceptions on the fly:
def my_view(request, \*args, \**kwargs):
...
raise x.HttpException(
status=418,
message="I can't help you, I'm a teapot"
)
...