|
|
|
|
@@ -0,0 +1,86 @@
|
|
|
|
|
package com.baeldung.web.error;
|
|
|
|
|
|
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
|
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
|
import org.springframework.web.context.request.WebRequest;
|
|
|
|
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
|
|
|
|
|
|
|
|
|
import com.baeldung.web.exception.MyDataAccessException;
|
|
|
|
|
import com.baeldung.web.exception.MyDataIntegrityViolationException;
|
|
|
|
|
import com.baeldung.web.exception.MyResourceNotFoundException;
|
|
|
|
|
|
|
|
|
|
@ControllerAdvice
|
|
|
|
|
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
|
|
|
|
|
|
|
|
|
|
public RestResponseEntityExceptionHandler() {
|
|
|
|
|
super();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// API
|
|
|
|
|
|
|
|
|
|
// 400
|
|
|
|
|
/*
|
|
|
|
|
* Some examples of exceptions that we could retrieve as 400 (BAD_REQUEST) responses:
|
|
|
|
|
* Hibernate's ConstraintViolationException
|
|
|
|
|
* Spring's DataIntegrityViolationException
|
|
|
|
|
*/
|
|
|
|
|
@ExceptionHandler({ MyDataIntegrityViolationException.class })
|
|
|
|
|
public ResponseEntity<Object> handleBadRequest(final MyDataIntegrityViolationException ex, final WebRequest request) {
|
|
|
|
|
final String bodyOfResponse = "This should be application specific";
|
|
|
|
|
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected ResponseEntity<Object> handleHttpMessageNotReadable(final HttpMessageNotReadableException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
|
|
|
|
|
final String bodyOfResponse = "This should be application specific";
|
|
|
|
|
// ex.getCause() instanceof JsonMappingException, JsonParseException // for additional information later on
|
|
|
|
|
return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
|
|
|
|
|
final String bodyOfResponse = "This should be application specific";
|
|
|
|
|
return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 404
|
|
|
|
|
/*
|
|
|
|
|
* Some examples of exceptions that we could retrieve as 404 (NOT_FOUND) responses:
|
|
|
|
|
* Java Persistence's EntityNotFoundException
|
|
|
|
|
*/
|
|
|
|
|
@ExceptionHandler(value = { MyResourceNotFoundException.class })
|
|
|
|
|
protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) {
|
|
|
|
|
final String bodyOfResponse = "This should be application specific";
|
|
|
|
|
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 409
|
|
|
|
|
/*
|
|
|
|
|
* Some examples of exceptions that we could retrieve as 409 (CONFLICT) responses:
|
|
|
|
|
* Spring's InvalidDataAccessApiUsageException
|
|
|
|
|
* Spring's DataAccessException
|
|
|
|
|
*/
|
|
|
|
|
@ExceptionHandler({ MyDataAccessException.class})
|
|
|
|
|
protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) {
|
|
|
|
|
final String bodyOfResponse = "This should be application specific";
|
|
|
|
|
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 412
|
|
|
|
|
|
|
|
|
|
// 500
|
|
|
|
|
|
|
|
|
|
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
|
|
|
|
|
/*500*/public ResponseEntity<Object> handleInternal(final RuntimeException ex, final WebRequest request) {
|
|
|
|
|
logger.error("500 Status Code", ex);
|
|
|
|
|
final String bodyOfResponse = "This should be application specific";
|
|
|
|
|
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|