|
| 1 | +package org.rest.common.web; |
| 2 | + |
| 3 | +import org.rest.common.persistence.exception.EntityNotFoundException; |
| 4 | +import org.springframework.dao.DataAccessException; |
| 5 | +import org.springframework.dao.DataIntegrityViolationException; |
| 6 | +import org.springframework.dao.InvalidDataAccessApiUsageException; |
| 7 | +import org.springframework.http.HttpHeaders; |
| 8 | +import org.springframework.http.HttpStatus; |
| 9 | +import org.springframework.http.ResponseEntity; |
| 10 | +import org.springframework.web.bind.annotation.ControllerAdvice; |
| 11 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 12 | +import org.springframework.web.context.request.WebRequest; |
| 13 | +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; |
| 14 | + |
| 15 | +@ControllerAdvice |
| 16 | +public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { |
| 17 | + |
| 18 | + public RestResponseEntityExceptionHandler() { |
| 19 | + super(); |
| 20 | + } |
| 21 | + |
| 22 | + // API |
| 23 | + |
| 24 | + @ExceptionHandler(value = { IllegalArgumentException.class, IllegalStateException.class, InvalidDataAccessApiUsageException.class, DataIntegrityViolationException.class, DataAccessException.class }) |
| 25 | + protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) { |
| 26 | + final String bodyOfResponse = "This should be application specific"; |
| 27 | + return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request); |
| 28 | + } |
| 29 | + |
| 30 | + @ExceptionHandler(value = { EntityNotFoundException.class }) |
| 31 | + protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) { |
| 32 | + final String bodyOfResponse = "This should be application specific"; |
| 33 | + return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request); |
| 34 | + } |
| 35 | + |
| 36 | +} |
0 commit comments