@Order(Ordered.HIGHEST_PRECEDENCE)
@ControllerAdvice
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
private final static Logger logger = LogManager.getLogger(RestExceptionHandler.class);
//реализации методов абстрактного класса
@ExceptionHandler({ NullPointerException.class })
public ResponseEntity<Object> handleNullPointerException(NullPointerException ex, WebRequest request) {
logger.error(ex.getLocalizedMessage(), ex);
return buildResponseEntity(new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage(), ex, null, new ArrayList<> (Arrays.asList(ex.getStackTrace()))));
}
@ExceptionHandler({ IllegalArgumentException.class })
public ResponseEntity<Object> handleIllegalArgumentException(IllegalArgumentException ex, WebRequest request) {
logger.error(ex.getLocalizedMessage(), ex);
return buildResponseEntity( new ApiError(HttpStatus.BAD_REQUEST, ex.getMessage(), ex, null, new ArrayList<>(Arrays.asList(ex.getStackTrace()))) );
}
@ExceptionHandler({ RuntimeException.class })
public ResponseEntity<Object> handleRuntimeException(RuntimeException ex, WebRequest request) {
logger.error(ex.getLocalizedMessage(), ex);
return buildResponseEntity(new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage(), ex, null, new ArrayList<> (Arrays.asList(ex.getStackTrace()))));
}
@ExceptionHandler({ javax.validation.ConstraintViolationException.class })
public ResponseEntity<Object> handleConstraintViolationException(javax.validation.ConstraintViolationException ex, WebRequest request) {
logger.error(ex.getMessage(), ex);
String message = ex.getMessage();
return buildResponseEntity(new ApiError(HttpStatus.BAD_REQUEST, message, ex, null, new ArrayList<> (Arrays.asList(ex.getStackTrace()))));
}
private ResponseEntity<Object> buildResponseEntity(ApiError apiError) {
return new ResponseEntity<>(apiError, apiError.getStatus());
}
}