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
| @Slf4j @Component @RestControllerAdvice public class GlobalExceptionAdvice {
@ExceptionHandler(value = Exception.class) public Result handleServiceException(RuntimeException exception) { log.error("[RuntimeException]:", exception); return Result.fail(ResultCode.SERVER_ERROR.getCode(), ResultCode.SERVER_ERROR.getMessage()); }
@ExceptionHandler(value = ServiceException.class) public Result handleServiceException(ServiceException exception) { log.error("[ServiceException]:", exception); return Result.fail(exception.getCode(), exception.getMessage()); }
@ExceptionHandler(value = NoHandlerFoundException.class) @ResponseStatus(HttpStatus.NOT_FOUND) public <T> Result<T> handleNoMappingException(NoHandlerFoundException exception) { log.info("NoHandlerFoundException: {}", exception.getMessage()); return Result.fail(ResultCode.NOT_FOUND.getCode(), exception.getMessage()); }
@ExceptionHandler(value = BindException.class) public Result handleBindException(BindException exception) { log.error("[BindException]:", exception); List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors(); for (FieldError error : fieldErrors) { return Result.fail(ResultCode.FAIL.getCode(), error.getDefaultMessage()); } return Result.fail(); }
@ExceptionHandler(value = MethodArgumentNotValidException.class) public Result handleMethodArgumentNotValidException(MethodArgumentNotValidException exception) { log.error("[MethodArgumentNotValidException]:", exception); List<FieldError> fieldErrors = exception.getBindingResult().getFieldErrors(); for (FieldError error : fieldErrors) { return Result.fail(ResultCode.FAIL.getCode(), error.getDefaultMessage()); } return Result.fail(); } }
|