提交 6d713222 作者: 方治民

feat: 优化 Result、Status、Code 对异常的快速处理实现

上级 ee83593b
/* (C) 2022 YiRing, Inc. */ /* (C) 2022 YiRing, Inc. */
package com.yiring.app.constant; package com.yiring.app.constant;
import com.yiring.app.exception.CodeException;
import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModel;
/** /**
...@@ -44,4 +45,18 @@ public enum Code { ...@@ -44,4 +45,18 @@ public enum Code {
public String reason() { public String reason() {
return this.reasonPhrase; return this.reasonPhrase;
} }
/**
* 业务代码异常
*/
public CodeException exception() {
return new CodeException(this);
}
/**
* 暴露异常
*/
public void expose() {
throw exception();
}
} }
...@@ -106,6 +106,26 @@ public class Result<T extends Serializable> implements Serializable { ...@@ -106,6 +106,26 @@ public class Result<T extends Serializable> implements Serializable {
} }
/** /**
* 返回默认的 400 错误响应
*
* @return Result
* @see Status#BAD_REQUEST
*/
public static <T extends Serializable> Result<T> no() {
return no(Status.BAD_REQUEST);
}
/**
* 返回默认的 400 错误响应
*
* @return Result
* @see Status#BAD_REQUEST
*/
public static <T extends Serializable> Result<T> no(String details) {
return no(Status.BAD_REQUEST, details);
}
/**
* 返回失败响应内容 * 返回失败响应内容
* *
* @return Result * @return Result
......
/* (C) 2021 YiRing, Inc. */ /* (C) 2021 YiRing, Inc. */
package com.yiring.common.core; package com.yiring.common.core;
import com.yiring.common.exception.FailStatusException;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
/** /**
...@@ -138,4 +139,21 @@ public enum Status { ...@@ -138,4 +139,21 @@ public enum Status {
public String toString() { public String toString() {
return this.value + " " + name(); return this.value + " " + name();
} }
/**
* 快速失败异常
*
* @param message 异常消息
*/
public FailStatusException exception(String message) {
return new FailStatusException(this, message);
}
/**
* 暴露异常
* @param message 异常消息
*/
public void expose(String message) {
throw exception(message);
}
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论