自定义返回异常信息给前端,或者直接用框架里面的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; } }

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

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容