统一异常处理
# 基于magic-api实现
通过覆盖ResultProvider
的buildException
实现全局异常处理。
@Component
public class MyResultProvider implements ResultProvider {
@Override
public Object buildResult(RequestEntity requestEntity, int code, String message, Object data) {
long timestamp = System.currentTimeMillis();
return new JsonBean<>(code, message, data, (int) (timestamp - requestEntity.getRequestTime()));
}
@Override
public Object buildException(RequestEntity requestEntity, Throwable throwable) {
return buildResult(requestEntity, 500, "系统内部出现错误");
}
}
# 基于Spring实现
与原有方式一样,自己实现全局异常统一处理,随后将magic-api
的异常处理交给spring
magic-api:
throw-exception: true # 执行出错时,异常将抛出处理
上次更新: 2023-03-23 13:11:51