|
7 | 7 | import jakarta.validation.ConstraintViolationException;
|
8 | 8 | import lombok.AllArgsConstructor;
|
9 | 9 | import lombok.extern.slf4j.Slf4j;
|
| 10 | +import org.apache.commons.lang3.exception.ExceptionUtils; |
| 11 | +import org.springframework.data.mapping.PropertyReferenceException; |
10 | 12 | import org.springframework.http.HttpStatus;
|
11 |
| -import org.springframework.http.ResponseEntity; |
| 13 | +import org.springframework.http.ProblemDetail; |
12 | 14 | import org.springframework.validation.FieldError;
|
13 | 15 | import org.springframework.web.bind.MethodArgumentNotValidException;
|
14 | 16 | import org.springframework.web.bind.annotation.ControllerAdvice;
|
15 | 17 | import org.springframework.web.bind.annotation.ExceptionHandler;
|
16 |
| -import ss.mod.demo.api.constant.ContMessage; |
| 18 | +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; |
| 19 | +import ss.mod.demo.api.constant.ContField; |
17 | 20 | import ss.mod.demo.api.exception.BadDataException;
|
18 |
| -import ss.mod.demo.api.exception.ErrorObject; |
19 | 21 | import ss.mod.demo.service.entity.BaseService;
|
20 | 22 |
|
| 23 | +import java.util.HashMap; |
21 | 24 | import java.util.List;
|
22 | 25 | import java.util.Map;
|
23 |
| -import java.util.stream.Collectors; |
24 | 26 |
|
25 | 27 | /**
|
26 | 28 | * Global exception handler
|
|
32 | 34 | @Slf4j
|
33 | 35 | @AllArgsConstructor
|
34 | 36 | public class GlobalExceptionHandler extends BaseService {
|
35 |
| - @ExceptionHandler({MethodArgumentNotValidException.class}) |
36 |
| - public ResponseEntity<ErrorObject> validatorExceptionHandle(MethodArgumentNotValidException ex) { |
37 |
| - Map<String, List<String>> errors = ex.getBindingResult() |
38 |
| - .getFieldErrors() |
39 |
| - .stream() |
40 |
| - .collect(Collectors.groupingBy(FieldError::getField, Collectors.mapping(FieldError::getDefaultMessage, Collectors.toList()))); |
41 |
| - return new ResponseEntity<>( |
42 |
| - ErrorObject.builder() |
43 |
| - .message(resolveMessage(ex.getBody().getDetail())) |
44 |
| - .errorParams(errors) |
45 |
| - .code("400-1") |
46 |
| - .status(HttpStatus.BAD_REQUEST.value()) |
47 |
| - .build(), |
48 |
| - HttpStatus.BAD_REQUEST); |
| 37 | + |
| 38 | + /** |
| 39 | + * Handle validation problem detail. |
| 40 | + * |
| 41 | + * @param e the e |
| 42 | + * @return the problem detail |
| 43 | + */ |
| 44 | + @ExceptionHandler(MethodArgumentNotValidException.class) |
| 45 | + ProblemDetail handleValidation(MethodArgumentNotValidException e) { |
| 46 | + ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ExceptionUtils.getMessage(e)); |
| 47 | + problemDetail.setTitle("Invalid data provided"); |
| 48 | + problemDetail.setProperty(ContField.TIMESTAMP, System.currentTimeMillis()); |
| 49 | + problemDetail.setProperty(ContField.ERRORS, handleValidationError(e.getFieldErrors())); |
| 50 | + return problemDetail; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Handle validation problem detail. |
| 55 | + * |
| 56 | + * @param exception the exception |
| 57 | + * @return the problem detail |
| 58 | + */ |
| 59 | + @ExceptionHandler(ConstraintViolationException.class) |
| 60 | + ProblemDetail handleValidation(ConstraintViolationException exception) { |
| 61 | + ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ExceptionUtils.getMessage(exception)); |
| 62 | + problemDetail.setTitle("Invalid data provided"); |
| 63 | + problemDetail.setProperty(ContField.TIMESTAMP, System.currentTimeMillis()); |
| 64 | + problemDetail.setProperty(ContField.ERRORS, exception.getConstraintViolations().stream().map(ConstraintViolation::getMessage).toList()); |
| 65 | + return problemDetail; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Handle bad data exception problem detail. |
| 70 | + * |
| 71 | + * @param e the e |
| 72 | + * @return the problem detail |
| 73 | + */ |
| 74 | + @ExceptionHandler(BadDataException.class) |
| 75 | + ProblemDetail handleBadDataException(BadDataException e) { |
| 76 | + String errorMsg = ExceptionUtils.getMessage(e); |
| 77 | + ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, errorMsg); |
| 78 | + problemDetail.setTitle(errorMsg); |
| 79 | + problemDetail.setProperty(ContField.TIMESTAMP, System.currentTimeMillis()); |
| 80 | + return problemDetail; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Handle property reference exception problem detail. |
| 85 | + * |
| 86 | + * @param exception the exception |
| 87 | + * @return the problem detail |
| 88 | + */ |
| 89 | + @ExceptionHandler(PropertyReferenceException.class) |
| 90 | + ProblemDetail handlePropertyReferenceException(PropertyReferenceException exception) { |
| 91 | + ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ExceptionUtils.getMessage(exception)); |
| 92 | + problemDetail.setTitle(ExceptionUtils.getMessage(exception)); |
| 93 | + problemDetail.setProperty(ContField.TIMESTAMP, System.currentTimeMillis()); |
| 94 | + problemDetail.setProperty(ContField.PROPERTY, exception.getPropertyName()); |
| 95 | + return problemDetail; |
49 | 96 | }
|
50 | 97 |
|
51 |
| - @ExceptionHandler({ConstraintViolationException.class}) |
52 |
| - public ResponseEntity<ErrorObject> validatorExceptionHandle(ConstraintViolationException ex) { |
53 |
| - Map<String, List<String>> errors = ex.getConstraintViolations() |
54 |
| - .stream() |
55 |
| - .collect(Collectors.groupingBy(c -> c.getPropertyPath().toString(), Collectors.mapping(ConstraintViolation::getMessage, Collectors.toList()))); |
| 98 | + /** |
| 99 | + * Handle method argument type mismatch exception problem detail. |
| 100 | + * |
| 101 | + * @param exception the exception |
| 102 | + * @return the problem detail |
| 103 | + */ |
| 104 | + @ExceptionHandler(MethodArgumentTypeMismatchException.class) |
| 105 | + ProblemDetail handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException exception) { |
| 106 | + ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ExceptionUtils.getMessage(exception)); |
| 107 | + problemDetail.setTitle(ExceptionUtils.getMessage(exception)); |
| 108 | + problemDetail.setProperty(ContField.TIMESTAMP, System.currentTimeMillis()); |
| 109 | + problemDetail.setProperty(ContField.ARGUMENT, exception.getName()); |
| 110 | + return problemDetail; |
| 111 | + } |
56 | 112 |
|
57 |
| - return new ResponseEntity<>( |
58 |
| - ErrorObject.builder() |
59 |
| - .message(ex.getMessage()) |
60 |
| - .errorParams(errors) |
61 |
| - .code("400-2") |
62 |
| - .status(HttpStatus.BAD_REQUEST.value()) |
63 |
| - .build(), |
64 |
| - HttpStatus.BAD_REQUEST); |
| 113 | + /** |
| 114 | + * Handle illegal argument exception problem detail. |
| 115 | + * |
| 116 | + * @param exception the exception |
| 117 | + * @return the problem detail |
| 118 | + */ |
| 119 | + @ExceptionHandler(IllegalArgumentException.class) |
| 120 | + ProblemDetail handleIllegalArgumentException(IllegalArgumentException exception) { |
| 121 | + ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, ExceptionUtils.getMessage(exception)); |
| 122 | + problemDetail.setTitle(ExceptionUtils.getMessage(exception)); |
| 123 | + problemDetail.setProperty(ContField.TIMESTAMP, System.currentTimeMillis()); |
| 124 | + return problemDetail; |
65 | 125 | }
|
66 | 126 |
|
67 |
| - @ExceptionHandler({BadDataException.class}) |
68 |
| - public ResponseEntity<ErrorObject> badDataExceptionHandle(BadDataException ex) { |
69 |
| - return new ResponseEntity<>( |
70 |
| - ErrorObject.builder() |
71 |
| - .message(resolveMessage(ex.getMessage())) |
72 |
| - .code(ex.getCode() != null ? ex.getCode() : "400-3") |
73 |
| - .status(HttpStatus.BAD_REQUEST.value()) |
74 |
| - .build(), |
75 |
| - HttpStatus.BAD_REQUEST); |
| 127 | + /** |
| 128 | + * Handle exception problem detail. |
| 129 | + * |
| 130 | + * @param e the e |
| 131 | + * @return the problem detail |
| 132 | + */ |
| 133 | + @ExceptionHandler(Exception.class) |
| 134 | + ProblemDetail handleException(Exception e) { |
| 135 | + log.error("Error ", e); |
| 136 | + ProblemDetail problemDetail; |
| 137 | + problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.INTERNAL_SERVER_ERROR, ExceptionUtils.getMessage(e)); |
| 138 | + problemDetail.setTitle(ExceptionUtils.getMessage(e)); |
| 139 | + problemDetail.setProperty(ContField.TIMESTAMP, System.currentTimeMillis()); |
| 140 | + return problemDetail; |
76 | 141 | }
|
77 | 142 |
|
78 |
| - @ExceptionHandler({Exception.class}) |
79 |
| - public ResponseEntity<ErrorObject> globalHandler(Exception ex) { |
80 |
| - log.error(ex.getMessage(), ex); |
81 |
| - return new ResponseEntity<>( |
82 |
| - ErrorObject.builder() |
83 |
| - .message(resolveMessage(ContMessage.SOMETHING_WENT_WRONG)) |
84 |
| - .code("500-1") |
85 |
| - .status(HttpStatus.INTERNAL_SERVER_ERROR.value()) |
86 |
| - .build(), HttpStatus.INTERNAL_SERVER_ERROR); |
| 143 | + private Map<String, String> handleValidationError(List<FieldError> fieldErrors) { |
| 144 | + Map<String, String> messages = new HashMap<>(); |
| 145 | + fieldErrors.forEach(fieldError -> messages.put(fieldError.getField(), fieldError.getDefaultMessage())); |
| 146 | + return messages; |
87 | 147 | }
|
88 | 148 | }
|
0 commit comments