In a previous series, we discussed a real-world pattern for exception handling and validation in Spring Boot & MVC REST Web Services. Please go through first if you have not. To summarize, we recommended coding an ErrorComposer to compose errors from exception objects, which could be used in a controller advice as well as a custom error controller. The controller advice would catch exceptions bubbling up from your controllers, whereas the error controller would be the ultimate weapon for handling exceptions raised at all levels, e.g. in filters.

In WebFlux though, Spring Boot provides a WebExceptionHandler filter instead of the error controller. So, instead of providing your custom error controller and error attributes, you would need to provide a custom WebExceptionHandler and/or ErrorAttributes. For more details, refer to Spring Boot documentation.

So, in our case, we can just code a custom error attributes bean and use our ErrorComposer in that. Spring Boot will recognize that bean and use that in its WebExceptionHandler. It can be coded as below:

public class CustomErrorAttributes<T extends Throwable> extends DefaultErrorAttributes {
    private ErrorResponseComposer<T> errorResponseComposer;
    public Map<String, Object> getErrorAttributes(ServerRequest request,
            boolean includeStackTrace) {
        Map<String, Object> errorAttributes = super.getErrorAttributes(request, includeStackTrace);     
        addErrorDetails(errorAttributes, request);
        return errorAttributes;
    protected void addErrorDetails(
            Map<String, Object> errorAttributes, ServerRequest request) {
        Throwable ex = getError(request);
        errorAttributes.put("exception", ex.getClass().getSimpleName());
        errorResponseComposer.compose((T)ex).ifPresent(errorResponse -> {
            if (errorResponse.getMessage() != null)
                errorAttributes.put("message", errorResponse.getMessage());
            Integer status = errorResponse.getStatus();
            if (status != null) {
                errorAttributes.put("status", status);
                errorAttributes.put("error", errorResponse.getError());
            if (errorResponse.getErrors() != null)
                errorAttributes.put("errors", errorResponse.getErrors());           

As you see, it extends Spring Boot's DefaultErrorAttributes and overrides the getErrorAttributes method, adding the attributes compiled by our ErrorComposer. For an exact implementation, look at Spring Lemon's LemonReactiveErrorAttributes class.

With this, you can handle all exceptions in a WebFlux REST API seamlessly!