Вы можете использовать @ControllerAdvice для обработки пользовательских исключений, например, для исключения UserNotAuthenticatedException
:
1) Создать универсальное c исключение
public abstract class GenericException extends RuntimeException {
public GenericException (String messageKey) {
super(messageKey);
}
public GenericException (String messageKey, Throwable cause) {
super(messageKey, cause);
}
private static final long serialVersionUID = 1L;
public abstract HttpStatus getHttpStatus();
}
2) Создать спецификацию c исключение с вашей аннотацией:
@ResponseStatus(HttpStatus.NOT_FOUND)
public class UserNotAuthenticatedException extends GenericException {
private static final long serialVersionUID = 1L;
public UserNotAuthenticatedException () {
super(ExceptionKeys.RESOURCE_NOT_FOUND_EXCEPTION);
}
public UserNotAuthenticatedException (Throwable cause) {
super(ExceptionKeys.RESOURCE_NOT_FOUND_EXCEPTION, cause);
}
public UserNotAuthenticatedException (String messageKey) {
super(messageKey);
}
public UserNotAuthenticatedException (String messageKey, Throwable cause) {
super(messageKey, cause);
}
@Override
public HttpStatus getHttpStatus() {
return HttpStatus.NOT_FOUND;
}
}
3) Создайте ошибку DTO:
@Data
public class ErrorDto {
private int code;
private String message;
private String additionalData;
public ErrorDto(String message, int code) {
super();
this.message = message;
this.code = code;
}
public ErrorDto(String message, String additionalData) {
super();
this.message = message;
this.additionalData = additionalData;
}
public ErrorDto() {
}
}
4) Затем создайте глобальный обработчик исключений для сопоставления вашего исключения со статусом:
@ControllerAdvice
@RestController
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler({GenericException .class})
public ResponseEntity<ErrorDto> handlePortalExceptions(final GenericException pex) {
log.error("{} [{}]", pex.getClass().getName(), pex.getMessage());
log.error(pex.getMessage(), pex);
final ErrorDto errorDetails = new ErrorDto(pex.getMessage(), pex.getHttpStatus().value());
errorDetails.setAdditionalData(pex.getCause() != null ? pex.getCause().getLocalizedMessage() : pex.getLocalizedMessage());
return new ResponseEntity<>(errorDetails, pex.getHttpStatus());
}
@ExceptionHandler({Exception.class})
public ResponseEntity<Object> handleAllExceptions(final Exception ex, WebRequest request) {
log.error("{} detected [{}]", ex.getClass().getName(), ex.getMessage());
log.error(ex.getMessage(), ex);
final ErrorDto errorDetails = new ErrorDto(ExceptionKeys.INTERNAL_SERVER_ERROR, HttpStatus.INTERNAL_SERVER_ERROR.value());
errorDetails.setAdditionalData(ex.getCause() != null ? ex.getCause().getLocalizedMessage() : ex.getLocalizedMessage());
return new ResponseEntity<>(errorDetails, HttpStatus.INTERNAL_SERVER_ERROR);
}
}