Template: Error Handling Strategy Document

Purpose

To define a consistent error handling strategy across services, ensuring predictable responses, easier debugging, and better user experience.


Scope

  • Applies to all backend services (APIs, microservices, serverless).
  • Used by Backend Developers, QA, Frontend Developers, and DevOps.
  • Covers error classification, format, codes, retry logic, and logging standards.

Structure

Section 1 – Error Classification

CategoryDescriptionExample (SaaS)Example (B2C)
Validation ErrorsInvalid input or missing fieldsEmail format invalidBooking date missing
Authentication/AuthorizationIdentity or role issuesInvalid JWT tokenUser not logged in
Business Logic ErrorsDomain-specific failuresUser already onboardedCleaner not available
Resource ErrorsMissing entities or duplicatesUser not foundBooking not found
System ErrorsInfra or dependency failureDB connection timeoutPayment gateway down
Rate LimitsToo many requests429 Too Many RequestsApp spamming API

Section 2 – Error Format (REST & GraphQL)

AspectREST ExampleGraphQL Example
Envelope{ "error": { "code": 400, "message": "Invalid email" } }{ "errors": [ { "message": "Invalid email", "path": ["createUser"] } ] }
Fieldscode, message, details, traceIdmessage, path, extensions.code
TraceabilityInclude traceId in all errorsExtensions field for trace IDs

Section 3 – Error Codes

CodeCategoryDescriptionExample
200SuccessRequest processed successfullyGET /users
400Validation ErrorInvalid/missing inputWrong email format
401UnauthorizedMissing/invalid tokenExpired JWT
403ForbiddenUser lacks permissionNon-admin updating roles
404Not FoundEntity doesn’t existBooking ID invalid
409ConflictDuplicate or state conflictEmail already in use
429Rate LimitToo many requestsThrottled client
500Server ErrorUnexpected failureDB crash
502/503Dependency ErrorDownstream service unavailableStripe API down

Section 4 – Retry & Recovery Guidelines

Error CategoryShould Retry?Client ActionServer Action
ValidationFix inputN/A
AuthRefresh token / loginN/A
Business LogicAdjust requestLog & notify
System (5xx)✅ (with backoff)Retry with exponential backoffCircuit breaker / fallback
Rate Limit (429)✅ (after wait)Respect Retry-After headerInclude rate-limit headers

Section 5 – Logging & Monitoring

RequirementStandard
Log LevelWARN for client errors, ERROR for server failures
ContextInclude traceId, userId (if available), requestId
StorageCentralized log collector (e.g., ELK, Datadog)
AlertsTrigger alerts on repeated 5xx or spikes in 4xx
RedactionStrip sensitive data (PII, tokens) before logging

Section 6 – User Messaging (Frontend Integration)

CategoryDeveloper-Facing MessageUser-Facing Message
Validation Error"Invalid email format"“Please enter a valid email.”
Auth Error"Expired JWT"“Your session expired. Please log in again.”
System Error"DB timeout on query X"“Something went wrong. Try again later.”

Blank Reusable Template

Error Classification

| Category | Description | Example |

Error Format

| Aspect | Contract |

Error Codes

| Code | Category | Description | Example |

Retry & Recovery

| Error Category | Should Retry? | Client Action | Server Action |

Logging & Monitoring

| Requirement | Standard |

User Messaging

| Category | Developer-Facing Message | User-Facing Message |