Error messages should be clear, specific, and provide enough context to understand the issue without exposing unnecessary implementation details. When handling errors:
Error messages should be clear, specific, and provide enough context to understand the issue without exposing unnecessary implementation details. When handling errors:
e.msg
) rather than the entire exceptionExample - Before:
except Exception as e:
http_error = HTTPException(
status_code=400, detail="There was an error parsing the body"
)
# And with unnecessary else:
if is_multipart_body and has_dict_error:
raise RequestInvalidContentTypeException(
status_code=415,
detail="Unsupported media type: multipart/form-data (expected application/json)",
)
else:
raise RequestValidationError(errors, body=body)
Example - After:
except Exception as e:
http_error = HTTPException(
status_code=400, detail=f"There was an error parsing the body: {e.msg}"
)
# Without unnecessary else (since the first raise will exit):
if is_multipart_body and has_dict_error:
raise RequestInvalidContentTypeException(
status_code=415,
detail="Unsupported media type: multipart/form-data (expected application/json)",
)
raise RequestValidationError(errors, body=body)
Enter the URL of a public GitHub repository