Skip to content

Error Handling Exercises

Master Go’s error handling patterns with exercises on custom errors and retry logic.

ExerciseDifficultyTimeDescription
Custom ErrorsEasy15 minCreate custom error types with context
Retry PatternsMedium25 minImplement exponential backoff retry logic
  • Error interface: Implementing the error interface
  • Custom errors: Adding context and metadata to errors
  • Error wrapping: Using fmt.Errorf with %w
  • errors.Is/As: Checking error types and values
  • Retry patterns: Exponential backoff, jitter

Before starting these exercises, you should be comfortable with:

  • Go interfaces
  • The error interface
  • Chapter 2: Error Handling Patterns
  1. Errors should provide enough context to understand what went wrong
  2. Wrap errors at layer boundaries to add context
  3. Use sentinel errors sparingly - prefer error types