|
@@ -142,18 +142,19 @@ public class GlobalExceptionInterceptor extends AbstractErrorController {
|
|
/**
|
|
/**
|
|
* 将异常转换成结果对象
|
|
* 将异常转换成结果对象
|
|
*
|
|
*
|
|
- * @param e 异常对象
|
|
|
|
|
|
+ * @param request Http请求对象
|
|
|
|
+ * @param e 异常对象
|
|
* @return 结果对象
|
|
* @return 结果对象
|
|
*/
|
|
*/
|
|
- private Result<?> error2result(Throwable e) {
|
|
|
|
|
|
+ private Result<?> error2result(HttpServletRequest request, Throwable e) {
|
|
if (e instanceof CompletionException) {
|
|
if (e instanceof CompletionException) {
|
|
- return this.error2result(e.getCause());
|
|
|
|
|
|
+ return this.error2result(request, e.getCause());
|
|
} else if (e instanceof HttpRequestMethodNotSupportedException) {
|
|
} else if (e instanceof HttpRequestMethodNotSupportedException) {
|
|
- return Result.build(HttpStatus.METHOD_NOT_ALLOWED);
|
|
|
|
|
|
+ return Result.build(HttpStatus.METHOD_NOT_ALLOWED, null, e.getMessage());
|
|
} else if (e instanceof HttpMediaTypeNotAcceptableException) {
|
|
} else if (e instanceof HttpMediaTypeNotAcceptableException) {
|
|
- return Result.build(HttpStatus.NOT_ACCEPTABLE);
|
|
|
|
|
|
+ return Result.build(HttpStatus.NOT_ACCEPTABLE, null, e.getMessage());
|
|
} else if (e instanceof HttpMediaTypeNotSupportedException) {
|
|
} else if (e instanceof HttpMediaTypeNotSupportedException) {
|
|
- return Result.build(HttpStatus.UNSUPPORTED_MEDIA_TYPE);
|
|
|
|
|
|
+ return Result.build(HttpStatus.UNSUPPORTED_MEDIA_TYPE, null, e.getMessage());
|
|
} else if (e instanceof HttpMessageNotReadableException) {
|
|
} else if (e instanceof HttpMessageNotReadableException) {
|
|
Map<String, String> errors = this.format((HttpMessageNotReadableException) e);
|
|
Map<String, String> errors = this.format((HttpMessageNotReadableException) e);
|
|
return CollectionUtils.isEmpty(errors) ? Result.build(HttpStatus.BAD_REQUEST) :
|
|
return CollectionUtils.isEmpty(errors) ? Result.build(HttpStatus.BAD_REQUEST) :
|
|
@@ -183,16 +184,26 @@ public class GlobalExceptionInterceptor extends AbstractErrorController {
|
|
return Result.build(HttpStatus.PRECONDITION_FAILED, ImmutableMap.of(
|
|
return Result.build(HttpStatus.PRECONDITION_FAILED, ImmutableMap.of(
|
|
((MissingServletRequestPartException) e).getRequestPartName(), e.getMessage()
|
|
((MissingServletRequestPartException) e).getRequestPartName(), e.getMessage()
|
|
));
|
|
));
|
|
- } else if (e instanceof ValidationException) {
|
|
|
|
|
|
+ } else if (e instanceof ValidationException || e instanceof TypeMismatchException
|
|
|
|
+ || e instanceof MultipartException || e instanceof HttpMessageConversionException
|
|
|
|
+ || e instanceof ServletRequestBindingException) {
|
|
return Result.build(HttpStatus.BAD_REQUEST, null, e.getMessage());
|
|
return Result.build(HttpStatus.BAD_REQUEST, null, e.getMessage());
|
|
- } else if (e instanceof TypeMismatchException || e instanceof MultipartException
|
|
|
|
- || e instanceof HttpMessageConversionException || e instanceof ServletRequestBindingException) {
|
|
|
|
- return Result.build(HttpStatus.BAD_REQUEST);
|
|
|
|
- } else if (e instanceof FrameworkException) {
|
|
|
|
- FrameworkException exception = (FrameworkException) e;
|
|
|
|
- return Result.build(exception.getStatus(), exception.getData(), exception.getMessage());
|
|
|
|
}
|
|
}
|
|
- return Result.build(HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
|
|
|
+
|
|
|
|
+ // 未知异常处理
|
|
|
|
+ Object data = null;
|
|
|
|
+ String message = e.getMessage();
|
|
|
|
+ HttpStatus status = HttpStatus.INTERNAL_SERVER_ERROR;
|
|
|
|
+ if (e instanceof FrameworkException) {
|
|
|
|
+ data = ((FrameworkException) e).getData();
|
|
|
|
+ status = ((FrameworkException) e).getStatus();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 如果是5xx异常且需要返回包装类型则屏蔽详细的异常信息
|
|
|
|
+ if (status.is5xxServerError() && SessionContextHolder.isResponseWrappingHeader(request)) {
|
|
|
|
+ message = status.getReasonPhrase();
|
|
|
|
+ }
|
|
|
|
+ return Result.build(status, data, message);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -222,16 +233,19 @@ public class GlobalExceptionInterceptor extends AbstractErrorController {
|
|
/**
|
|
/**
|
|
* 异常方法
|
|
* 异常方法
|
|
*
|
|
*
|
|
- * @param request Http请求对象
|
|
|
|
|
|
+ * @param request Http请求对象
|
|
|
|
+ * @param response Http响应对象
|
|
* @return 异常结果
|
|
* @return 异常结果
|
|
*/
|
|
*/
|
|
@RequestMapping("${server.error.path:${error.path:/error}}")
|
|
@RequestMapping("${server.error.path:${error.path:/error}}")
|
|
- public Result<?> error(HttpServletRequest request) {
|
|
|
|
|
|
+ public Result<?> error(HttpServletRequest request, HttpServletResponse response) {
|
|
HttpStatus status = this.getStatus(request);
|
|
HttpStatus status = this.getStatus(request);
|
|
Map<String, Object> attributes = this.getErrorAttributes(request, this.options);
|
|
Map<String, Object> attributes = this.getErrorAttributes(request, this.options);
|
|
String uri = (String) attributes.get("path");
|
|
String uri = (String) attributes.get("path");
|
|
String message = (String) attributes.get("message");
|
|
String message = (String) attributes.get("message");
|
|
log.warn(SessionContextHolder.getLoggingMessage(uri, request.getMethod(), status.value(), message));
|
|
log.warn(SessionContextHolder.getLoggingMessage(uri, request.getMethod(), status.value(), message));
|
|
|
|
+ response.setStatus(status.value());
|
|
|
|
+ SessionContextHolder.initializeResponseWrappingHeader(response);
|
|
return Result.build(status, null, message);
|
|
return Result.build(status, null, message);
|
|
}
|
|
}
|
|
|
|
|
|
@@ -245,7 +259,7 @@ public class GlobalExceptionInterceptor extends AbstractErrorController {
|
|
*/
|
|
*/
|
|
@ExceptionHandler(Exception.class)
|
|
@ExceptionHandler(Exception.class)
|
|
public Result<?> exception(HttpServletRequest request, HttpServletResponse response, Exception exception) {
|
|
public Result<?> exception(HttpServletRequest request, HttpServletResponse response, Exception exception) {
|
|
- Result<?> result = this.error2result(exception);
|
|
|
|
|
|
+ Result<?> result = this.error2result(request, exception);
|
|
HttpStatus status = HttpUtils.getStatus(result.getCode());
|
|
HttpStatus status = HttpUtils.getStatus(result.getCode());
|
|
this.log(request, status, exception);
|
|
this.log(request, status, exception);
|
|
response.setStatus(status.value());
|
|
response.setStatus(status.value());
|