Back to all reviewers

Clear informative errors

fastapi/fastapi
Based on 2 comments
Python

Error messages should be clear, specific, and provide enough context to understand the issue without exposing unnecessary implementation details. When handling errors:

Error Handling Python

Reviewer Prompt

Error messages should be clear, specific, and provide enough context to understand the issue without exposing unnecessary implementation details. When handling errors:

  1. Include specific details about what went wrong rather than using generic messages
  2. Consider using appropriate attributes of the exception (like e.msg) rather than the entire exception
  3. For common error cases, provide tailored error messages with appropriate status codes
  4. Keep error handling code clean by avoiding unnecessary control structures

Example - 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)
2
Comments Analyzed
Python
Primary Language
Error Handling
Category

Source Discussions