-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomGlobalExceptionHandler.java
97 lines (89 loc) · 3.57 KB
/
CustomGlobalExceptionHandler.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.bookstore.exception;
import java.time.LocalDateTime;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
@ControllerAdvice
public class CustomGlobalExceptionHandler extends ResponseEntityExceptionHandler {
@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException ex,
HttpHeaders headers,
HttpStatusCode status,
WebRequest request
) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("status", HttpStatus.BAD_REQUEST.value());
List<String> errors = ex.getBindingResult().getAllErrors().stream()
.map(this::getErrorMessage)
.toList();
body.put("errors", errors);
return new ResponseEntity<>(body, headers, status);
}
@ExceptionHandler(EntityNotFoundException.class)
protected ResponseEntity<Object> handleEntityNotFoundException(
EntityNotFoundException ex,
WebRequest request
) {
Map<String, Object> body = getBody(ex);
return handleExceptionInternal(
ex, body, new HttpHeaders(), HttpStatus.BAD_REQUEST, request
);
}
@ExceptionHandler(RegistrationException.class)
protected ResponseEntity<Object> handleRegistrationException(
RegistrationException ex,
WebRequest request
) {
Map<String, Object> body = getBody(ex);
return handleExceptionInternal(
ex, body, new HttpHeaders(), HttpStatus.BAD_REQUEST, request
);
}
@ExceptionHandler(DataBaseConflictException.class)
protected ResponseEntity<Object> handleDataBaseConflictException(
DataBaseConflictException ex,
WebRequest request
) {
Map<String, Object> body = getBody(ex);
return handleExceptionInternal(
ex, body, new HttpHeaders(), HttpStatus.BAD_REQUEST, request
);
}
@Override
protected ResponseEntity<Object> handleExceptionInternal(
Exception ex,
Object body,
HttpHeaders headers,
HttpStatusCode statusCode,
WebRequest request) {
return super.handleExceptionInternal(ex, body, headers, statusCode, request);
}
private String getErrorMessage(ObjectError objectError) {
if (objectError instanceof FieldError) {
String field = ((FieldError) objectError).getField();
String message = objectError.getDefaultMessage();
return field + " " + message;
}
return objectError.getDefaultMessage();
}
private Map<String, Object> getBody(Exception ex) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("timestamp", LocalDateTime.now());
body.put("status", HttpStatus.BAD_REQUEST.value());
body.put("errors", ex.getMessage());
return body;
}
}