From 1559eff9813f548b5ee44a9f94ebfa49015a6e59 Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Fri, 5 Jun 2020 12:42:31 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=BC=82=E5=B8=B8=E8=BE=93?= =?UTF-8?q?=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/cm/common/advice/ResponseAdvice.java | 26 ++++++++++++++----- .../com/cm/common/result/ErrorResult.java | 16 ++++++++++-- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/cloud-common/src/main/java/com/cm/common/advice/ResponseAdvice.java b/cloud-common/src/main/java/com/cm/common/advice/ResponseAdvice.java index b0cda70..e40362d 100644 --- a/cloud-common/src/main/java/com/cm/common/advice/ResponseAdvice.java +++ b/cloud-common/src/main/java/com/cm/common/advice/ResponseAdvice.java @@ -1,6 +1,7 @@ package com.cm.common.advice; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.cm.common.enums.ErrorResultCodeEnum; import com.cm.common.exception.*; import com.cm.common.exception.base.SystemException; @@ -8,10 +9,12 @@ import com.cm.common.result.ErrorResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; +import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.sql.SQLSyntaxErrorException; @@ -30,11 +33,9 @@ public class ResponseAdvice { private static final Logger LOG = LoggerFactory.getLogger(ResponseAdvice.class); @ResponseBody - @ExceptionHandler({SystemException.class, SQLSyntaxErrorException.class}) - public void responseException(HttpServletResponse response, Exception e) throws IOException { + @ExceptionHandler({SystemException.class, SQLSyntaxErrorException.class, Exception.class}) + public void responseException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException { LOG.error(e.getMessage(), e); - response.setStatus(HttpStatus.BAD_REQUEST.value()); - response.setHeader("Content-Type", "application/json;charset=UTF-8"); ErrorResult result = new ErrorResult(ErrorResultCodeEnum.SYSTEM_ERROR.getValue(), "系统错误"); if (e instanceof SaveException) { result.setCode(ErrorResultCodeEnum.SAVE_ERROR.getValue()); @@ -58,9 +59,22 @@ public class ResponseAdvice { // 自定义提示信息 if (e instanceof SystemException) { result.setMsg(e.getMessage()); + } else { + StringBuilder errorMessageSB = new StringBuilder(); + for (StackTraceElement stackTraceElement : e.getStackTrace()) { + errorMessageSB.append(stackTraceElement.toString()).append("\n"); + } + result.setDetail(errorMessageSB.toString()); + result.setDetail(e.getMessage()); + } + String contentType = request.getContentType(); + if (contentType != null && contentType.contains(MediaType.APPLICATION_JSON_VALUE)) { + response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE); + response.setStatus(HttpStatus.BAD_REQUEST.value()); + response.getWriter().write(JSON.toJSONString(result)); + } else { + request.getSession().setAttribute("errorMessage", JSON.toJSONString(result)); } - response.getWriter().write(JSON.toJSONString(result)); } - } diff --git a/cloud-common/src/main/java/com/cm/common/result/ErrorResult.java b/cloud-common/src/main/java/com/cm/common/result/ErrorResult.java index 1ea7f7d..4a68912 100644 --- a/cloud-common/src/main/java/com/cm/common/result/ErrorResult.java +++ b/cloud-common/src/main/java/com/cm/common/result/ErrorResult.java @@ -19,6 +19,8 @@ public class ErrorResult implements Serializable { private Integer code; @ApiModelProperty(name = "msg", value = "错误信息", example = "查询失败") private String msg; + @ApiModelProperty(name = "detail", value = "错误说明") + private String detail; public ErrorResult() { } @@ -44,13 +46,23 @@ public class ErrorResult implements Serializable { this.msg = msg; } + public String getDetail() { + return detail == null ? "" : detail.trim(); + } + + public void setDetail(String detail) { + this.detail = detail; + } + @Override public String toString() { final StringBuilder sb = new StringBuilder("{"); sb.append("\"code\":") .append(code); - sb.append(",\"msg\":") - .append("\"").append(msg).append("\""); + sb.append(",\"msg\":\"") + .append(msg).append('\"'); + sb.append(",\"detail\":\"") + .append(detail).append('\"'); sb.append('}'); return sb.toString(); }