tiangolo/fastapi

axios can't receive error response status code

2277509846 opened this issue · 0 comments

Privileged issue

  • I'm @tiangolo or he asked me directly to create an issue here.

Issue Content

Here is a middleware I defined:
class AccessControlMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next: Callable[..., Awaitable[Response]]) -> Response:
if request.url.path == '/api/forbidden':
return Response(status_code=status.HTTP_403_FORBIDDEN, content="Forbidden")
response = await call_next(request)
return response

When I access an interface that does not exist, axios can know the 404 status code from the response, but when I access /api/forbidden, I cannot know the status code.

axios.get('http://127.0.0.1:8000/api/notfound').then(res => {
console.log(res);
}).catch(err => {
console.log(err.response); // axios error contains 404
});

axios.get('http://127.0.0.1:8000/api/forbidden').then(res => {
console.log(res);
}).catch(err => {
console.log(err.response); // undefined
})