自定义错误代码(枚举)
public enum ErrorCod {
FAIL_TO_IMPORT,
FAIL_TO_EXPORT,
FAIL_TO_INSERT,
FAIL_TO_UPDATE,
FAIL_TO_DELETE,
FAIL_TO_BATCH_OPERATE,
ERROR_PARAM,
UNHANDLED_EXCEPTION
}
自定义异常类
public class XxxException extends RuntimeException {
private ErrorCod errorCod;
public BizException(String errMsg,ErrorCod errorCod){
super(errMsg);
this.errorCod = errorCod;
}
public ErrorCod getErrorCod() {
return errorCod;
}
public void setErrorCod(ErrorCod errorCod) {
this.errorCod = errorCod;
}
}
定义全局异常处理器
/**
* 自定义异常处理器
*/
@RestController
@ControllerAdvice("com.xxx.yyy.controller")
public class RestExceptionHandler {
private static final Logger LOGGER = LoggerFactory.getLogger(RestExceptionHandler.class);
private static final String ERROR_CODE = "errCode";
private static final String ERROR_MESSAGE = "errMsg";
private static final String PROMPT_MESSAGE_1 = "发生未处理异常: ";
private static final String PROMPT_MESSAGE_2 = "发生异常原因为:";
private static final String PROMPT_MESSAGE_3 = "异常堆栈信息为:";
private static final String PROMPT_MESSAGE_4 = "未处理异常,请联系管理员:";
private String errMessage;
@ExceptionHandler(value = BizException.class)
public ResponseEntity<Map<String, Object>> bizExceptionHandler(BizException e) {
Map<String, Object> errResult = new HashMap<>();
errResult.put(ERROR_CODE, e.getErrorCod());
errResult.put(ERROR_MESSAGE, e.getMessage());
return new ResponseEntity<>(errResult, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(value = Exception.class)
public ResponseEntity<Map<String, Object>> commonExceptionHandler(Exception e) {
errMessage = PROMPT_MESSAGE_1 + e.getClass().getName();
LOGGER.error(errMessage);
errMessage = PROMPT_MESSAGE_2 + e.getMessage();
LOGGER.error(errMessage);
errMessage = PROMPT_MESSAGE_3 + Arrays.toString(e.getStackTrace()).replace(", ", ", \n");
LOGGER.error(errMessage);
Map<String, Object> errResult = new HashMap<>();
errResult.put(ERROR_CODE, ErrorCod.UNHANDLED_EXCEPTION);
errResult.put(ERROR_MESSAGE, PROMPT_MESSAGE_4);
return new ResponseEntity<>(errResult, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(value = SQLException.class)
public ResponseEntity<Map<String, Object>> sqlExceptionHandler(SQLException e) {
while (null != e.getNextException()) {
e = e.getNextException();
}
errMessage = PROMPT_MESSAGE_1 + e.getClass().getName();
LOGGER.error(errMessage);
errMessage = PROMPT_MESSAGE_2 + e.getMessage();
LOGGER.error(errMessage);
errMessage = PROMPT_MESSAGE_3 + Arrays.toString(e.getStackTrace()).replace(", ", ", \n");
LOGGER.error(errMessage);
Map<String, Object> errResult = new HashMap<>();
errResult.put(ERROR_CODE, ErrorCod.UNHANDLED_EXCEPTION);
errResult.put(ERROR_MESSAGE, PROMPT_MESSAGE_4);
return new ResponseEntity<>(errResult, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(value = InvocationTargetException.class)
public ResponseEntity<Map<String, Object>> reflectExceptionHandler(InvocationTargetException e) {
Throwable targetEx = e.getTargetException();
Throwable t1 = e;
if (null != targetEx) {
while (null != t1.getCause()) {
t1 = t1.getCause();
}
if (SQLException.class.isAssignableFrom(t1.getClass())) {
SQLException sqle = (SQLException) t1;
while (null != sqle.getNextException()) {
sqle = sqle.getNextException();
}
errMessage = PROMPT_MESSAGE_1 + e.getClass().getName();
LOGGER.error(errMessage);
errMessage = PROMPT_MESSAGE_2 + e.getMessage();
LOGGER.error(errMessage);
errMessage = PROMPT_MESSAGE_3 + Arrays.toString(e.getStackTrace()).replace(", ", ", \n");
LOGGER.error(errMessage);
} else {
errMessage = PROMPT_MESSAGE_1 + e.getClass().getName();
LOGGER.error(errMessage);
errMessage = PROMPT_MESSAGE_2 + e.getMessage();
LOGGER.error(errMessage);
errMessage = PROMPT_MESSAGE_3 + Arrays.toString(e.getStackTrace()).replace(", ", ", \n");
LOGGER.error(errMessage);
}
} else {
errMessage = PROMPT_MESSAGE_1 + e.getClass().getName();
LOGGER.error(errMessage);
errMessage = PROMPT_MESSAGE_2 + e.getMessage();
LOGGER.error(errMessage);
errMessage = PROMPT_MESSAGE_3 + Arrays.toString(e.getStackTrace()).replace(", ", ", \n");
LOGGER.error(errMessage);
}
Map<String, Object> errResult = new HashMap<>();
errResult.put(ERROR_CODE, ErrorCod.UNHANDLED_EXCEPTION);
errResult.put(ERROR_MESSAGE, PROMPT_MESSAGE_4);
return new ResponseEntity<>(errResult, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
像上文,poi那篇文章一样,有错误直接抛异常就可以了。每种解决方式都有优势、有弊端 没有最好的,只有最适合的,欢迎广大朋友提出优化,或更好的解决方案 大家共同进步