Video in progress

How do you standardize the error format and error codes in service-to-service calls across the organization?

For HTTP APIs I take RFC 9457 ProblemDetail as the common format: a stable, machine-readable type/code, status, detail, and extensions like traceId. The error code catalog is open, whether a retry is possible is stated explicitly, and the format is checked with a linter and contract tests.

📖 Answer

Without a standard, client code looks like this: payment-service returns {"error": "..."}, order-service returns {"message": "...", "code": 12}, and customer-service returns an HTML page. Every client writes a separate parser for each service and checks the error text with contains("not found"). If the text changes, the client breaks.

The format:

{
  "type": "https://errors.example.uz/payments/insufficient-funds",
  "title": "Mablag' yetarli emas",
  "status": 422,
  "detail": "Hisobda 120 000 so'm bor, 150 000 so'm kerak",
  "instance": "/payments/812",
  "code": "PAYMENTS.INSUFFICIENT_FUNDS",
  "retryable": false,
  "traceId": "6f1c..."
}

Spring 6 has ProblemDetail and ErrorResponseException ready, and spring.mvc.problemdetails.enabled=true switches the standard Spring errors to this format too. A @RestControllerAdvice in the shared starter maps the main exceptions, so each team doesn't write its own.

The rules (short and mandatory):

  • code (or type) is a contract: it doesn't change and isn't removed. title and detail are for humans, they may change, and clients don't parse them.
  • Codes have a domain prefix and live in a central catalog: which service, what it means, what the client should do.
  • Retry semantics are explicit: with 4xx don't retry, with 503 and 429 retrying is possible with Retry-After. Retry policies rely on the contract, not on guesses.
  • Internal details (stack traces, SQL, internal host names) never appear in a response: they are only in the log, found through the traceId.
  • A downstream error is translated into your own domain, not passed through blindly.

The same idea applies in asynchronous flows: the error code and reason in the headers of a message that landed in the DLT, a code field in saga replies.

Hold the standard with tools, not documents: a shared Problem schema in OpenAPI and a linter rule (for example, Spectral), and error responses are checked in contract tests too. The best lever for spreading a standard is to hand it out free through a ready starter: a team gets the right format without writing anything.