-
Notifications
You must be signed in to change notification settings - Fork 0
/
ErrorController.java
74 lines (59 loc) · 2.67 KB
/
ErrorController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package se.mow_e.controllers;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.server.ResponseStatusException;
import javax.validation.ConstraintViolationException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.HashMap;
import java.util.Map;
@RestControllerAdvice
@Validated
public class ErrorController {
@ExceptionHandler(ResponseStatusException.class)
public ResponseEntity<?> handleStatusException(ResponseStatusException ex) {
return ResponseEntity.status(ex.getRawStatusCode()).body(Map.of(
"status", "error",
"code", ex.getRawStatusCode(),
"reason", ex.getReason()
));
}
@ExceptionHandler(SQLIntegrityConstraintViolationException.class)
public ResponseEntity<?> handleStatusException(SQLIntegrityConstraintViolationException ex) {
return ResponseEntity.status(ex.getErrorCode()).body(Map.of(
"status", "error",
"code", ex.getErrorCode(),
"reason", ex.getLocalizedMessage(),
"sql", ex.getSQLState()
));
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, Object> handleValidationExceptions(MethodArgumentNotValidException ex) {
Map<String, Object> errors = new HashMap<>();
errors.put("status", "error");
ex.getBindingResult().getAllErrors().forEach((error) -> {
String fieldName = ((FieldError) error).getField();
String errorMessage = error.getDefaultMessage();
errors.put(fieldName, errorMessage);
});
return errors;
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
public Map<String, Object> handleValidationExceptions(ConstraintViolationException ex) {
Map<String, Object> errors = new HashMap<>();
errors.put("status", "error");
ex.getConstraintViolations().forEach((error) -> {
PathImpl path = (PathImpl) error.getPropertyPath();
errors.put(path.getLeafNode().toString(), error.getMessage());
});
return errors;
}
}