@ControllerAdvice
public class ExceptionController {
@ExceptionHandler(value = {BadRequestException.class,
InUseException.class, AlreadyExistsException.class, DataIntegrityViolationException.class,
NotEnoughException.class, NotSupportedException.class, NotYoursException.class, NoCommitYetException.class
})
public ResponseEntity<Object> badRequest(Exception e) {
String message = null;
if (e instanceof DataIntegrityViolationException) {
message = "Duplicated Key!";
} else if (e instanceof AlreadyExistsException) {
message = "Already Exists!";
} else if (e instanceof InUseException) {
message = "In Use!";
} else if (e instanceof NotEnoughException) {
message = "Not Enough!";
} else if (e instanceof NotSupportedException) {
message = "Not Support!";
} else if (e instanceof NotYoursException) {
message = "Not Yours!";
} else if (e instanceof NoCommitYetException) {
message = "Not Commit Yet";
}
return new ResponseEntity<>(message, HttpStatus.BAD_REQUEST);
}
@ExceptionHandler(value = {NotFoundException.class,})
public ResponseEntity<Object> notFound(Exception e) {
String message = null;
if (e instanceof NotFoundException) {
message = "Not Found!";
}
return new ResponseEntity<>(message, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(value = {PermissionDeniedException.class, UnauthorizedException.class})
public ResponseEntity<Object> unauthorized(Exception e) {
String message = null;
if (e instanceof PermissionDeniedException) {
message = "Permission Denied";
} else if (e instanceof UnauthorizedException) {
message = "Unauthorized";
}
return new ResponseEntity<>(message, HttpStatus.UNAUTHORIZED);
}
@ExceptionHandler(value = {InternalServerErrorException.class, UnbelievableException.class})
public ResponseEntity<Object> internalError(Exception e) {
String message = "";
return new ResponseEntity<>(message, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
自定义异常类
最新推荐文章于 2020-03-28 11:43:56 发布