|
@@ -1,53 +1,57 @@
|
|
|
-package com.fdkankan.gateway.exception;
|
|
|
-
|
|
|
-import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
-import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
-import com.fdkankan.common.response.ResultData;
|
|
|
-import lombok.RequiredArgsConstructor;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-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;
|
|
|
-
|
|
|
-@Slf4j
|
|
|
-@Order(-1)
|
|
|
-@Configuration
|
|
|
-@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
|
|
-public class GlobalErrorWebExceptionHandler implements ErrorWebExceptionHandler {
|
|
|
-
|
|
|
- private final ObjectMapper objectMapper;
|
|
|
-
|
|
|
- @Override
|
|
|
- public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
|
|
|
- ServerHttpResponse response = exchange.getResponse();
|
|
|
- if (response.isCommitted()) {
|
|
|
- return Mono.error(ex);
|
|
|
- }
|
|
|
-
|
|
|
- // 设置返回JSON
|
|
|
- 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(ResultData.fail(500,ex.getMessage())));
|
|
|
- }
|
|
|
- catch (JsonProcessingException e) {
|
|
|
- log.error("Error writing response", ex);
|
|
|
- return bufferFactory.wrap(new byte[0]);
|
|
|
- }
|
|
|
- }));
|
|
|
- }
|
|
|
-}
|
|
|
+//package com.fdkankan.gateway.exception;
|
|
|
+//
|
|
|
+//import com.fasterxml.jackson.core.JsonProcessingException;
|
|
|
+//import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+//import com.fdkankan.common.constant.ServerCode;
|
|
|
+//import com.fdkankan.common.response.ResultData;
|
|
|
+//import lombok.RequiredArgsConstructor;
|
|
|
+//import lombok.extern.slf4j.Slf4j;
|
|
|
+//import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+//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.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;
|
|
|
+//
|
|
|
+//@Slf4j
|
|
|
+//@Order(-1)
|
|
|
+////@Configuration
|
|
|
+//@RequiredArgsConstructor(onConstructor = @__(@Autowired))
|
|
|
+//public class GlobalErrorWebExceptionHandler implements ErrorWebExceptionHandler {
|
|
|
+//
|
|
|
+// private final ObjectMapper objectMapper;
|
|
|
+//
|
|
|
+// @Override
|
|
|
+// public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
|
|
|
+//
|
|
|
+// ServerHttpResponse response = exchange.getResponse();
|
|
|
+// if (response.isCommitted()) {
|
|
|
+// return Mono.error(ex);
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 设置返回JSON
|
|
|
+// 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 {
|
|
|
+// //返回响应结果
|
|
|
+// log.error(ex.getMessage());
|
|
|
+// return bufferFactory.wrap(objectMapper.writeValueAsBytes(ResultData.fail(ServerCode.SYSTEM_ERROR.code(), ServerCode.SYSTEM_ERROR.message())));
|
|
|
+// }
|
|
|
+// catch (JsonProcessingException e) {
|
|
|
+// log.error("Error writing response", ex);
|
|
|
+// return bufferFactory.wrap(new byte[0]);
|
|
|
+// }
|
|
|
+// }));
|
|
|
+// }
|
|
|
+//}
|