question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to catch `Exception` via exception_handlers since FastAPI 0.70.0

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

################################################
#
# Pay attention: below is Python 3.10 syntax
#
################################################

from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from pydantic import ValidationError

async def internal_server_error(request: Request, exc: Exception | ValidationError) -> "JSONResponse":
    return JSONResponse(content={"detail": "Internal Server Error"}, status_code=500)

app = FastAPI(
    exception_handlers={
        Exception: internal_server_error,
        ValidationError: internal_server_error,
    },
)

@app.get("/call_me")
async def raise_exception() -> None:
    raise Exception("I am exception.")

Description

  • Open the browser and call the endpoint: /call_me
  • It returns plain text: Internal Server Error
  • But I excepted it return JSON: {"detail": "Internal Server Error"}

Operating System

Linux

Operating System Details

No response

FastAPI Version

0.70.0

Python Version

Python 3.10.0

Additional Context

It works fine on FastAPI 0.68.2. Issue is also affected 0.69.0

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
alexiricommented, Nov 10, 2021

I’m eagerly looking forward to https://github.com/tiangolo/fastapi/pull/4145 getting merged. 😄

2reactions
gromsteruscommented, Nov 1, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

Release Notes - FastAPI
After the dependency with yield handles the exception (or not) the exception is raised again. So that any exception handlers can catch it,...
Read more >
Catch `Exception` globally in FastAPI - Stack Overflow
In case you want to capture all unhandled exceptions (internal server error), there's a very simple way of doing it. Documentation
Read more >
fastapi - PyPI
Robust: Get production-ready code. With automatic interactive documentation. Standards-based: Based on (and fully compatible with) the open standards for APIs: ...
Read more >
Implementing FastAPI Services - Camillo Visini
To handle custom exceptions occurring at the service layer, as instances of class AppExceptionCase , a respective exception handler is added to ...
Read more >
fastapi Changelog - pyup.io
After the dependency with `yield` handles the exception (or not) the exception is raised again. So that any exception handlers can catch it,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found