|
@@ -1,14 +1,16 @@
|
|
|
package cn.itlym.shoulder.platform.gateway.config;
|
|
|
|
|
|
import cn.itlym.shoulder.platform.gateway.ex.ShoulderGatewayException;
|
|
|
-import lombok.extern.shoulder.SLog;
|
|
|
import org.shoulder.core.dto.response.BaseResult;
|
|
|
import org.shoulder.core.exception.ErrorCode;
|
|
|
+import org.shoulder.core.log.AppLoggers;
|
|
|
+import org.shoulder.core.log.Logger;
|
|
|
import org.springframework.beans.factory.ObjectProvider;
|
|
|
-import org.springframework.boot.autoconfigure.web.ResourceProperties;
|
|
|
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
|
|
+import org.springframework.boot.autoconfigure.web.WebProperties;
|
|
|
import org.springframework.boot.autoconfigure.web.reactive.error.DefaultErrorWebExceptionHandler;
|
|
|
import org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration;
|
|
|
+import org.springframework.boot.web.error.ErrorAttributeOptions;
|
|
|
import org.springframework.boot.web.reactive.error.ErrorAttributes;
|
|
|
import org.springframework.context.ApplicationContext;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
@@ -18,8 +20,13 @@ import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.codec.ServerCodecConfigurer;
|
|
|
import org.springframework.lang.NonNull;
|
|
|
+import org.springframework.web.ErrorResponseException;
|
|
|
import org.springframework.web.reactive.function.BodyInserters;
|
|
|
-import org.springframework.web.reactive.function.server.*;
|
|
|
+import org.springframework.web.reactive.function.server.RequestPredicates;
|
|
|
+import org.springframework.web.reactive.function.server.RouterFunction;
|
|
|
+import org.springframework.web.reactive.function.server.RouterFunctions;
|
|
|
+import org.springframework.web.reactive.function.server.ServerRequest;
|
|
|
+import org.springframework.web.reactive.function.server.ServerResponse;
|
|
|
import org.springframework.web.reactive.result.view.ViewResolver;
|
|
|
import org.springframework.web.server.ResponseStatusException;
|
|
|
import reactor.core.publisher.Mono;
|
|
@@ -33,13 +40,14 @@ import java.util.stream.Collectors;
|
|
|
*
|
|
|
* @author lym
|
|
|
*/
|
|
|
-@SLog
|
|
|
@Configuration(
|
|
|
proxyBeanMethods = false
|
|
|
)
|
|
|
@Order(Ordered.HIGHEST_PRECEDENCE)
|
|
|
public class JsonExceptionHandler extends DefaultErrorWebExceptionHandler {
|
|
|
|
|
|
+ private static final Logger log = AppLoggers.APP_ERROR;
|
|
|
+
|
|
|
private static final String ATTRIBUTE_NAME_HTTP_STATUS = "httpStatus";
|
|
|
|
|
|
private static final String ATTRIBUTE_NAME_RESPONSE = "shoulderResponseBody";
|
|
@@ -52,7 +60,7 @@ public class JsonExceptionHandler extends DefaultErrorWebExceptionHandler {
|
|
|
/**
|
|
|
* @see ErrorWebFluxAutoConfiguration#errorWebExceptionHandler
|
|
|
*/
|
|
|
- public JsonExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties,
|
|
|
+ public JsonExceptionHandler(ErrorAttributes errorAttributes, WebProperties.Resources resourceProperties,
|
|
|
ServerProperties serverProperties, ApplicationContext applicationContext,
|
|
|
ObjectProvider<ViewResolver> viewResolvers,
|
|
|
ServerCodecConfigurer serverCodecConfigurer) {
|
|
@@ -82,12 +90,12 @@ public class JsonExceptionHandler extends DefaultErrorWebExceptionHandler {
|
|
|
* 为了安全,不打印堆栈信息,即使使用者手动开启
|
|
|
*/
|
|
|
@Override
|
|
|
- protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
|
|
|
+ protected Map<String, Object> getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) {
|
|
|
int httpStatus = DEFAULT_SERVER_ERROR_HTTP_STATUS;
|
|
|
Throwable error = super.getError(request);
|
|
|
- if (error instanceof ResponseStatusException) {
|
|
|
+ if (error instanceof ErrorResponseException) {
|
|
|
// spring 定义的 http 异常
|
|
|
- httpStatus = ((ResponseStatusException) error).getStatus().value();
|
|
|
+ httpStatus = ((ResponseStatusException) error).getStatusCode().value();
|
|
|
} else if (error instanceof ErrorCode) {
|
|
|
// shoulder 定义的异常
|
|
|
ErrorCode errorCode = (ErrorCode) error;
|
|
@@ -116,8 +124,8 @@ public class JsonExceptionHandler extends DefaultErrorWebExceptionHandler {
|
|
|
@NonNull
|
|
|
@Override
|
|
|
protected Mono<ServerResponse> renderErrorResponse(ServerRequest request) {
|
|
|
- boolean includeStackTrace = isIncludeStackTrace(request, MediaType.ALL);
|
|
|
- Map<String, Object> error = getErrorAttributes(request, includeStackTrace);
|
|
|
+ ErrorAttributeOptions errorAttributeOptions = getErrorAttributeOptions(request, MediaType.ALL);
|
|
|
+ Map<String, Object> error = getErrorAttributes(request, errorAttributeOptions);
|
|
|
return ServerResponse.status(getHttpStatus(error)).contentType(MediaType.APPLICATION_JSON)
|
|
|
.body(BodyInserters.fromValue(error));
|
|
|
}
|