-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathExceptionAdvice.java
More file actions
180 lines (159 loc) · 6.21 KB
/
ExceptionAdvice.java
File metadata and controls
180 lines (159 loc) · 6.21 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package clap.server.exception;
import clap.server.exception.code.AuthErrorCode;
import clap.server.exception.code.BaseErrorCode;
import clap.server.exception.code.GlobalErrorCode;
import clap.server.exception.code.MemberErrorCode;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.ConstraintViolation;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
@Slf4j
@RestControllerAdvice(basePackages = {"clap.server.adapter.inbound.web"})
public class ExceptionAdvice extends ResponseEntityExceptionHandler {
@Override
public ResponseEntity<Object> handleMethodArgumentNotValid(
MethodArgumentNotValidException e,
HttpHeaders headers,
HttpStatusCode status,
WebRequest request
) {
log.error("Validation error occurred: {}", e.getMessage(), e); // 로그 추가
Map<String, String> errors = new LinkedHashMap<>();
e.getBindingResult()
.getFieldErrors()
.forEach(fieldError -> {
String fieldName = fieldError.getField();
String errorMessage = Optional.ofNullable(fieldError.getDefaultMessage()).orElse("");
errors.merge(fieldName, errorMessage,
(existingErrorMessage, newErrorMessage) -> existingErrorMessage + ", " + newErrorMessage);
});
return handleExceptionInternalArgs(
e,
HttpHeaders.EMPTY,
GlobalErrorCode.BAD_REQUEST,
request,
errors
);
}
@ExceptionHandler
public ResponseEntity<Object> validation(ConstraintViolationException e, WebRequest request) {
log.error("ConstraintViolationException occurred: {}", e.getMessage(), e); // 로그 추가
String errorMessage = e.getConstraintViolations().stream()
.map(ConstraintViolation::getMessage)
.findFirst()
.orElseThrow(() -> new RuntimeException("ConstraintViolationException Error"));
return handleExceptionInternalConstraint(
e,
GlobalErrorCode.valueOf(errorMessage),
HttpHeaders.EMPTY,
request
);
}
@ExceptionHandler
public ResponseEntity<Object> exception(Exception e, WebRequest request) {
log.error("Unhandled exception occurred: {}", e.getMessage(), e); // 로그 추가
return handleExceptionInternalFalse(
e,
GlobalErrorCode.INTERNAL_SERVER_ERROR,
HttpHeaders.EMPTY,
GlobalErrorCode.INTERNAL_SERVER_ERROR.getHttpStatus(),
request,
e.getMessage()
);
}
@ExceptionHandler(value = { BaseException.class })
public ResponseEntity<Object> onThrowException(BaseException exception, HttpServletRequest request) {
BaseErrorCode baseErrorCode = exception.getCode();
log.error("BaseException occurred: {}", baseErrorCode.getMessage());
return handleExceptionInternal(exception, baseErrorCode, null, request);
}
private ResponseEntity<Object> handleExceptionInternal(
BaseException e,
BaseErrorCode baseErrorCode,
HttpHeaders headers,
HttpServletRequest request
) {
String code = baseErrorCode.getCustomCode();
WebRequest webRequest = new ServletWebRequest(request);
return super.handleExceptionInternal(
e,
code,
headers,
baseErrorCode.getHttpStatus(),
webRequest
);
}
private ResponseEntity<Object> handleExceptionInternalFalse(
Exception e,
BaseErrorCode baseErrorCode,
HttpHeaders headers,
HttpStatus status,
WebRequest request,
String errorPoint
) {
String code = baseErrorCode.getCustomCode();
return super.handleExceptionInternal(
e,
code,
headers,
status,
request
);
}
private ResponseEntity<Object> handleExceptionInternalArgs(
Exception e,
HttpHeaders headers,
BaseErrorCode baseErrorCode,
WebRequest request,
Map<String, String> errorArgs
) {
String code = baseErrorCode.getCustomCode();
return super.handleExceptionInternal(
e,
code,
headers,
baseErrorCode.getHttpStatus(),
request
);
}
private ResponseEntity<Object> handleExceptionInternalConstraint(
Exception e,
BaseErrorCode baseErrorCode,
HttpHeaders headers,
WebRequest request
) {
String code = baseErrorCode.getCustomCode();
return super.handleExceptionInternal(
e,
code,
headers,
baseErrorCode.getHttpStatus(),
request
);
}
@ExceptionHandler(AccessDeniedException.class)
public ResponseEntity<Object> handleAccessDeniedException(AccessDeniedException e, WebRequest request) {
return handleExceptionInternalFalse(
e,
AuthErrorCode.FORBIDDEN,
HttpHeaders.EMPTY,
HttpStatus.FORBIDDEN,
request,
AuthErrorCode.FORBIDDEN.getMessage()
);
}
}