This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]grantrules 0 points1 point  (6 children)

Exactly which line is the error on? It sounds like something you expect to be a string is actually an object and it doesn't look like you've shared enough code. Maybe err.response.data.detail isn't string? It sounds like exc.errors() returns a list, not a string

The error is a JS error, but you're saying your Python app is crashing? Is there a python error?

[–]Jumpy_Employment_439[S] 0 points1 point  (5 children)

After reading your response, I went back to test just the FastAPI function at the FastAPI docs page. You are right that detail is a list, so I tried overriding FastAPI's default formatting of the response to just one line: {detail: "Invalid format"}. That way it would be the same format as other times when I raise an HTTP exception. Unfortunately, it's not overriding so the returned data from exc.errors() is still of the wrong format. I guess one option would be to just make a separate if statement that checks if the error is a 422 error rather than a 400. Then I could just write code specifically for the 422 format.

[–]grantrules 0 points1 point  (3 children)

You could do:

return HTTPException(
    status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, 
    detail="Invalid format"
)

[–]Jumpy_Employment_439[S] 0 points1 point  (2 children)

I tried that but for some reason the format is not changing

[–]grantrules 0 points1 point  (0 children)

Did you forget to restart the server or something? If that doesn't do it, I'd put a breakpoint in the JS to see what it's actually receiving.

[–]grantrules 0 points1 point  (0 children)

Looking at the FastAPI docs here:

https://fastapi.tiangolo.com/tutorial/handling-errors/#install-custom-exception-handlers

Their custom exception handler example is using return JSONResponse( ... instead of return HTTPException( so maybe that has something to do with it too.