|
@@ -33,6 +33,7 @@ public class CommonException {
|
|
|
private Logger logger = LoggerFactory.getLogger(CommonException.class);
|
|
|
private String defaultSystemError = "CORE.ERROR.001";
|
|
|
private String defaultParamsError = "CORE.PARAMS.001";
|
|
|
+ private String defaultNotFoundError = "CORE.NOT_FOUND";
|
|
|
private String defaultMessage = "系统异常";
|
|
|
@Autowired
|
|
|
private Environment environment;
|
|
@@ -42,10 +43,7 @@ public class CommonException {
|
|
|
@ResponseStatus(code = HttpStatus.INTERNAL_SERVER_ERROR)
|
|
|
public Object exception(Exception ex, WebRequest request) {
|
|
|
logger.error(ex.getMessage(), ex);
|
|
|
- Info info = new Info();
|
|
|
- info.setSuccess(false);
|
|
|
- info.setErrorCode(environment.getProperty("mvc.common.exception.code", defaultSystemError));
|
|
|
- info.setMessage(environment.getProperty("mvc.common.exception.message", defaultMessage));
|
|
|
+ Info info = getInfo(environment.getProperty("mvc.common.exception.code", defaultSystemError), environment.getProperty("mvc.common.exception.message", defaultMessage));
|
|
|
return info;
|
|
|
}
|
|
|
|
|
@@ -62,10 +60,7 @@ public class CommonException {
|
|
|
sb.append(objectError.getField() + " 字段值 " + objectError.getRejectedValue() + " - " + objectError.getDefaultMessage());
|
|
|
}
|
|
|
}
|
|
|
- Info info = new Info();
|
|
|
- info.setSuccess(false);
|
|
|
- info.setErrorCode(defaultParamsError);
|
|
|
- info.setMessage(sb.toString());
|
|
|
+ Info info = getInfo(defaultParamsError, sb.toString());
|
|
|
return info;
|
|
|
}
|
|
|
|
|
@@ -73,10 +68,7 @@ public class CommonException {
|
|
|
@ResponseBody
|
|
|
public Object coreException(CoreException ex) {
|
|
|
logger.debug(ex.getMessage(), ex);
|
|
|
- Info info = new Info();
|
|
|
- info.setSuccess(false);
|
|
|
- info.setErrorCode(ex.getErrorCode());
|
|
|
- info.setMessage(ex.getMessage());
|
|
|
+ Info info = getInfo(ex.getErrorCode(), ex.getMessage());
|
|
|
return info;
|
|
|
}
|
|
|
|
|
@@ -84,21 +76,20 @@ public class CommonException {
|
|
|
@ResponseBody
|
|
|
public Object businessException(BusinessException bx) {
|
|
|
logger.debug(bx.getMessage(), bx);
|
|
|
- Info info = new Info();
|
|
|
- info.setSuccess(false);
|
|
|
- info.setErrorCode(bx.getErrorCode());
|
|
|
- info.setMessage(bx.getMessage());
|
|
|
+ Info info = getInfo(bx.getErrorCode(), bx.getMessage());
|
|
|
return info;
|
|
|
}
|
|
|
|
|
|
+ private Info getInfo(String errorCode, String message) {
|
|
|
+ return Info.NO(errorCode, message);
|
|
|
+ }
|
|
|
+
|
|
|
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
|
@ResponseBody
|
|
|
@ResponseStatus(value = HttpStatus.NOT_FOUND)
|
|
|
- public Object notFondMehtod(Exception ex) {
|
|
|
- Info info = new Info();
|
|
|
- info.setSuccess(false);
|
|
|
- info.setErrorCode(defaultSystemError);
|
|
|
- info.setMessage("not fond");
|
|
|
+ public Object notFondMethod(Exception ex) {
|
|
|
+ // 将匹配不到方法的异常状态定位404。
|
|
|
+ Info info = getInfo(defaultNotFoundError, "not fond");
|
|
|
return info;
|
|
|
}
|
|
|
}
|