def test_error_response_dict_details(self): """To test details in the form of dict""" error_response = ErrorResponse(source="test source", detail="test detail") expected_dict = {'status': error_response.status, 'source': error_response.source, 'title': error_response.title, 'detail': error_response.detail} self.assertEqual(error_response.to_dict(), expected_dict)
def test_error_response_base_respond(self): """Method to test base error response methods""" with self.app.test_request_context(): base_error_response = ErrorResponse( source="test source", detail="test detail" ) json_object = json.dumps(jsonapi_errors([base_error_response.to_dict()])) assert base_error_response.respond() != \ make_response( json_object, 200, {'Content-Type': 'application/vnd.api+json'} )
def test_errors(self): """Check ErrorsHelper: errors codes""" with app.test_request_context(): # Base error base_error = ErrorResponse({'source': ''}, 'Internal Server Error.', title='Internal Server Error.', status=500) self.assertEqual(base_error.status, 500) # Forbidden Error forbidden_error = ForbiddenError({'source': ''}, 'Super admin access is required') self.assertEqual(forbidden_error.status, 403) # Not Found Error not_found_error = NotFoundError({'source': ''}, 'Object not found.') self.assertEqual(not_found_error.status, 404) # Not Found Error internal_server_error = ServerError({'source': ''}, 'Internal Server Error.') self.assertEqual(internal_server_error.status, 500)
def handle_exception(error: ErrorResponse): return error.respond()
def internal_server_error(error): if current_app.config['PROPOGATE_ERROR'] is True: exc = ErrorResponse(str(error)) else: exc = ErrorResponse('Unknown error') return exc.respond()