统一异常/服务处理
java开发中的异常可以通过切面@RestControllerAdvice统一进行处理返回。
但为了包装统一服务调用和统一异常可以包装一波:
一、包装异常类
@EqualsAndHashCode(callSuper = true) @Data public class MyException extends RuntimeException { private Integer code; private String message; public MyException() { super(CommonResultEnum.FAIL.desc()); this.code = CommonResultEnum.FAIL.code(); this.message = CommonResultEnum.FAIL.desc(); } public MyException(ResultativeEnum result) { super(result.desc()); this.code = result.code(); this.message = result.desc(); } public MyException(ResultativeEnum result, String message) { super(message); this.code = result.code(); this.message = result.desc(); } public MyException(ResultativeEnum result, String format, String... args) { super(MessageFormatter.arrayFormat(format, args).getMessage()); this.code = result.code(); this.message = super.getMessage(); } public MyException(Throwable cause) { super(cause); this.message = this.getMessage(); } public MyException(ResultativeEnum result, Throwable cause) { super(result.desc(), cause); this.code = result.code(); this.message = result.desc(); } public MyException(Integer code) { this.code = code; } public MyException(String message, Integer code) { super(message); this.code = code; } public MyException(String message, Throwable cause, Integer code) { super(message, cause); this.code = code; } public MyException(Throwable cause, Integer code) { super(cause); this.code = code; } public MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, Integer code) { super(message, cause, enableSuppression, writableStackTrace); this.code = code; } }
@Data public class MyException extends RuntimeException { private Integer code; private String message; public MyException() { super(CommonResultEnum.FAIL.desc()); this.code = CommonResultEnum.FAIL.code(); this.message = CommonResultEnum.FAIL.desc(); } public MyException(ResultativeEnum result) { super(result.desc()); this.code = result.code(); this.message = result.desc(); } public MyException(ResultativeEnum result, String message) { super(message); this.code = result.code(); this.message = result.desc(); } public MyException(ResultativeEnum result, String format, String... args) { super(MessageFormatter.arrayFormat(format, args).getMessage()); this.code = result.code(); this.message = super.getMessage(); } public MyException(Throwable cause) { super(cause); this.message = this.getMessage(); } public MyException(ResultativeEnum result, Throwable cause) { super(result.desc(), cause); this.code = result.code(); this.message = result.desc(); } public MyException(Integer code) { this.code = code; } public MyException(String message, Integer code) { super(message); this.code = code; } public MyException(String message, Throwable cause, Integer code) { super(message, cause); this.code = code; } public MyException(Throwable cause, Integer code) { super(cause); this.code = code; } public MyException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace, Integer code) { super(message, cause, enableSuppression, writableStackTrace); this.code = code; }
二、封装统一处理类
@Slf4j public class MyHandler<T, R> { public Response<R> handle(Function<T, R> func, T t) { try { R result = func.apply(t); return Response.success(result).build(); //统一服务处理 } catch (MyException ex) { log.error("fail to handle ex", ex); return Response.<R>fail(null, ex.getCode(), ex.getMessage()).build(); //统一异常处理 } catch (Exception e) { log.error("cause un handle exception", e); return Response.<R>fail(null, 500, "服务异常").build(); } } }
三、调用统一处理
public Response<Page<ApiQueryRespVO>> queryApi(@RequestBody @Validated ApiQueryDTO request) { MyHandler<ApiQueryDTO, Page<ApiQueryRespVO>> exHandler = new MyHandler<>(); return exHandler.handle(apiService::queryApis, request);
以上使用了函数式编程,将服务进行统一处理,异常进行统一处理。
1、将通过毅力完成的事转化为习惯。
2、清心寡欲、方能高枕无忧。
3、纸上得来终觉浅,绝知此事要躬行。
2、清心寡欲、方能高枕无忧。
3、纸上得来终觉浅,绝知此事要躬行。
种一棵树最好的时间是 十年前。 其次是, 现在!