|
| 1 | +package org.baeldung.web; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import org.springframework.beans.TypeMismatchException; |
| 7 | +import org.springframework.http.HttpHeaders; |
| 8 | +import org.springframework.http.HttpStatus; |
| 9 | +import org.springframework.http.ResponseEntity; |
| 10 | +import org.springframework.validation.BindException; |
| 11 | +import org.springframework.validation.FieldError; |
| 12 | +import org.springframework.validation.ObjectError; |
| 13 | +import org.springframework.web.HttpMediaTypeNotSupportedException; |
| 14 | +import org.springframework.web.HttpRequestMethodNotSupportedException; |
| 15 | +import org.springframework.web.bind.MethodArgumentNotValidException; |
| 16 | +import org.springframework.web.bind.MissingServletRequestParameterException; |
| 17 | +import org.springframework.web.bind.annotation.ControllerAdvice; |
| 18 | +import org.springframework.web.bind.annotation.ExceptionHandler; |
| 19 | +import org.springframework.web.context.request.WebRequest; |
| 20 | +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; |
| 21 | +import org.springframework.web.multipart.support.MissingServletRequestPartException; |
| 22 | +import org.springframework.web.servlet.NoHandlerFoundException; |
| 23 | +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; |
| 24 | + |
| 25 | +@ControllerAdvice |
| 26 | +public class CustomRestExceptionHandler extends ResponseEntityExceptionHandler { |
| 27 | + |
| 28 | + // 400 |
| 29 | + |
| 30 | + @Override |
| 31 | + protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { |
| 32 | + logger.info(ex.getClass().getName()); |
| 33 | + // |
| 34 | + final List<String> errors = new ArrayList<String>(); |
| 35 | + for (final FieldError error : ex.getBindingResult().getFieldErrors()) { |
| 36 | + errors.add(error.getField() + ": " + error.getDefaultMessage()); |
| 37 | + } |
| 38 | + for (final ObjectError error : ex.getBindingResult().getGlobalErrors()) { |
| 39 | + errors.add(error.getObjectName() + ": " + error.getDefaultMessage()); |
| 40 | + } |
| 41 | + final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors); |
| 42 | + return handleExceptionInternal(ex, apiError, headers, apiError.getStatus(), request); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected ResponseEntity<Object> handleBindException(final BindException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { |
| 47 | + logger.info(ex.getClass().getName()); |
| 48 | + // |
| 49 | + final List<String> errors = new ArrayList<String>(); |
| 50 | + for (final FieldError error : ex.getBindingResult().getFieldErrors()) { |
| 51 | + errors.add(error.getField() + ": " + error.getDefaultMessage()); |
| 52 | + } |
| 53 | + for (final ObjectError error : ex.getBindingResult().getGlobalErrors()) { |
| 54 | + errors.add(error.getObjectName() + ": " + error.getDefaultMessage()); |
| 55 | + } |
| 56 | + final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors); |
| 57 | + return handleExceptionInternal(ex, apiError, headers, apiError.getStatus(), request); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected ResponseEntity<Object> handleTypeMismatch(final TypeMismatchException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { |
| 62 | + logger.info(ex.getClass().getName()); |
| 63 | + // |
| 64 | + final String error = ex.getValue() + " value for " + ex.getPropertyName() + " should be of type " + ex.getRequiredType(); |
| 65 | + |
| 66 | + final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), error); |
| 67 | + return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus()); |
| 68 | + } |
| 69 | + |
| 70 | + @ExceptionHandler({ MethodArgumentTypeMismatchException.class }) |
| 71 | + public ResponseEntity<Object> handleMethodArgumentTypeMismatch(final MethodArgumentTypeMismatchException ex, final WebRequest request) { |
| 72 | + logger.info(ex.getClass().getName()); |
| 73 | + // |
| 74 | + final String error = ex.getName() + " should be of type " + ex.getRequiredType().getName(); |
| 75 | + |
| 76 | + final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), error); |
| 77 | + return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus()); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected ResponseEntity<Object> handleMissingServletRequestPart(final MissingServletRequestPartException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { |
| 82 | + logger.info(ex.getClass().getName()); |
| 83 | + // |
| 84 | + final String error = ex.getRequestPartName() + " part is missing"; |
| 85 | + final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), error); |
| 86 | + return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus()); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + protected ResponseEntity<Object> handleMissingServletRequestParameter(final MissingServletRequestParameterException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { |
| 91 | + logger.info(ex.getClass().getName()); |
| 92 | + // |
| 93 | + final String error = ex.getParameterName() + " parameter is missing"; |
| 94 | + final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), error); |
| 95 | + return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus()); |
| 96 | + } |
| 97 | + |
| 98 | + // 404 |
| 99 | + |
| 100 | + @Override |
| 101 | + protected ResponseEntity<Object> handleNoHandlerFoundException(final NoHandlerFoundException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { |
| 102 | + logger.info(ex.getClass().getName()); |
| 103 | + // |
| 104 | + final String error = "No handler found for " + ex.getHttpMethod() + " " + ex.getRequestURL(); |
| 105 | + |
| 106 | + final ApiError apiError = new ApiError(HttpStatus.NOT_FOUND, ex.getLocalizedMessage(), error); |
| 107 | + return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus()); |
| 108 | + } |
| 109 | + |
| 110 | + // 405 |
| 111 | + |
| 112 | + @Override |
| 113 | + protected ResponseEntity<Object> handleHttpRequestMethodNotSupported(final HttpRequestMethodNotSupportedException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { |
| 114 | + logger.info(ex.getClass().getName()); |
| 115 | + // |
| 116 | + final StringBuilder builder = new StringBuilder(); |
| 117 | + builder.append(ex.getMethod()); |
| 118 | + builder.append(" method is not supported for this request. Supported methods are "); |
| 119 | + ex.getSupportedMethods(); |
| 120 | + |
| 121 | + final ApiError apiError = new ApiError(HttpStatus.METHOD_NOT_ALLOWED, ex.getLocalizedMessage(), builder.toString()); |
| 122 | + return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus()); |
| 123 | + } |
| 124 | + |
| 125 | + // 415 |
| 126 | + |
| 127 | + @Override |
| 128 | + protected ResponseEntity<Object> handleHttpMediaTypeNotSupported(final HttpMediaTypeNotSupportedException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { |
| 129 | + logger.info(ex.getClass().getName()); |
| 130 | + // |
| 131 | + final StringBuilder builder = new StringBuilder(); |
| 132 | + builder.append(ex.getContentType()); |
| 133 | + builder.append(" media type is not supported. Supported media types are "); |
| 134 | + ex.getSupportedMediaTypes().forEach(t -> builder.append(t + ", ")); |
| 135 | + |
| 136 | + final ApiError apiError = new ApiError(HttpStatus.UNSUPPORTED_MEDIA_TYPE, ex.getLocalizedMessage(), builder.substring(0, builder.length() - 2)); |
| 137 | + return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus()); |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | + // 500 |
| 142 | + |
| 143 | + @ExceptionHandler({ Exception.class }) |
| 144 | + public ResponseEntity<Object> handleAll(final Exception ex, final WebRequest request) { |
| 145 | + logger.info(ex.getClass().getName()); |
| 146 | + // |
| 147 | + final ApiError apiError = new ApiError(HttpStatus.INTERNAL_SERVER_ERROR, ex.getLocalizedMessage(), "error occurred"); |
| 148 | + return new ResponseEntity<Object>(apiError, new HttpHeaders(), apiError.getStatus()); |
| 149 | + } |
| 150 | + |
| 151 | +} |
| 152 | + |
0 commit comments