Error Handling

Error handling for Spring Boot microservices

Spring Middleware defines a unified error handling model for Spring Boot microservices. It provides structured error responses, domain-specific error codes, and consistent propagation across service boundaries for both REST and GraphQL APIs.

Structured error responses Domain error codes Request and span correlation Remote error propagation REST and GraphQL consistency

What the error model provides

Errors are not treated as raw HTTP responses or framework exceptions. The platform defines a consistent structure that allows clients and services to understand failures in a predictable and machine-readable way.

  • Stable error contract for clients
  • Explicit domain-level error codes
  • Context propagation across services
  • Clear distinction between domain and technical failures

Where it applies

The same error model is used across the platform, regardless of the entry point or communication style.

  • REST APIs: structured JSON error payloads
  • GraphQL APIs: error codes exposed through extensions
  • Service communication: errors propagated across services

HTTP error representation

All REST endpoints return errors using the same JSON structure, making error handling predictable for clients and consistent across services.

Standard error payload

{
  "statusCode": 404,
  "statusMessage": "Not Found",
  "code": "PRODUCT:NOT_FOUND",
  "message": "Product not found",
  "extensions": {}
}

Key fields

  • statusCode: HTTP status (e.g. 404, 500)
  • code: domain error identifier (DOMAIN:KEY)
  • message: human-readable description
  • extensions: contextual metadata

Error propagation across services

Errors produced by one service are propagated through the communication layer without losing structure or metadata.

Service A throws error Communication Layer maps to structured error Service B receives structured error

What is preserved

  • HTTP status and error code
  • Domain-level meaning
  • Request ID and span information
  • Call chain across services

RemoteServerException

Remote failures are wrapped into structured exceptions that expose remote service metadata and allow applications to inspect or translate errors.

GraphQL error representation

GraphQL uses the same domain error codes, exposed through the extensions field.

GraphQL error example

{
  "message": "Product not found",
  "path": ["product"],
  "extensions": {
    "code": "PRODUCT:NOT_FOUND"
  }
}

Consistency with REST

  • Same domain error codes
  • Same error catalog
  • Same propagation model
  • Different transport, same semantics

Guidelines for service authors

Services should align with the error model to ensure consistency across the platform.

Define clear error codes

Use DOMAIN:KEY patterns like PRODUCT:NOT_FOUND or ORDER:INVALID_STATE.

Use ServiceException

Throw structured exceptions instead of raw runtime errors.

Avoid leaking internals

Translate low-level failures into domain-level errors.