(f)
return inner
A decorator for request handlers that returns structured error responses

    Inspired by (and some parts shamelessly copied from)
    https://github.com/jsocol/django-jsonview
A decorator for request handlers that returns structured error responses
A decorator for request handlers that returns structured error responses
def json_error(f):
    """A decorator for request handlers that returns structured error responses

    Inspired by (and some parts shamelessly copied from)
    https://github.com/jsocol/django-jsonview
    """
    @wraps(f)
    def inner(request, *args, **kwargs):
        from . import as_text
        try:
            response = f(request, *args, **kwargs)

            # Some errors are not exceptions. :\
            if isinstance(response, http.HttpResponseNotAllowed):
                blob = json.dumps({
                    'error': 405,
                    'message': 'HTTP method not allowed.'
                })
                return http.HttpResponse(blob, status=405, content_type=JSON)

            return response

        except http.Http404 as e:
            return _get_json_exception_response(404, request, e, log_message='Not found: %s')
        except PermissionDenied as e:
            return _get_json_exception_response(403, request, e, log_message='Forbidden (Permission denied): %s')
        except BadRequest as e:
            return _get_json_exception_response(400, request, e)
        except Exception as e:
            message = f'JSON exception response: {as_text(e)}'
            notify_exception(request, message)
            return _get_json_exception_response(500, request, e)
    return inner
def json_error ( f ) : @ wraps ( f ) def inner ( request , * args , * * kwargs ) : from . import as_text try : response = f ( request , * args , * * kwargs ) # Some errors are not exceptions. :\ if isinstance ( response , http . HttpResponseNotAllowed ) : blob = json . dumps ( { 'error' : 405 , 'message' : 'HTTP method not allowed.' } ) return http . HttpResponse ( blob , status = 405 , content_type = JSON ) return response except http . Http404 as e : return _get_json_exception_response ( 404 , request , e , log_message = 'Not found: %s' ) except PermissionDenied as e : return _get_json_exception_response ( 403 , request , e , log_message = 'Forbidden (Permission denied): %s' ) except BadRequest as e : return _get_json_exception_response ( 400 , request , e ) except Exception as e : message = f'JSON exception response: {as_text(e)}' notify_exception ( request , message ) return _get_json_exception_response ( 500 , request , e ) return inner