2019-07-27 23:03:27 +08:00
|
|
|
package com.cm.common.advice;
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
|
import com.cm.common.enums.ErrorResultCodeEnum;
|
|
|
|
import com.cm.common.exception.*;
|
|
|
|
import com.cm.common.exception.base.SystemException;
|
|
|
|
import com.cm.common.result.ErrorResult;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.http.HttpStatus;
|
2020-06-05 12:42:31 +08:00
|
|
|
import org.springframework.http.MediaType;
|
2020-06-06 22:01:26 +08:00
|
|
|
import org.springframework.security.authentication.InsufficientAuthenticationException;
|
2019-07-27 23:03:27 +08:00
|
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
|
|
2020-06-05 12:42:31 +08:00
|
|
|
import javax.servlet.http.HttpServletRequest;
|
2019-07-27 23:03:27 +08:00
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.sql.SQLSyntaxErrorException;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ClassName: ResponseAdvice
|
|
|
|
* @Description: Response结果处理
|
|
|
|
* @Author: WangGeng
|
|
|
|
* @Date: 2019/2/26 5:14 PM
|
|
|
|
* @Version: 1.0
|
|
|
|
**/
|
|
|
|
@ControllerAdvice
|
|
|
|
public class ResponseAdvice {
|
|
|
|
|
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(ResponseAdvice.class);
|
|
|
|
|
|
|
|
@ResponseBody
|
2020-06-05 12:42:31 +08:00
|
|
|
@ExceptionHandler({SystemException.class, SQLSyntaxErrorException.class, Exception.class})
|
|
|
|
public void responseException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException {
|
2020-06-06 22:01:26 +08:00
|
|
|
if (e instanceof InsufficientAuthenticationException) {
|
|
|
|
throw new InsufficientAuthenticationException(e.getMessage());
|
|
|
|
}
|
2020-06-09 23:37:55 +08:00
|
|
|
if (e instanceof FileException) {
|
|
|
|
LOG.error(e.getMessage());
|
|
|
|
} else {
|
|
|
|
LOG.error(e.getMessage(), e);
|
|
|
|
}
|
2019-07-27 23:03:27 +08:00
|
|
|
ErrorResult result = new ErrorResult(ErrorResultCodeEnum.SYSTEM_ERROR.getValue(), "系统错误");
|
|
|
|
if (e instanceof SaveException) {
|
|
|
|
result.setCode(ErrorResultCodeEnum.SAVE_ERROR.getValue());
|
|
|
|
} else if (e instanceof RemoveException) {
|
|
|
|
result.setCode(ErrorResultCodeEnum.REMOVE_ERROR.getValue());
|
|
|
|
} else if (e instanceof UpdateException) {
|
|
|
|
result.setCode(ErrorResultCodeEnum.UPDATE_ERROR.getValue());
|
|
|
|
} else if (e instanceof SearchException) {
|
|
|
|
result.setCode(ErrorResultCodeEnum.QUERY_ERROR.getValue());
|
|
|
|
} else if (e instanceof ParamsException) {
|
|
|
|
result.setCode(ErrorResultCodeEnum.PARAMS_ERROR.getValue());
|
|
|
|
} else if (e instanceof FileException) {
|
|
|
|
result.setCode(ErrorResultCodeEnum.FILE_ERROR.getValue());
|
2019-08-27 11:20:57 +08:00
|
|
|
} else if (e instanceof AppDeviceException) {
|
|
|
|
result.setCode(ErrorResultCodeEnum.DEVICE_ERROR.getValue());
|
2019-09-19 23:17:15 +08:00
|
|
|
} else if (e instanceof AppVersionException) {
|
|
|
|
result.setCode(ErrorResultCodeEnum.DEVICE_VERSION_ERROR.getValue());
|
2019-08-27 11:20:57 +08:00
|
|
|
} else if (e instanceof AccessTokenException) {
|
2019-08-17 08:56:38 +08:00
|
|
|
response.setStatus(HttpStatus.UNAUTHORIZED.value());
|
2019-07-27 23:03:27 +08:00
|
|
|
}
|
|
|
|
// 自定义提示信息
|
|
|
|
if (e instanceof SystemException) {
|
|
|
|
result.setMsg(e.getMessage());
|
2020-06-05 12:42:31 +08:00
|
|
|
} 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();
|
2021-04-12 18:23:32 +08:00
|
|
|
if (contentType != null && (contentType.contains(MediaType.APPLICATION_JSON_VALUE) || contentType.contains(MediaType.APPLICATION_FORM_URLENCODED_VALUE))) {
|
2020-06-05 12:42:31 +08:00
|
|
|
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));
|
2019-07-27 23:03:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|