123 lines
4.4 KiB
Java
123 lines
4.4 KiB
Java
package com.cdzy.gateway;
|
|
|
|
import cn.dev33.satoken.exception.NotLoginException;
|
|
import com.cdzy.common.enums.Code;
|
|
import com.cdzy.common.model.response.JsonResult;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
|
|
import org.springframework.cloud.gateway.support.NotFoundException;
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.core.annotation.Order;
|
|
import org.springframework.core.io.buffer.DataBuffer;
|
|
import org.springframework.http.HttpHeaders;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
|
import org.springframework.web.reactive.resource.NoResourceFoundException;
|
|
import org.springframework.web.server.ServerWebExchange;
|
|
import reactor.core.publisher.Mono;
|
|
|
|
/**
|
|
* 网关统一异常处理
|
|
*
|
|
* @author ruoyi
|
|
*/
|
|
@Order(-1)
|
|
@Configuration
|
|
public class GatewayExceptionHandler implements ErrorWebExceptionHandler {
|
|
private static final Logger log = LoggerFactory.getLogger(GatewayExceptionHandler.class);
|
|
|
|
@NotNull
|
|
@Override
|
|
public Mono<Void> handle(ServerWebExchange exchange, @NotNull Throwable ex) {
|
|
ServerHttpResponse response = exchange.getResponse();
|
|
|
|
if (exchange.getResponse().isCommitted()) {
|
|
return Mono.error(ex);
|
|
}
|
|
|
|
String msg;
|
|
Integer code = Code.FAILED;
|
|
Throwable cause = ex.getCause();
|
|
if (cause instanceof NotLoginException) {
|
|
msg = cause.getMessage();
|
|
code = Code.UN_AUTHENTICATE;
|
|
return webFluxResponseWriter(response, msg, code);
|
|
} else if (ex instanceof NotFoundException) {
|
|
msg = "服务未找到";
|
|
} else if (ex instanceof NoResourceFoundException) {
|
|
msg = "路径不存在";
|
|
} else {
|
|
msg = "内部服务器错误";
|
|
}
|
|
|
|
log.error("[网关异常处理]请求路径:{},异常信息:{}", exchange.getRequest().getPath(), ex.getMessage());
|
|
|
|
return webFluxResponseWriter(response, msg, code);
|
|
}
|
|
|
|
/**
|
|
* 设置webflux模型响应
|
|
*
|
|
* @param response ServerHttpResponse
|
|
* @param value 响应内容
|
|
* @param code 响应码
|
|
* @return Mono<Void>
|
|
*/
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value,Integer code) {
|
|
return webFluxResponseWriter(response, HttpStatus.OK, value, code);
|
|
}
|
|
|
|
/**
|
|
* 设置webflux模型响应
|
|
*
|
|
* @param response ServerHttpResponse
|
|
* @param code 响应状态码
|
|
* @param value 响应内容
|
|
* @return Mono<Void>
|
|
*/
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, Object value, int code) {
|
|
return webFluxResponseWriter(response, HttpStatus.OK, value, code);
|
|
}
|
|
|
|
/**
|
|
* 设置webflux模型响应
|
|
*
|
|
* @param response ServerHttpResponse
|
|
* @param status http状态码
|
|
* @param code 响应状态码
|
|
* @param value 响应内容
|
|
* @return Mono<Void>
|
|
*/
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, HttpStatus status, Object value, int code) {
|
|
return webFluxResponseWriter(response, MediaType.APPLICATION_JSON_VALUE, status, value, code);
|
|
}
|
|
|
|
/**
|
|
* 设置webflux模型响应
|
|
*
|
|
* @param response ServerHttpResponse
|
|
* @param contentType content-type
|
|
* @param status http状态码
|
|
* @param code 响应状态码
|
|
* @param value 响应内容
|
|
* @return Mono<Void>
|
|
*/
|
|
public static Mono<Void> webFluxResponseWriter(ServerHttpResponse response, String contentType, HttpStatus status, Object value, int code) {
|
|
response.setStatusCode(status);
|
|
response.getHeaders().add(HttpHeaders.CONTENT_TYPE, contentType);
|
|
JsonResult<String> result = JsonResult.failed(code,value.toString());
|
|
ObjectMapper objectMapper = new ObjectMapper();
|
|
DataBuffer dataBuffer = null;
|
|
try {
|
|
dataBuffer = response.bufferFactory().wrap(objectMapper.writeValueAsBytes(result));
|
|
} catch (JsonProcessingException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
return response.writeWith(Mono.just(dataBuffer));
|
|
}
|
|
} |