Skip to content

Commit

Permalink
重构服务端异常体系 #124
Browse files Browse the repository at this point in the history
  • Loading branch information
magestacks committed Mar 2, 2022
1 parent a1d6e8a commit 27739d4
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Collections;

import static cn.hippo4j.common.constant.Constants.ACCESS_TOKEN;
import static cn.hippo4j.common.web.exception.ErrorCodeEnum.LOGIN_TIMEOUT;

/**
* JWT authorization filter.
Expand Down Expand Up @@ -92,7 +93,7 @@ private UsernamePasswordAuthenticationToken getAuthentication(String tokenHeader
String token = tokenHeader.replace(JwtTokenUtil.TOKEN_PREFIX, "");
boolean expiration = JwtTokenUtil.isExpiration(token);
if (expiration) {
throw new ServiceException("登录时间过长,请退出重新登录");
throw new ServiceException(LOGIN_TIMEOUT);
}

String username = JwtTokenUtil.getUsername(token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,31 @@ public class Result<T> implements Serializable {

private static final long serialVersionUID = -4408341719434417427L;

/**
* Correct return code.
*/
public static final String SUCCESS_CODE = "0";

/**
* Return code.
*/
private String code;

/**
* Message.
*/
private String message;

/**
* Response data.
*/
private T data;

/**
* Is success.
*
* @return
*/
public boolean isSuccess() {
return SUCCESS_CODE.equals(code);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package cn.hippo4j.common.web.base;

import cn.hippo4j.common.web.exception.AbstractException;
import cn.hippo4j.common.web.exception.ErrorCode;
import cn.hippo4j.common.web.exception.ErrorCodeEnum;
import cn.hippo4j.common.web.exception.ServiceException;

import java.util.Optional;

/**
* Results.
Expand All @@ -11,37 +14,85 @@
*/
public final class Results {

/**
* 成功.
*
* @return
*/
public static Result<Void> success() {
return new Result<Void>()
.setCode(Result.SUCCESS_CODE);
}

/**
* 成功.
*
* @param data 并设置 DATA 参数
* @param <T> 对应 DATA 字段的数据类型
* @return
*/
public static <T> Result<T> success(T data) {
return new Result<T>()
.setCode(Result.SUCCESS_CODE)
.setData(data);
}

public static <T> Result<T> failure(ServiceException serviceException) {
return new Result<T>().setCode(ErrorCodeEnum.SERVICE_ERROR.getCode())
.setMessage(serviceException.getMessage());
/**
* 返回失败信息.
*
* @return
*/
public static Result<Void> failure() {
return failure(ErrorCodeEnum.SERVICE_ERROR.getCode(), ErrorCodeEnum.SERVICE_ERROR.getMessage());
}

/**
* 框架定义抽象异常拦截.
*
* @param abstractException 框架自定义抽象异常
* @return
*/
protected static Result<Void> failure(AbstractException abstractException) {
String errorCode = Optional.ofNullable(abstractException.getErrorCode())
.map(ErrorCode::getCode)
.orElse(ErrorCodeEnum.SERVICE_ERROR.getCode());

return new Result<Void>().setCode(errorCode)
.setMessage(abstractException.getMessage());
}

/**
* 未知异常.
*
* @param throwable 未知异常
* @return
*/
public static Result<Void> failure(Throwable throwable) {
return new Result<Void>().setCode(ErrorCodeEnum.SERVICE_ERROR.getCode())
.setMessage(throwable.getMessage());
}

public static <T> Result<T> failure(String code, String message) {
return new Result<T>()
.setCode(code)
.setMessage(message);
/**
* 返回失败信息.
*
* @param errorCode 错误编码、错误信息
* @return
*/
public static Result<Void> failure(ErrorCode errorCode) {
return failure(errorCode.getCode(), errorCode.getMessage());
}

public static <T> Result<T> failure(ErrorCodeEnum errorCode) {
return new Result<T>()
.setCode(errorCode.getCode())
.setMessage(errorCode.getMessage());
/**
* 返回失败信息.
*
* @param code 错误编码.
* @param message 错误信息.
* @return
*/
public static Result<Void> failure(String code, String message) {
return new Result<Void>()
.setCode(code)
.setMessage(message);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.hippo4j.common.web.exception;

import lombok.Getter;

/**
* Abstract exception.
*
* @author chen.ma
* @date 2022/3/2 20:01
*/
public class AbstractException extends RuntimeException {

@Getter
public final ErrorCode errorCode;

public AbstractException(String message, Throwable throwable, ErrorCode errorCode) {
super(message, throwable);
this.errorCode = errorCode;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cn.hippo4j.common.web.exception;

/**
* 错误码抽象接口.
*
* @author chen.ma
* @date 2021/9/16 15:39
*/
public interface ErrorCode {

/**
* 错误码.
*
* @return
*/
String getCode();

/**
* 错误信息.
*
* @return
*/
String getMessage();

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* @author chen.ma
* @date 2021/3/19 16:07
*/
public enum ErrorCodeEnum {
public enum ErrorCodeEnum implements ErrorCode {

/**
* UNKNOWN_ERROR
Expand Down Expand Up @@ -66,10 +66,21 @@ public String getCode() {
public String getMessage() {
return "NOT_FOUND";
}
};
},

public abstract String getCode();
/**
* LOGIN_TIMEOUT
*/
LOGIN_TIMEOUT {
@Override
public String getCode() {
return "A000004";
}

public abstract String getMessage();
@Override
public String getMessage() {
return "登录时间过长, 请退出重新登录";
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.hippo4j.common.web.exception;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
Expand All @@ -9,38 +8,90 @@
* @author chen.ma
* @date 2021/3/19 16:14
*/
@Data
@EqualsAndHashCode(callSuper = true)
public class ServiceException extends RuntimeException {
public class ServiceException extends AbstractException {

private static final long serialVersionUID = -8667394300356618037L;
/**
* 无指定错误码.
*/
public ServiceException() {
this(ErrorCodeEnum.SERVICE_ERROR);
}

private String detail;
/**
* 指定错误码.
*
* @param errorCode
*/
public ServiceException(ErrorCode errorCode) {
this(errorCode.getMessage(), null, errorCode);
}

/**
* 指定报错信息.
*
* @param message
*/
public ServiceException(String message) {
super(message);
this(message, null, ErrorCodeEnum.SERVICE_ERROR);
}

public ServiceException(String message, String detail) {
super(message);
this.detail = detail;
/**
* 异常抛出.
*
* @param cause
*/
public ServiceException(Throwable cause) {
this(cause, cause.getMessage());
}

/**
* 异常抛出并指定错误信息.
*
* @param message
* @param cause
*/
public ServiceException(String message, Throwable cause) {
super(message, cause);
this.detail = cause.getMessage();
this(message, cause, ErrorCodeEnum.SERVICE_ERROR);
}

/**
* 异常抛出并指定错误信息.
*
* @param cause
* @param message
*/
public ServiceException(Throwable cause, String message) {
this(message, cause, ErrorCodeEnum.SERVICE_ERROR);
}

/**
* 异常抛出并指定错误码.
*
* @param cause
* @param errorCode
*/
public ServiceException(Throwable cause, ErrorCode errorCode) {
this(errorCode.getMessage(), cause, errorCode);
}

public ServiceException(String message, String detail, Throwable cause) {
super(message, cause);
this.detail = detail;
/**
* 报错信息与错误码同时存在, 并且不一致.
* <p> 以报错信息为主, 进行打印.
*
* @param message
* @param cause
* @param errorCode
*/
public ServiceException(String message, Throwable cause, ErrorCode errorCode) {
super(message, cause, errorCode);
}

@Override
public String toString() {
return "ServiceException{" +
"message='" + getMessage() + "'," +
"detail='" + getDetail() + "'" +
"code='" + errorCode.getCode() + "'," +
"message='" + errorCode.getMessage() + "'" +
'}';
}

Expand Down

0 comments on commit 27739d4

Please sign in to comment.