|
| 1 | +package org.baeldung.web.error; |
| 2 | + |
| 3 | +import javax.persistence.EntityNotFoundException; |
| 4 | + |
| 5 | +import org.hibernate.exception.ConstraintViolationException; |
| 6 | +import org.springframework.dao.DataAccessException; |
| 7 | +import org.springframework.dao.DataIntegrityViolationException; |
| 8 | +import org.springframework.dao.InvalidDataAccessApiUsageException; |
| 9 | +import org.springframework.http.HttpHeaders; |
| 10 | +import org.springframework.http.HttpStatus; |
| 11 | +import org.springframework.http.ResponseEntity; |
| 12 | +import org.springframework.http.converter.HttpMessageNotReadableException; |
| 13 | +import org.springframework.web.bind.MethodArgumentNotValidException; |
| 14 | +import org.springframework.web.bind.annotation.ControllerAdvice; |
| 15 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 16 | +import org.springframework.web.context.request.WebRequest; |
| 17 | +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; |
| 18 | + |
| 19 | +@ControllerAdvice |
| 20 | +public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { |
| 21 | + |
| 22 | + public RestResponseEntityExceptionHandler() { |
| 23 | + super(); |
| 24 | + } |
| 25 | + |
| 26 | + // API |
| 27 | + |
| 28 | + // 400 |
| 29 | + |
| 30 | + @ExceptionHandler({ ConstraintViolationException.class, DataIntegrityViolationException.class }) |
| 31 | + public ResponseEntity<Object> handleBadRequest(final RuntimeException ex, final WebRequest request) { |
| 32 | + final String bodyOfResponse = "This should be application specific"; |
| 33 | + return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request); |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + protected ResponseEntity<Object> handleHttpMessageNotReadable(final HttpMessageNotReadableException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { |
| 38 | + final String bodyOfResponse = "This should be application specific"; |
| 39 | + // ex.getCause() instanceof JsonMappingException, JsonParseException // for additional information later on |
| 40 | + return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { |
| 45 | + final String bodyOfResponse = "This should be application specific"; |
| 46 | + return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request); |
| 47 | + } |
| 48 | + |
| 49 | + // 403 |
| 50 | + |
| 51 | + // 404 |
| 52 | + |
| 53 | + @ExceptionHandler(value = { EntityNotFoundException.class }) |
| 54 | + protected ResponseEntity<Object> handleBadRequest(final EntityNotFoundException ex, final WebRequest request) { |
| 55 | + final String bodyOfResponse = "This should be application specific"; |
| 56 | + return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request); |
| 57 | + } |
| 58 | + |
| 59 | + // 409 |
| 60 | + |
| 61 | + @ExceptionHandler({ InvalidDataAccessApiUsageException.class, DataAccessException.class }) |
| 62 | + protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) { |
| 63 | + final String bodyOfResponse = "This should be application specific"; |
| 64 | + return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request); |
| 65 | + } |
| 66 | + |
| 67 | + // 412 |
| 68 | + |
| 69 | + // 500 |
| 70 | + |
| 71 | + @ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class }) |
| 72 | + /*500*/public ResponseEntity<Object> handleInternal(final RuntimeException ex, final WebRequest request) { |
| 73 | + logger.error("500 Status Code", ex); |
| 74 | + final String bodyOfResponse = "This should be application specific"; |
| 75 | + return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments