重构服务端异常体系 #124

pull/131/head
chen.ma 2 years ago
parent a1d6e8a723
commit 27739d458f

@ -23,6 +23,7 @@ import java.io.IOException;
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.
@ -92,7 +93,7 @@ public class JWTAuthorizationFilter extends BasicAuthenticationFilter {
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);

@ -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);
}

@ -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.
@ -11,37 +14,85 @@ import cn.hippo4j.common.web.exception.ServiceException;
*/
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);
}
}

@ -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;
}
}

@ -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();
}

@ -6,7 +6,7 @@ package cn.hippo4j.common.web.exception;
* @author chen.ma
* @date 2021/3/19 16:07
*/
public enum ErrorCodeEnum {
public enum ErrorCodeEnum implements ErrorCode {
/**
* UNKNOWN_ERROR
@ -66,10 +66,21 @@ public enum ErrorCodeEnum {
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 "登录时间过长, 请退出重新登录";
}
}
}

@ -1,6 +1,5 @@
package cn.hippo4j.common.web.exception;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
@ -9,38 +8,90 @@ import lombok.EqualsAndHashCode;
* @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() + "'" +
'}';
}

Loading…
Cancel
Save