Skip to content

Instantly share code, notes, and snippets.

@kjivan
Last active July 3, 2024 19:56
Show Gist options
  • Save kjivan/2e583320a1b4718ffccffd97cfe8bcf2 to your computer and use it in GitHub Desktop.
Save kjivan/2e583320a1b4718ffccffd97cfe8bcf2 to your computer and use it in GitHub Desktop.
Error Handling Best Practices

Error Handling Best Practices

  • System Errors
    • eg null pointers/failed db query/failed api calls
    • Log this at the error level
    • Log stack trace if possible
    • Log any relevant identifiers if possible
    • Don't log any unapproved sensitive data
  • User errors
    • eg invalid zip code/name length
    • Optionally log at warn level
      • Still an error for the user just not the system
    • Notify user of the error
      • Use Alerts, Toast, HTTP Codes/Payload, Kaka Message
        • Clearly define the specific issue
          • eg invalid last name is better than invalid request
          • eg last name too long is better than invalid last name
          • eg last name must be less than 25 characters better than last name too long
        • If it makes sense describe ways to correct issue
          • eg if database is locked ask user to retry later

Exceptions

  • Do not throw an exception if it can be handled in the current function
  • Catch user error exceptions so they don't trigger error messages
  • Do clean up regardless of exception in finally block
  • Use framework exception handling where applicable (eg. @ControllerAdvice)

Related

Logging Best Practices

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment