|
1 | 1 | package com.bnc.sbjb.rest;
|
2 | 2 |
|
3 |
| -import com.bnc.sbjb.model.api.CustomError; |
| 3 | +import com.bnc.api.model.error.CustomError; |
| 4 | +import com.bnc.api.model.error.ValidationError; |
| 5 | +import java.nio.file.AccessDeniedException; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.NoSuchElementException; |
| 9 | +import javax.servlet.http.HttpServletRequest; |
4 | 10 | import org.slf4j.Logger;
|
5 | 11 | import org.slf4j.LoggerFactory;
|
6 | 12 | import org.springframework.http.HttpStatus;
|
| 13 | +import org.springframework.http.converter.HttpMessageNotReadableException; |
| 14 | +import org.springframework.validation.FieldError; |
| 15 | +import org.springframework.web.HttpMediaTypeNotSupportedException; |
| 16 | +import org.springframework.web.HttpRequestMethodNotSupportedException; |
| 17 | +import org.springframework.web.bind.MethodArgumentNotValidException; |
7 | 18 | import org.springframework.web.bind.annotation.ControllerAdvice;
|
8 | 19 | import org.springframework.web.bind.annotation.ExceptionHandler;
|
9 | 20 | import org.springframework.web.bind.annotation.ResponseBody;
|
10 | 21 | import org.springframework.web.bind.annotation.ResponseStatus;
|
| 22 | +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; |
11 | 23 |
|
12 | 24 | @ControllerAdvice
|
13 | 25 | public class DefaultExceptionHandler {
|
14 | 26 |
|
15 | 27 | private static final Logger logger = LoggerFactory.getLogger(DefaultExceptionHandler.class);
|
16 | 28 |
|
| 29 | + @ExceptionHandler({NoSuchElementException.class}) |
| 30 | + @ResponseStatus(HttpStatus.NOT_FOUND) |
| 31 | + public void handleExceptionNotFound(final Exception ex) { |
| 32 | + logger.debug("Could not find resource", ex); |
| 33 | + } |
| 34 | + |
| 35 | + @ExceptionHandler(IllegalArgumentException.class) |
| 36 | + @ResponseStatus(HttpStatus.BAD_REQUEST) |
| 37 | + @ResponseBody |
| 38 | + public CustomError handleIllegalArgumentException(final IllegalArgumentException ex) { |
| 39 | + logger.debug("Invalid Argument Received", ex); |
| 40 | + return new CustomError(HttpStatus.BAD_REQUEST, "Invalid Argument Received"); |
| 41 | + } |
| 42 | + |
| 43 | + @ExceptionHandler({ |
| 44 | + HttpMessageNotReadableException.class, |
| 45 | + MethodArgumentTypeMismatchException.class, |
| 46 | + HttpMediaTypeNotSupportedException.class}) |
| 47 | + @ResponseStatus(HttpStatus.BAD_REQUEST) |
| 48 | + @ResponseBody |
| 49 | + public CustomError handleBadRequests(final Exception ex) { |
| 50 | + logger.debug("Bad input from client", ex); |
| 51 | + return new CustomError(HttpStatus.BAD_REQUEST, ex.getMessage()); |
| 52 | + } |
| 53 | + |
| 54 | + @ExceptionHandler(MethodArgumentNotValidException.class) |
| 55 | + @ResponseStatus(code = HttpStatus.BAD_REQUEST) |
| 56 | + @ResponseBody |
| 57 | + public CustomError handleMethodArgumentNotValid(MethodArgumentNotValidException ex) { |
| 58 | + CustomError errors = new CustomError(HttpStatus.BAD_REQUEST, "Validation Error"); |
| 59 | + List<ValidationError> subErrors = new ArrayList<>(); |
| 60 | + if (ex.getBindingResult().getFieldErrorCount() > 0) { |
| 61 | + List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors(); |
| 62 | + for (FieldError fieldError : fieldErrors) { |
| 63 | + subErrors.add(new ValidationError(fieldError.getObjectName().replace("Dto", ""), fieldError.getField(), |
| 64 | + fieldError.getRejectedValue(), fieldError.getDefaultMessage())); |
| 65 | + } |
| 66 | + } |
| 67 | + errors.setSubErrors(subErrors); |
| 68 | + return errors; |
| 69 | + } |
| 70 | + |
| 71 | + @ExceptionHandler(AccessDeniedException.class) |
| 72 | + @ResponseStatus(HttpStatus.FORBIDDEN) |
| 73 | + public void handleAccessDenied(AccessDeniedException ex) { |
| 74 | + logger.debug("Access Denied", ex); |
| 75 | + } |
| 76 | + |
| 77 | + @ExceptionHandler({HttpRequestMethodNotSupportedException.class}) |
| 78 | + @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) |
| 79 | + @ResponseBody |
| 80 | + public CustomError handleExceptionMethodNotAllowed(final Exception ex) { |
| 81 | + logger.debug("Method not allowed", ex); |
| 82 | + return new CustomError(HttpStatus.METHOD_NOT_ALLOWED, "HTTP Method not allowed"); |
| 83 | + } |
| 84 | + |
17 | 85 | @ExceptionHandler(Exception.class)
|
18 | 86 | @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
19 | 87 | @ResponseBody
|
20 |
| - public CustomError handleException(Exception ex) { |
21 |
| - logger.error("Unknown exception errorMessage=\"{}\"]", ex.getMessage(), ex); |
| 88 | + public CustomError handleException(HttpServletRequest httpServletRequest, Exception ex) { |
| 89 | + logger.error("Unknown exception [queryString=\"{}\" errorMessage=\"{}\"]", httpServletRequest.getQueryString(), ex.getMessage(), ex); |
22 | 90 | return new CustomError(HttpStatus.INTERNAL_SERVER_ERROR,
|
23 | 91 | "Oops something went wrong. Please contact us if this keeps occurring.");
|
24 | 92 | }
|
|
0 commit comments