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

2022-12-11  本文已影响0人  学习微站
//自定义返回异常信息给前端 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; } }

本文使用 文章同步助手 同步

上一篇下一篇

猜你喜欢

热点阅读