From 5438d263a78b11228a8325bb5c8b6daef416adfd Mon Sep 17 00:00:00 2001 From: lbw Date: Mon, 11 Sep 2023 13:26:21 +0800 Subject: [PATCH] =?UTF-8?q?:fire:=20=E5=88=A0=E9=99=A4=20GlobalExceptionCo?= =?UTF-8?q?nfiguration.java=20=E4=BD=9C=E7=94=A8=E5=92=8C=20GlobalExceptio?= =?UTF-8?q?nHandler.java=20=E9=87=8D=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/GlobalExceptionConfiguration.java | 61 ------------------- .../handler/GlobalExceptionHandler.java | 2 + 2 files changed, 2 insertions(+), 61 deletions(-) delete mode 100644 pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/GlobalExceptionConfiguration.java diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/GlobalExceptionConfiguration.java b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/GlobalExceptionConfiguration.java deleted file mode 100644 index ace0bc44a..000000000 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/config/GlobalExceptionConfiguration.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.pig4cloud.pig.gateway.config; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.pig4cloud.pig.common.core.util.R; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.annotation.Order; -import org.springframework.core.io.buffer.DataBufferFactory; -import org.springframework.http.MediaType; -import org.springframework.http.server.reactive.ServerHttpResponse; -import org.springframework.web.server.ResponseStatusException; -import org.springframework.web.server.ServerWebExchange; -import org.springframework.web.server.handler.ResponseStatusExceptionHandler; -import reactor.core.publisher.Mono; - -/** - * @author lengleng - * @date 2023-07-30 - *

- * 网关异常通用处理器,只作用在webflux 环境下 , 优先级低于 {@link ResponseStatusExceptionHandler} 执行 - */ -@Slf4j -@Order(-1) -@Configuration -@RequiredArgsConstructor -public class GlobalExceptionConfiguration implements ErrorWebExceptionHandler { - - private final ObjectMapper objectMapper; - - @Override - public Mono handle(ServerWebExchange exchange, Throwable ex) { - ServerHttpResponse response = exchange.getResponse(); - - if (response.isCommitted()) { - return Mono.error(ex); - } - - // header set - response.getHeaders().setContentType(MediaType.APPLICATION_JSON); - if (ex instanceof ResponseStatusException) { - response.setStatusCode(((ResponseStatusException) ex).getStatusCode()); - } - - return response.writeWith(Mono.fromSupplier(() -> { - DataBufferFactory bufferFactory = response.bufferFactory(); - try { - log.error("Error rquest :{} Error Spring Cloud Gateway : {}", exchange.getRequest().getPath(), - ex.getMessage()); - return bufferFactory.wrap(objectMapper.writeValueAsBytes(R.failed(ex.getMessage()))); - } - catch (JsonProcessingException e) { - log.warn("Error writing response", ex); - return bufferFactory.wrap(new byte[0]); - } - })); - } - -} diff --git a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/GlobalExceptionHandler.java b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/GlobalExceptionHandler.java index 967ee46ec..2a7232e82 100644 --- a/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/GlobalExceptionHandler.java +++ b/pig-gateway/src/main/java/com/pig4cloud/pig/gateway/handler/GlobalExceptionHandler.java @@ -22,6 +22,7 @@ import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; +import org.springframework.context.annotation.Configuration; import org.springframework.core.annotation.Order; import org.springframework.core.io.buffer.DataBufferFactory; import org.springframework.http.MediaType; @@ -39,6 +40,7 @@ */ @Slf4j @Order(-1) +@Configuration @RequiredArgsConstructor public class GlobalExceptionHandler implements ErrorWebExceptionHandler {