自定义异常类
自定义异常
package com.es.test.exception;
import com.es.test.util.ResponseCodeEnum;
/**
* 自定义异常(CustomException)
*
* @author arjun
* @date 2020/7/27 13:59
*/
public class CustomException extends RuntimeException {
private String code;
// 给子类用的方法
public CustomException(ResponseCodeEnum responseCodeEnum) {
this(responseCodeEnum.getMessage(), responseCodeEnum.getCode());
}
private CustomException(String message, String code) {
super(message);
this.code = code;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
系统异常
package com.es.test.exception;
/**
* 系统异常(SystemException)
*
* @author arjun
* @date 2020/7/27 13:59
*/
public class SystemException extends RuntimeException {
public SystemException(String msg){
super(msg);
}
public SystemException() {
super();
}
}
code
package com.es.test.util;
/**
* @author arjun
* @date 2020/7/27 13:59
*/
public enum ResponseCodeEnum {
PARAMS_FAILED("A0400", "用户请求参数错误"),
PARAMS_NULL("A0410", "请求必填参数为空"),
SYS_ERR("B0320", "系统资源访问异常"),
CACHE_OVERTIME("C0230", "缓存服务超时"),
TABLE_NOT_FIND("C0311", "表不存在"),
FORCED_TRANSFER_ERROR("B0312", "强转出错");
private String code;
private String message;
ResponseCodeEnum(String code, String message) {
this.code = code;
this.message = message;
}
public final String getCode() {
return this.code;
}
public final String getMessage() {
return this.message;
}
}
异常控制处理器
package com.es.test.exception;
import com.es.test.exception.CustomException;
import com.es.test.exception.SystemException;
import com.es.test.util.R;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.http.HttpStatus;
import org.springframework.jdbc.UncategorizedSQLException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
/**
* 异常控制处理器
*
* @author arjun
* @date 2020/7/27 13:59
*/
@Slf4j
public class ExceptionAdvice {
/**
* 捕捉自定义异常
* @return
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(CustomException.class)
public R handle(CustomException e) {
log.error(e.getMessage());
return R.failed(e.getMessage());
}
/**
* 捕捉系统异常
* @return
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(SystemException.class)
public R handle(SystemException e) {
log.error(e.getMessage(),e);
return new R(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), null);
}
/**
* 捕捉404异常
* @return
*/
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NoHandlerFoundException.class)
public R handle(NoHandlerFoundException e) {
log.error(e.getMessage(),e);
return new R(HttpStatus.NOT_FOUND.value(),HttpStatus.NOT_FOUND.getReasonPhrase(), null);
}
/**
* @param request
* @param ex
* @return
*/
/*@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public R globalException(HttpServletRequest request, Throwable ex) {
log.error(ex.toString()+ex.getMessage(),ex);
return new R(this.getStatus(request).value(),this.getStatus(request).getReasonPhrase() , null);
}*/
/**
* 获取状态码
* @param request
* @return
*/
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
return HttpStatus.valueOf(statusCode);
}
/**
* 捕捉系统异常
* @return
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(MyBatisSystemException.class)
public R handle(MyBatisSystemException e) {
log.error("全局异常信息 ex={}", e.getMessage(), e);
return new R(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), null);
}
@ExceptionHandler(UncategorizedSQLException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public R sqlExceptionHandler(UncategorizedSQLException e) {
log.error("数据库SQL执行异常 ex={}", e.getMessage(), e);
return R.failed("数据库操作异常");
}
}