spring-cloud-netflix: Zuul filters exception handling

Current implementation of exception handling in spring-cloud-zuul is quite ambigious. I tried to simplify it a little bit in my PR. Now I want to handle specific exceptions and return appropriate responses for them. Normally in any spring application I would create separate class annotated with ControllerAdvice and having methods with @ExceptionHandler for specific exception types. In spring-cloud-zuul exceptions get handled by BasicErrorController and none of ExceptionHandler annotated methods get executed. Is it a bug or an intenteded decision and what are the reasons?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 14
  • Comments: 17 (11 by maintainers)

Most upvoted comments

Understood. Thanks for the update.

For example if timeout occurs while routing to service client gets following response from Zuul:

{
"timestamp":1479314971425,
"status":500,
"error":"Internal Server Error",
"exception":"com.netflix.zuul.exception.ZuulException",
"message":"TIMEOUT"
}

This is default message that is produced by BasicErrorController. But I want to not only have my own message format in case of such or other ZuulException I would also like to handle exceptions the way it is done in standart spring applications via @ExceptionHandler. Right now to achieve this I need to extend AbstractErrorController and the basic implementation of custom exception handling looks like that:

@RestController
public class ZuulErrorController extends AbstractErrorController {

    @Value("${error.path:/error}")
    private String errorPath;

    public ZuulErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes);
    }

    @Override
    public String getErrorPath() {
        return errorPath;
    }

    @RequestMapping(value = "${error.path:/error}", produces = "application/json;charset=UTF-8")
    public ResponseEntity error(HttpServletRequest request) {
        HttpStatus status = getStatus(request);
        final String errorMessage = getErrorMessage(request);
        return ResponseEntity.status(status).body(generateErrorResponse(UNKNOWN_ERROR, errorMessage));
    }

    private String getErrorMessage(HttpServletRequest request) {
        final Throwable exc = (Throwable) request.getAttribute("javax.servlet.error.exception");
        return exc != null ? exc.getMessage() : "Unexpected error occurred";
    }
}

Please advice how exception handling in Zuul can be done the right way. Thanks.

@stiyyagura Would you mind explaining what did you end up doing, so if someone stumbles upon this thread later, they’ll have something to go on.