Scrivio Interview library
DesignAdvanced7 min read · updated 12 November 2026

Define a consistent error-handling standard for WebFlux services: how exceptions map to HTTP responses, what is logged, and how upstream errors are translated.

I would standardise on RFC 9457 problem details as the error response format, enabled with spring.webflux.problemdetails.enabled or a custom handler, map domain exceptions to statuses in one @RestControllerAdvice, and keep a WebExceptionHandler for errors raised outside controllers, such as in filters. Downstream failures are translated into our own error codes without leaking upstream bodies, each error is logged once at the edge with a correlation id, and streaming endpoints get their own rule because once the response is committed the status can no longer change.

Why this question is asked

Without a standard, every WebFlux service invents its own error JSON, logs the same failure at five operators, and occasionally returns a downstream service's raw error body, internal hostnames included, to the public. Clients then need special code per service, and on-call engineers search noisy logs. A good standard answers three questions consistently: what the client sees, what we log, and how errors from other services are translated. Interviewers ask senior candidates for this because it shows whether they can turn framework features into rules a whole team can follow. In this answer, we will recap the reactive error path, choose the response format, place the mapping code, translate upstream errors, settle the logging rule, and handle streaming responses.

1A Quick Recap: Where Errors Go in WebFlux

In WebFlux, an exception thrown in a controller, or an error signal from the Mono it returns, travels up to the framework. Spring first looks for an @ExceptionHandler method, in the controller or in a @ControllerAdvice class, that matches the exception type. If none matches, or if the error happened outside a controller, for example in a WebFilter, a security filter or while routing, the error reaches the chain of WebExceptionHandler beans. Spring Boot registers one called DefaultErrorWebExceptionHandler at the end of that chain, which renders the default error response.

So there are two layers: controller-level handling for exceptions our code knows about, and a global handler as the safety net for everything else. A standard must cover both, or errors from filters will look different from errors from controllers.

Step throughHow an error becomes a response
  1. Error in controller or its Mono?
    YesLook for a matching @ExceptionHandlerNoGo to WebExceptionHandler chain
  2. Matching @ExceptionHandler found?in the controller or a @ControllerAdvice
    YesReturn ProblemDetail with our codeNoGo to WebExceptionHandler chain
  3. WebExceptionHandler chainour handler first, Boot's default last
  4. Response written as application/problem+jsonstatus, type, title, code, traceId

4 steps in order. Play them, or step through them one at a time.

2The Response Format: Problem Details

RFC 9457, which replaced RFC 7807, defines problem details: a small JSON object with the media type application/problem+json and the fields type (a URI naming the kind of error), title, status, detail and instance, plus any extension fields we add. Spring Framework 6 supports it natively with the ProblemDetail class and ErrorResponse exceptions, and Spring Boot can turn it on for its built-in exceptions with spring.webflux.problemdetails.enabled=true.

JSON
{
"type": "https://errors.example.com/order-not-found",
"title": "Order not found",
"status": 404,
"detail": "No order with id 42",
"instance": "/orders/42",
"code": "ORDER_NOT_FOUND",
"traceId": "4bf92f3577b34da6a3ce929d0e0e4736"
}

Using a published standard instead of our own format means clients and tools already understand it. The two extension fields are the important additions: code, a stable machine-readable error code clients can switch on, and traceId, so a support ticket can be matched to the logs.

3Mapping Domain Exceptions in One Place

Domain code throws meaningful exceptions, such as OrderNotFoundException or InsufficientStockException, and never builds HTTP responses itself. One @RestControllerAdvice per service, ideally extending a base class from the shared starter, maps them.

Java
@RestControllerAdvice
class ApiErrors extends ResponseEntityExceptionHandler { // reactive variant: Spring's built-in errors as ProblemDetail
@ExceptionHandler(OrderNotFoundException.class)
ProblemDetail notFound(OrderNotFoundException e) {
ProblemDetail p = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, e.getMessage());
p.setType(URI.create("https://errors.example.com/order-not-found"));
p.setProperty("code", "ORDER_NOT_FOUND");
return p; // 404, application/problem+json
}
@ExceptionHandler(UpstreamUnavailableException.class)
ProblemDetail upstream(UpstreamUnavailableException e) {
ProblemDetail p = ProblemDetail.forStatus(HttpStatus.SERVICE_UNAVAILABLE);
p.setProperty("code", e.code()); // "PRICING_UNAVAILABLE", no upstream body
return p; // 503
}
}

The ResponseEntityExceptionHandler used here is the reactive one from org.springframework.web.reactive.result.method.annotation; it renders Spring's own exceptions, such as validation failures and unsupported media types, as problem details too. For errors that never reach a controller, the starter also registers a WebExceptionHandler, ordered before Boot's default, that renders the same format, so a request rejected by a filter looks like any other error. Unknown exceptions become a 500 with a generic title and code INTERNAL_ERROR, never the exception message, which might contain internal details.

4Translating Upstream Errors

