Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions flask_restx/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,9 +726,13 @@ def handle_error(self, e):
got_request_exception.send(current_app._get_current_object(), exception=e)

if isinstance(e, HTTPException):
code = HTTPStatus(e.code)
code = None
if e.code is not None:
code = HTTPStatus(e.code)
elif e.response is not None:
code = HTTPStatus(e.response.status_code)
if include_message_in_response:
default_data = {"message": getattr(e, "description", code.phrase)}
default_data = {"message": e.description or code.phrase}
headers = e.get_response().headers
elif self._default_error_handler:
result = self._default_error_handler(e)
Expand Down
19 changes: 18 additions & 1 deletion tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
from flask import Blueprint, abort
from flask.signals import got_request_exception

from werkzeug.exceptions import HTTPException, BadRequest, NotFound, Aborter
from werkzeug import Response
from werkzeug.exceptions import (
Aborter,
BadRequest,
HTTPException,
NotFound,
Unauthorized,
)
from werkzeug.http import quote_etag, unquote_etag

import flask_restx as restx
Expand Down Expand Up @@ -645,6 +652,16 @@ def test_handle_error_with_code(self, app):
assert response.status_code == 500
assert json.loads(response.data.decode()) == {"foo": "bar"}

def test_handle_error_http_exception_response_code_only(self, app):
api = restx.Api(app)
http_exception = HTTPException(response=Response(status=401))

response = api.handle_error(http_exception)
assert response.status_code == 401
assert json.loads(response.data.decode()) == {
"message": "Unauthorized",
}

def test_errorhandler_swagger_doc(self, app, client):
api = restx.Api(app)

Expand Down