preface
Because the Spring Clod Gateway uses a Web Flux implementation, the @ControllerAdvice method cannot be used for global exception handling and needs to be handled using the implementation class of the ErrorWebExceptionHandler interface. So we inherit DefaultErrorWebExceptionHandler (realizing the unity of the custom exception handling.
CustomErrorWebExceptionHandler implementation
/** * Description: Custom global exception handler **@author xhsf
* @create2020/11/26 treasure * /
public class CustomErrorWebExceptionHandler extends DefaultErrorWebExceptionHandler {
public CustomErrorWebExceptionHandler( ErrorAttributes errorAttributes, ResourceProperties resourceProperties, ErrorProperties errorProperties, ApplicationContext applicationContext) {
super(errorAttributes, resourceProperties, errorProperties, applicationContext);
}
/** * get the exception attribute *@param request ServerRequest
* @param includeStackTrace includeStackTrace
* @return Map<String, Object>
*/
@Override
protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
Map<String, Object> map = new HashMap<>();
map.put("success".false);
map.put("data".null);
Throwable error = super.getError(request);
map.put("message", error.getMessage());
map.put("errorCode", ErrorCode.INVALID_PARAMETER);
// The gateway is abnormal
if (error instanceof NotFoundException) {
map.put("errorCode", ErrorCode.INVALID_PARAMETER_NOT_FOUND);
}
return map;
}
/** * specifies a JSON method for handling the response@param errorAttributes ErrorAttributes
* @return RouterFunction<ServerResponse>
*/
@Override
protected RouterFunction<ServerResponse> getRoutingFunction(ErrorAttributes errorAttributes) {
return RouterFunctions.route(RequestPredicates.all(), this::renderErrorResponse);
}
/** * get the corresponding HttpStatus **@param errorAttributes Map<String, Object>
* @return HttpStatus
*/
@Override
protected int getHttpStatus(Map<String, Object> errorAttributes) {
ErrorCode errorCode = (ErrorCode) errorAttributes.get("errorCode");
returnerrorCode.getHttpStatus().value(); }}Copy the code
The focus here is on the getErrorAttributes() and getHttpStatus() methods. GetErrorAttributes () is used to get an exception and to customize the response result based on the type of exception. Here I have encapsulated the {success, data, message, errorCode} structures that the project uses uniformly.
GetHttpStatus () is used to customize the HTTP status code based on the results from getErrorAttributes() because ErrorCode already has the HTTP status code attached so it can be retrieved directly.
ErrorHandlerConfig
This class is used to configure a custom handler.
/** * Description: Override default global exception handling **@author xhsf
* @create2020/11/26 he * /
@Configuration
@EnableConfigurationProperties({ServerProperties.class, ResourceProperties.class})
public class ErrorHandlerConfig {
private final ServerProperties serverProperties;
private final ApplicationContext applicationContext;
private final ResourceProperties resourceProperties;
private final List<ViewResolver> viewResolvers;
private final ServerCodecConfigurer serverCodecConfigurer;
public ErrorHandlerConfig(ServerProperties serverProperties, ResourceProperties resourceProperties, ObjectProvider
> viewResolversProvider, ServerCodecConfigurer serverCodecConfigurer, ApplicationContext applicationContext)
{
this.serverProperties = serverProperties;
this.applicationContext = applicationContext;
this.resourceProperties = resourceProperties;
this.viewResolvers = viewResolversProvider.getIfAvailable(Collections::emptyList);
this.serverCodecConfigurer = serverCodecConfigurer;
}
@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public ErrorWebExceptionHandler errorWebExceptionHandler(ErrorAttributes errorAttributes) {
CustomErrorWebExceptionHandler exceptionHandler = new CustomErrorWebExceptionHandler(
errorAttributes, resourceProperties, serverProperties.getError(), applicationContext);
exceptionHandler.setViewResolvers(viewResolvers);
exceptionHandler.setMessageWriters(serverCodecConfigurer.getWriters());
exceptionHandler.setMessageReaders(serverCodecConfigurer.getReaders());
returnexceptionHandler; }}Copy the code