When a WebClient call to another service fails, the tempting shortcut is to pass its status and body straight through. That leaks internal details, such as hostnames, stack traces or other services' error codes, and it couples our clients to services they should not know about. The rule is: catch upstream errors at the client adapter and translate them into our own exceptions with our own codes.

A 404 from the pricing service might mean 'no price yet' and become an empty result or our own 404; a 500 or a timeout becomes UpstreamUnavailableException with code PRICING_UNAVAILABLE and status 503 or 502. The upstream status and a short, safe extract of its body go into the log, not the response. This translation lives in one place per downstream dependency, typically an onErrorResume and onErrorMap at the end of the client method.

5Logging Once, With Correlation Ids

Reactive pipelines make it easy to log the same error many times: doOnError in the repository, in the service, in the controller. The standard says an error is logged exactly once, at the edge, by the global handler, with the trace id, the error code, the status, the request path and the exception with its cause chain. Client errors (4xx) are logged at a lower level, often without a stack trace, because they are usually the client's problem; server errors (5xx) are logged at error level with the full cause.

Correlation ids come from tracing: with Micrometer Tracing and automatic context propagation enabled, the trace id is in the MDC on whichever thread runs the handler, and it is also put in the response's traceId field. Intermediate layers that want to add context do so by wrapping the exception with more information, not by logging it.

6Streaming Responses and the Committed Response

There is one case the normal flow cannot handle. For a streaming endpoint, such as Server-Sent Events or a Flux written as JSON lines, the status 200 and headers are sent with the first element. Once the response is committed, an error later in the stream cannot change the status, and a problem details body cannot be written. The connection simply ends, and the client may see a truncated stream.

So the standard has a streaming rule: validate and authorise everything that can fail before the first element, so those failures still produce proper error responses; for failures during the stream, send a final error event with the same code and trace id fields, then complete; and make clients treat a stream without an explicit completion event as failed. The global handler should detect a committed response and only log, rather than trying to write an error body.

Java
Flux<ServerSentEvent<Object>> events = prices.stream()
.map(p -> ServerSentEvent.builder((Object) p).event("price").build())
.onErrorResume(e -> Flux.just(ServerSentEvent.builder((Object) Map.of(
"code", "PRICING_UNAVAILABLE", "traceId", traceId()))
.event("error").build())); // a final event the client understands; status stays 200

7Follow-up questions

Why keep a WebExceptionHandler if we have @ControllerAdvice?

Because @ControllerAdvice only sees exceptions from controller methods. Errors from WebFilters, Spring Security, request routing or body decoding before a handler is chosen never reach it. Without a global handler that renders the same format, those errors come out in Boot's default shape, and clients see two error formats from one service.

Should validation errors list every field?

Yes, as an extension field such as errors with one entry per field, holding the field name, a code and a human message. That is client-facing information they need to fix the request. What must not appear is internal detail like class names or SQL, which belong in logs.

How do we stop teams returning raw exception messages?

Put the base advice and global handler in the shared starter so the safe behaviour is the default, and add a test in the starter's test kit that sends requests causing unknown exceptions and asserts that the response contains only the generic title and code. Code review then only needs to watch for new domain exceptions.

8Conclusion

In this answer, we designed an error-handling standard for WebFlux services. Responses use RFC 9457 problem details, enabled through spring.webflux.problemdetails.enabled or our own handlers, with stable codes and trace ids as extension fields. Domain exceptions are mapped in one @RestControllerAdvice, while a WebExceptionHandler covers errors outside controllers. Upstream errors are translated into our own codes and never passed through, errors are logged once at the edge with correlation ids, and streaming endpoints validate before the first element and signal later failures with a final error event, because a committed response cannot change its status.

What to say in the interviewAbout 90 seconds aloud

Open with

I would standardise on RFC 9457 problem details as the only error format, so every response is application/problem+json with type, title, status and detail, plus two extensions: a stable code clients can rely on and the trace id.

Then, if they want more

Spring Boot can enable it for built-in exceptions with spring.webflux.problemdetails.enabled, and our shared starter provides a base @RestControllerAdvice that maps domain exceptions like OrderNotFoundException to statuses and codes, and turns unknown exceptions into a generic 500 without the message.

Because controller advice does not see errors from filters, security or routing, the starter also registers a WebExceptionHandler ahead of Boot's default that renders the same format.

For downstream calls, each WebClient adapter translates errors into our own exceptions, for example a pricing 500 or timeout becomes PRICING_UNAVAILABLE with a 503, and upstream bodies only go to the logs, never to clients.

Logging happens once, at the edge, with the trace id from Micrometer Tracing and automatic context propagation, at warn for 4xx and error for 5xx with the cause chain; intermediate layers wrap exceptions rather than log them.

Finally, streaming endpoints need their own rule: once the first element is sent the status is committed, so we validate before streaming and send a final error event with the code and trace id if something fails mid-stream.

About this answer

Last updated
12 November 2026
Versions it names
Spring Framework 6
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Define a consistent error-handling standard for WebFlux services: how exceptions map to...": https://scrivio-interview-library.netlify.app/spring-boot/webflux/97d9fcff75/