def wrapped(*args, **kwargs): if flask.request.method in ('POST', 'PATCH'): if flask.request.headers.get( 'Content-Type') != 'application/vnd.api+json': return response.JsonApiErrorResponse( { 'source': '', 'detail': 'Content-Type header must be application/vnd.api+json', 'title': 'InvalidRequestHeader', 'status': 415 }, status=415).make_response() if flask.request.headers.get( 'Accept', 'application/vnd.api+json') != 'application/vnd.api+json': return response.JsonApiErrorResponse( { 'source': '', 'detail': 'Accept header must be application/vnd.api+json', 'title': 'InvalidRequestHeader', 'status': 406 }, status=406).make_response() return func(*args, **kwargs)
def test_jsonapi_error_response(app): with app.test_request_context( '/example/5d285d41-398d-4322-a6d4-d15af6bd930e/'): error_response = response.JsonApiErrorResponse( exceptions.JsonApiException( source= "An object containing references to the source of the error. " "http://jsonapi.org/format/#errors", detail='exception details', title='Something terrible happened.', status=http.HTTPStatus.INTERNAL_SERVER_ERROR).to_dict() ).make_response() assert json.loads(error_response.data.decode()) == { "jsonapi": { "version": "1.0" }, "errors": [{ "detail": "exception details", "title": "Something terrible happened.", "status": 500, "source": "An object containing references to the source of the error. " "http://jsonapi.org/format/#errors" }] }
def dispatch_request(self, *args, **kwargs): self.args = args self.kwargs = kwargs handler = getattr(self, request.method.lower(), self._http_method_not_allowed) try: response_object = handler(request, *args, **kwargs) except exceptions.JsonApiException as e: return response.JsonApiErrorResponse( e.to_dict(), status=e.status).make_response() else: return response_object.make_response()