Customizing Zuul Exception

后端 未结 7 1145
说谎
说谎 2020-11-29 05:23

I have a scenario in Zuul where the service that the URL is routed too might be down . So the reponse body gets thrown with 500 HTTP Status and ZuulException in the JSON bod

7条回答
  •  离开以前
    2020-11-29 05:50

        The simplest solution is to follow first 4 steps.
    
    
         1. Create your own CustomErrorController extends
            AbstractErrorController which will not allow the
            BasicErrorController to be called.
         2. Customize according to your need refer below method from
            BasicErrorController.
    
        
     
            @RequestMapping
            public ResponseEntity> error(HttpServletRequest request) {
                Map body = getErrorAttributes(request,
                        isIncludeStackTrace(request, MediaType.ALL));
                HttpStatus status = getStatus(request);
                return new ResponseEntity<>(body, status);
            }
        
    4. You can control whether you want exception / stack trace to be printed or not can do as mentioned below:
    
        server.error.includeException=false
        server.error.includeStacktrace=ON_TRACE_PARAM
        
    ==================================================== 5. If you want all together different error response re-throw your custom exception from your CustomErrorController and implement the Advice class as mentioned below:
    
    
    @Controller
    @Slf4j
    public class CustomErrorController extends BasicErrorController {
    
        public CustomErrorController(ErrorAttributes errorAttributes, ServerProperties serverProperties,
                List errorViewResolvers) {
    
            super(errorAttributes, serverProperties.getError(), errorViewResolvers);
            log.info("Created");
        }
    
        @Override
        public ResponseEntity> error(HttpServletRequest request) {
            Map body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
            HttpStatus status = getStatus(request);
            throw new CustomErrorException(String.valueOf(status.value()), status.getReasonPhrase(), body);
        }
    }
    
    
        @ControllerAdvice
        public class GenericExceptionHandler {
        // Exception handler annotation invokes a method when a specific exception
            // occurs. Here we have invoked Exception.class since we
            // don't have a specific exception scenario.
            @ExceptionHandler(CustomException.class)
            @ResponseBody
            public ErrorListWsDTO customExceptionHandle(
                    final HttpServletRequest request,
                    final HttpServletResponse response,
                    final CustomException exception) {
                    LOG.info("Exception Handler invoked");
                    ErrorListWsDTO errorData = null;
                    errorData = prepareResponse(response, exception);
                    response.setStatus(Integer.parseInt(exception.getCode()));
                    return errorData;
            }
    
            /**
             * Prepare error response for BAD Request
             *
             * @param response
             * @param exception
             * @return
             */
            private ErrorListWsDTO prepareResponse(final HttpServletResponse response,
                    final AbstractException exception) {
                    final ErrorListWsDTO errorListData = new ErrorListWsDTO();
                    final List errorList = new ArrayList<>();
                    response.setStatus(HttpStatus.BAD_REQUEST.value());
                    final ErrorWsDTO errorData = prepareErrorData("500",
                            "FAILURE", exception.getCause().getMessage());
                    errorList.add(errorData);
                    errorListData.setErrors(errorList);
                    return errorListData;
            }
    
            /**
             * This method is used to prepare error data
             *
             * @param code
             *            error code
             * @param status
             *            status can be success or failure
             * @param exceptionMsg
             *            message description
             * @return ErrorDTO
             */
            private ErrorWsDTO prepareErrorData(final String code, final String status,
                    final String exceptionMsg) {
    
                    final ErrorWsDTO errorDTO = new ErrorWsDTO();
                    errorDTO.setReason(code);
                    errorDTO.setType(status);
                    errorDTO.setMessage(exceptionMsg);
                    return errorDTO;
            }
    
        }
        

提交回复
热议问题