调整异常输出
This commit is contained in:
parent
c594c0bc91
commit
1559eff981
@ -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));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user