自定义异常

public class AobpException extends Exception implements ExceptionConstant {
private int code;

private String msg;

public AobpException(int code, String msg) {
this.code = code;
this.msg = msg;
}

public int getCode() {
return code;
}


public String getMsg() {
return msg;
}


public static AobpException notFoundException() {
return new AobpException(NOT_FOUND, NOT_FOUND_MSG);
}

public static AobpException notFoundException(String msg) {
return new AobpException(NOT_FOUND, msg);
}

public static AobpException paramsInvalidException() {
return new AobpException(PARAMS_INVALID, PARAMS_INVALID_MSG);
}

public static AobpException paramsInvalidException(String msg) {
return new AobpException(PARAMS_INVALID, msg);
}

public static AobpException userNotLoginException() {
return new AobpException(UNAUTHORIZED, USER_NOT_LOGIN_MSG);
}

public static AobpException noPermissionException() {
return new AobpException(NOT_FOUND, PERMISSION_DENIED_MSG);
}

public static AobpException userAlreadyExist() {
return new AobpException(FORBIDDEN, USER_EXIST_MSG);
}

}
public interface ResultCode {
int SUCCESS = 200;
int BAD_REQUEST = 400;
int UNAUTHORIZED = 401;
int FORBIDDEN = 403;
int NOT_FOUND = 404;
int METHOD_ALLOWED = 405;
int PARAMS_INVALID = 412;
int INTERNAL_ERR = 500;
}
public interface ExceptionConstant extends ResultCode {
String NOT_FOUND_MSG = "Not Found";
String PARAMS_INVALID_MSG = "Params Invalid";
String USER_NOT_LOGIN_MSG = "User Not Login";
String USER_EXIST_MSG = "User Already Exist";
String PERMISSION_DENIED_MSG = "Permission Denied";
String UNKNOWN_ERROR = "Server failed";

String SYSTEM_EXCEPTION_MESSAGE= "系统异常,请联系管路员";
}

public interface StatusConstant extends ResultCode {
String STATUS_OK = "OK";
String STATUS_NOT_FOUND = "Not Found";
String STATUS_PARAMS_INVALID = "Params Invalid";
String STATUS_INTERNAL_ERR = "Server Internal Error";
}

public class Status implements StatusConstant {
private int code;
private String statusMsg;

public Status() {

}

public Status(int code, String statusMsg) {
this.code = code;
this.statusMsg = statusMsg;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public String getStatusMsg() {
return statusMsg;
}

public void setStatusMsg(String statusMsg) {
this.statusMsg = statusMsg;
}

public static Status ok() {
return new Status(SUCCESS, STATUS_OK);
}

public static Status ok(String msg) {
return new Status(SUCCESS, msg);
}

public static Status notFound() {
return new Status(NOT_FOUND, STATUS_NOT_FOUND);
}

public static Status paramsInvalid() {
return new Status(PARAMS_INVALID, STATUS_PARAMS_INVALID);
}

public static Status error() {
return new Status(INTERNAL_ERR, STATUS_INTERNAL_ERR);
}

public static Status error(String msg) {
return new Status(INTERNAL_ERR, msg);
}
}


posted @ 2019-03-20 20:05  GavinYao  阅读(393)  评论(0)    收藏  举报