Java 封裝全域性異常類
今天學習Java第五天,寫demo的時候捕獲異常,很不習慣,畢竟PHP可以很方便直觀的處理,查查資料,自己寫了一個,剛開始學,寫的有點LOW
1.全域性異常處理類
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cg.xcx.common.base.JsonReturn;
import java.util.ArrayList;
import java.util.List;
/**
* ClassName:全域性異常處理
*/
@ControllerAdvice
public class GlobalExceptionHandler {
@Autowired
private Environment environment;
@ExceptionHandler(value = Exception.class)
@ResponseBody
public JsonReturn defaultErrorHandler(HttpServletRequest request, Exception ex)
throws Exception {
String errorCode = null;
String errorMsg = null;
Object errorData = new ArrayList();
String active = environment.getProperty("spring.profiles.active");
if (ex instanceof BusinessException) {
BusinessException se = (BusinessException) ex;
errorCode = se.getCode();
errorMsg = se.getMessage();
errorData = se.getData();
// 遮蔽線上異常
} else if(active == "prod"){
errorCode = "0";
errorMsg = "內部服務錯誤";
} else {
ex.printStackTrace();
}
return new JsonReturn(errorCode,errorMsg, errorData);
}
}
2.建立一個異常檔案 exception
定義一個繼承runtime異常的類,BaseRuntimeException
import org.springframework.util.StringUtils;
import java.util.List;
public class BaseRuntimeException extends RuntimeException {
private Object errorData;
private Throwable cause = null;
private String errorCode = null;
private String errorMsg = ""; // 客戶描述
private String alarmMsg = "[email protected]#$"; // 告警描述
static final long serialVersionUID = 0L;
public BaseRuntimeException() {
}
public BaseRuntimeException(String errorCode) {
this.errorCode = errorCode;
}
/**
* 透傳其他模組的錯誤資訊 Creates a new instance of BaseRuntimeException.
*
* @param errorCode
* @param msg
*/
public BaseRuntimeException(String errorCode, String msg) {
super(msg);
this.errorMsg = msg;
this.errorCode = errorCode;
System.out.println(this.errorCode);
System.out.println(this.errorMsg);
}
/**
* 透傳其他模組的錯誤資訊並告警 Creates a new instance of BaseRuntimeException.
*
* @param errorCode
* @param msg
*/
public BaseRuntimeException(String errorCode, String msg, String alarmMsg) {
super(msg);
this.errorMsg = msg;
this.alarmMsg = alarmMsg;
this.errorCode = errorCode;
}
public BaseRuntimeException(Throwable cause) {
super(cause);
}
public BaseRuntimeException(String errorCode, String msg, Throwable cause) {
super(msg);
this.cause = cause;
this.errorCode = errorCode;
}
public BaseRuntimeException(String errorCode, String msg, Object data) {
super(msg);
this.errorData = data;
this.errorCode = errorCode;
}
@Override
public Throwable getCause() {
return this.cause;
}
public String getExceptionMessage() {
if (super.getMessage() != null) {
return super.getMessage();
}
if (this.cause != null) {
return this.cause.toString();
}
return null;
}
public String getErrorCode() {
return this.errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String getAlarmMsg() {
return alarmMsg;
}
public void setAlarmMsg(String alarmMsg) {
this.alarmMsg = alarmMsg;
}
public boolean isAlarm() {
if ((!StringUtils.isEmpty(this.alarmMsg)) && (!this.alarmMsg.equals("[email protected]#$"))) {
return true;
}
return false;
}
}
3.定義全域性返回
import java.util.ArrayList;
/**
* 全域性錯誤碼
* Created by LY on 2018/04/11.
*/
public enum GlobalErrorCode{
// Login error
LOGIN_ERROR_TOKEN_INVALID("-10000", "無效的token.", new ArrayList<>()),
/**
* 引數類錯誤碼
*/
REQUEST_PACKET_ERROR("400010","請求資料錯誤!"),
TCP_SYSTEM_CONNECT_ERROR("400012","連線資料伺服器失敗!"),
REQUEST_PARSE_PACKET_ERROR("400015","伺服器處理資料失敗!"),
REQUEST_DB_ADD_ERROR("30001","資料庫新增處理失敗!"),
REQUEST_DB_SAVE_ERROR("30002","資料庫修改處理失敗!"),
REQ_PARAM_TOKEN_NOTNULL("400014","token引數為空!"),
REQ_PARAM_TTL_INVALID("-10000","登陸超時!");
GlobalErrorCode(String code,String message){
this.code = code;
this.message = message;
}
GlobalErrorCode(String code,String message, Object data){
this.code = code;
this.message = message;
this.data = data;
}
private String code;
private String message;
private Object data;
public String getCode() {
return this.code;
}
public String getMessage() {
return this.message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
4.錯誤捕捉
import java.util.List;
public class BusinessException extends BaseRuntimeException {
private static final long serialVersionUID = 1L;
//自定義錯誤碼
private String code;
private Object data;
//自定義構造器,只保留一個,讓其必須輸入錯誤碼及內容
public BusinessException(String code, String msg) {
super(code, msg);
this.code = code;
}
public BusinessException(String code, String msg, Object data) {
super(code, msg, data);
this.code = code;
this.data = data;
}
public BusinessException(GlobalErrorCode globalErrorCode) {
super(globalErrorCode.getCode(), globalErrorCode.getMessage(), globalErrorCode.getData());
this.code = globalErrorCode.getCode();
this.data = globalErrorCode.getData();
}
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
基本以上四個可以拿去直接使用