摘要: 基于springcloud + vue的分布式服务paascloud项目 (异常处理).
异常处理
参数抛指定的参数异常, 业务异常必须抛出指定编码。
正例:
1
| throw new UacBizException(ErrorCodeEnum.UAC10011021);
|
如有业务编码 需要抛出指定业务编码
1
| throw new UacBizException(ErrorCodeEnum.UAC10013002, menuId);
|
ErrorCodeEnum枚举实现详见代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public int deleteUacMenuById(Long id, LoginAuthDto loginAuthDto) { Preconditions.checkArgument(id != null, "菜单id不能为空"); int result; // 获取当前菜单信息 UacMenu uacMenuQuery = new UacMenu(); uacMenuQuery.setId(id); uacMenuQuery = mapper.selectOne(uacMenuQuery); if (PublicUtil.isEmpty(uacMenuQuery)) { throw new UacBizException(ErrorCodeEnum.UAC10013003, id); }
... return result; }
|
web全局异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| @Slf4j @RestControllerAdvice public class GlobalExceptionHandler { @Resource private TaskExecutor taskExecutor; @Value("${spring.profiles.active}") String profile; @Value("${spring.application.name}") String applicationName; @Resource private MdcExceptionLogFeignApi mdcExceptionLogFeignApi;
/** * 参数非法异常. * * @param e the e * * @return the wrapper */ @ExceptionHandler(IllegalArgumentException.class) @ResponseStatus(HttpStatus.OK) @ResponseBody public Wrapper illegalArgumentException(IllegalArgumentException e) { log.error("参数非法异常={}", e.getMessage(), e); return WrapMapper.wrap(ErrorCodeEnum.GL99990100.code(), e.getMessage()); }
/** * 业务异常. * * @param e the e * * @return the wrapper */ @ExceptionHandler(BusinessException.class) @ResponseStatus(HttpStatus.OK) @ResponseBody public Wrapper businessException(BusinessException e) { log.error("业务异常={}", e.getMessage(), e); return WrapMapper.wrap(e.getCode() == 0 ? Wrapper.ERROR_CODE : e.getCode(), e.getMessage()); }
/** * 全局异常. * * @param e the e * * @return the wrapper */ @ExceptionHandler(Exception.class) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ResponseBody public Wrapper exception(Exception e) { log.info("保存全局异常信息 ex={}", e.getMessage(), e); taskExecutor.execute(() -> { GlobalExceptionLogDto globalExceptionLogDto = new GlobalExceptionLogDto().getGlobalExceptionLogDto(e, profile, applicationName); mdcExceptionLogFeignApi.saveAndSendExceptionLog(globalExceptionLogDto); }); return WrapMapper.error(); } }
|
Rpc全局异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| @Slf4j public class Oauth2FeignErrorInterceptor implements ErrorDecoder { private final ErrorDecoder defaultErrorDecoder = new Default();
/** * Decode exception. * * @param methodKey the method key * @param response the response * * @return the exception */ @Override public Exception decode(final String methodKey, final Response response) { if (response.status() >= HttpStatus.BAD_REQUEST.value() && response.status() < HttpStatus.INTERNAL_SERVER_ERROR.value()) { return new HystrixBadRequestException("request exception wrapper"); }
ObjectMapper mapper = new ObjectMapper(); try { HashMap map = mapper.readValue(response.body().asInputStream(), HashMap.class); Integer code = (Integer) map.get("code"); String message = (String) map.get("message"); if (code != null) { ErrorCodeEnum anEnum = ErrorCodeEnum.getEnum(code); if (anEnum != null) { if (anEnum == ErrorCodeEnum.GL99990100) { throw new IllegalArgumentException(message); } else { throw new BusinessException(anEnum); } } else { throw new BusinessException(ErrorCodeEnum.GL99990500, message); } } } catch (IOException e) { log.info("Failed to process response body"); } return defaultErrorDecoder.decode(methodKey, response); } }
|
我的开源项目
如果觉得本文对您有帮助, 空闲的时候点帮忙加个星星, 您的支持将鼓励我继续创作的动力!