From 0586f3958ee7e7b2bf9be90911b3eb3c8b8494b2 Mon Sep 17 00:00:00 2001 From: pig666 Date: Sun, 24 May 2020 19:43:34 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20Introducing=20new=20features.=20#1?= =?UTF-8?q?279=20Spring=20Cloud=20Gateway=20=E5=85=A8=E5=B1=80=E9=80=9A?= =?UTF-8?q?=E7=94=A8=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GlobalExceptionConfiguration.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/GlobalExceptionConfiguration.java diff --git a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/GlobalExceptionConfiguration.java b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/GlobalExceptionConfiguration.java new file mode 100644 index 000000000..cf154bfa0 --- /dev/null +++ b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/GlobalExceptionConfiguration.java @@ -0,0 +1,57 @@ +package com.pig4cloud.pigx.common.gateway.configuration; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.pig4cloud.pigx.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 reactor.core.publisher.Mono; + +/** + * @author lengleng + * @date 2020/5/23 + *

+ * 网关异常通用处理器,只作用在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).getStatus()); + } + + return response + .writeWith(Mono.fromSupplier(() -> { + DataBufferFactory bufferFactory = response.bufferFactory(); + try { + return bufferFactory.wrap(objectMapper.writeValueAsBytes(R.failed(ex.getMessage()))); + } catch (JsonProcessingException e) { + log.warn("Error writing response", ex); + return bufferFactory.wrap(new byte[0]); + } + })); + } + +}