0
点赞
收藏
分享

微信扫一扫

自定义返回异常信息给前端,或者直接用框架里面的RuntimeException


//自定义返回异常信息给前端
public class test2 {
//抛出异常 main
@Test
public void test3() {
//自定义返回异常信息给前端
throw new SaasNetworkException(SaasExceptionCode.BRAND_NOT_EXITS);
}

}

//1 SaasNetworkException
class SaasNetworkException extends AbstractLegoException {

public SaasNetworkException(IExceptionResult result) {
super(result);
}
}

//2 AbstractLegoException--->common服务
/**

* 自定义异常

@Data
public abstract class AbstractLegoException extends RuntimeException {

//异常状态码
private int code;

//返回用户错误信息
private String message;

public AbstractLegoException(IExceptionResult result) {
super(result.getMsg());
this.code = result.getCode();
this.message = result.getMsg();
}

public AbstractLegoException(IExceptionResult result, Throwable throwable) {
super(result.getMsg(), throwable);
this.code = result.getCode();
this.message = result.getMsg();
}
}
*/


//3 IExceptionResult--->common
/**
*
public interface IExceptionResult {
int getCode();

String getMsg();
}
*/
//4 enum SaasExceptionCode
enum SaasExceptionCode implements IExceptionResult {


BRAND_NOT_EXITS(1008, "品牌数据不存在"),

;

/**
* code
*/
private int code;
/**
* msg
*/
private String msg;

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

@Override
public int getCode() {
return code;
}

@Override
public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}
}

或者直接用框架里面的RuntimeException

if (min == null) {
throw new RuntimeException("输入参数错误, atv.min is null");
}


举报

相关推荐

0 条评论