def handle_error(self, e): if e.__class__ in self._error_handlers: handler = self._error_handlers[e.__class__] result = handler(e) e = HTTPException(str(e)) e.data, e.code = result if len(result) == 2 else (result, 500) elif self._default_error_handler: result = self._default_error_handler(e) e = HTTPException(str(e)) e.data, e.code = result if len(result) == 2 else (result, 500) return super(Api, self).handle_error(e)
def handle_error(self, e): if e.__class__ in self._error_handlers: handler = self._error_handlers[e.__class__] result = handler(e) e = HTTPException(str(e)) e.data, e.code = result if len(result) == 2 else (result, 500) return super(Api, self).handle_error(e)
def test_handle_error_401_sends_challege_default_realm(self, api): exception = HTTPException() exception.code = 401 exception.data = {"foo": "bar"} resp = api.handle_error(exception) assert resp.status_code == 401 assert resp.headers["WWW-Authenticate"] == 'Basic realm="flask-restx"'
def test_handle_error_401_sends_challege_default_realm(self, api): exception = HTTPException() exception.code = 401 exception.data = {'foo': 'bar'} resp = api.handle_error(exception) assert resp.status_code == 401 assert resp.headers['WWW-Authenticate'] == 'Basic realm="flask-restplus"'
def test_handle_error_401_sends_challege_default_realm(self): exception = HTTPException() exception.code = 401 exception.data = {'foo': 'bar'} with self.app_401.test_request_context('/foo'): resp = self.api_401.handle_error(exception) self.assertEquals(resp.status_code, 401) self.assertEquals(resp.headers['WWW-Authenticate'], 'Basic realm="flask-restful"')
def test_handle_error_401_sends_challege_default_realm(self): api = restplus.Api(self.app, serve_challenge_on_401=True) exception = HTTPException() exception.code = 401 exception.data = {'foo': 'bar'} with self.app.test_request_context('/foo'): resp = api.handle_error(exception) self.assertEqual(resp.status_code, 401) self.assertEqual(resp.headers['WWW-Authenticate'], 'Basic realm="flask-restful"')
def test_when_flask_restful_error_is_raised(self): expected_status_code = 400 expected_message = ["Input Error - field: missing"] exception = HTTPException() exception.data = {'message': {'field': 'missing'}} exception.code = 400 response = handle_error(exception) self.assertResponse(response, expected_status_code, expected_message)
def test_handle_error_401_sends_challege_default_realm(self): api = restplus.Api(self.app, serve_challenge_on_401=True) exception = HTTPException() exception.code = 401 exception.data = {'foo': 'bar'} with self.app.test_request_context('/foo'): resp = api.handle_error(exception) self.assertEqual(resp.status_code, 401) self.assertEqual(resp.headers['WWW-Authenticate'], 'Basic realm="flask-restful"')
def raise_exception(message, message_dict, error_code=http.UNPROCESSABLE_REQUEST.code): e = HTTPException(message) e.data = message_dict e.code = error_code raise e