Should I align the formats of REST API request validation and database error responses?

Viewed 132

In FastAPI, I have an endpoint that will return jsonified Pydantic's ValidationError if specific string in request body is empty:

{"detail":[{"loc":["body","text"],"msg":"ensure this value has at least 1 characters","type":"value_error.any_str.min_length","ctx":{"limit_value":1}}]}

Same endpoint will return a jsonified HTTPException if the string is not unique (this constraint is set on database level, not Pydantic).

{"detail":"This field cannot be empty"}

So the format of errors for this endpoint is not unified. However, my frontend expects it to be.

I know I could write custom error handlers on the backend to return errors in unified format, but should I do so? Or is it better to adjust my frontend to be able to extract the errors in both formats?

0 Answers
Related