2

We have a complex business logic where we raise several HTTPExceptions with FastAPI if an error occurs. When we raise an exception we define a status code and a detail error message. The problem is that this error message is in english but our application has a german interface and we would like to display this error message translated to german in a toastr.

Unfortunately FastAPI doesn't describe what to do if you want to display the detail error message in another language so I was wondering what the best way would be to show a translated error message when we raise said HttpExceptions.

A HTTPException with FastAPI looks somewhat like this:

raise HTTPException(
    status_code=HTTP_403_FORBIDDEN, detail=PERMISSION_ERROR
)

We can additionally maybe add some custom headers but that's basically it.

  • 1
    You can use pydantic-i18n: https://pydantic-i18n.boardpack.org/ – fchancel May 18 '22 at 12:17
  • This looks promising, thank you very much! – Tobias Landmann May 18 '22 at 12:42
  • Have you looked into a possible use of [interceptors](https://angular.io/api/common/http/HttpInterceptor)? –  May 18 '22 at 12:53
  • I'm not sure if your problem lies in catching the error before it is being displayed, or the translating itself. There are libraries like [ngx-translate](https://github.com/ngx-translate/core) to help with this. –  May 18 '22 at 12:55

0 Answers0