Skip to content

Commit 6e94053

Browse files
committed
refactor: 공통 예외 처리 로직 추가
1 parent bc3b3a8 commit 6e94053

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

src/main/java/eatda/exception/GlobalExceptionHandler.java

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,69 +24,70 @@ public class GlobalExceptionHandler {
2424

2525
@ExceptionHandler(BindException.class)
2626
public ResponseEntity<ErrorResponse> handleBindingException(BindException exception) {
27-
return toErrorResponse(EtcErrorCode.CLIENT_REQUEST_ERROR);
27+
return toErrorResponse(EtcErrorCode.CLIENT_REQUEST_ERROR, exception);
2828
}
2929

3030
@ExceptionHandler(ConstraintViolationException.class)
3131
public ResponseEntity<ErrorResponse> handleConstraintViolationException(ConstraintViolationException exception) {
32-
return toErrorResponse(EtcErrorCode.CLIENT_REQUEST_ERROR);
32+
return toErrorResponse(EtcErrorCode.CLIENT_REQUEST_ERROR, exception);
3333
}
3434

3535
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
3636
public ResponseEntity<ErrorResponse> handleMethodArgumentTypeMismatchException(
3737
MethodArgumentTypeMismatchException exception) {
38-
return toErrorResponse(EtcErrorCode.METHOD_ARGUMENT_TYPE_MISMATCH);
38+
return toErrorResponse(EtcErrorCode.METHOD_ARGUMENT_TYPE_MISMATCH, exception);
3939
}
4040

4141
@ExceptionHandler(ClientAbortException.class)
4242
public ResponseEntity<ErrorResponse> handleClientAbortException(ClientAbortException exception) {
43-
return toErrorResponse(EtcErrorCode.ALREADY_DISCONNECTED);
43+
log.warn("[ClientAbortException] {}: {}", exception.getClass().getSimpleName(), exception.getMessage());
44+
return toErrorResponse(EtcErrorCode.ALREADY_DISCONNECTED, null);
4445
}
4546

4647
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
4748
public ResponseEntity<ErrorResponse> handleHttpRequestMethodNotSupportedException(
4849
HttpRequestMethodNotSupportedException exception
4950
) {
50-
return toErrorResponse(EtcErrorCode.METHOD_NOT_SUPPORTED);
51+
return toErrorResponse(EtcErrorCode.METHOD_NOT_SUPPORTED, exception);
5152
}
5253

5354
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
5455
public ResponseEntity<ErrorResponse> handleHttpMediaTypeNotSupportedException(
5556
HttpMediaTypeNotSupportedException exception
5657
) {
57-
return toErrorResponse(EtcErrorCode.MEDIA_TYPE_NOT_SUPPORTED);
58+
return toErrorResponse(EtcErrorCode.MEDIA_TYPE_NOT_SUPPORTED, exception);
5859
}
5960

6061
@ExceptionHandler(NoResourceFoundException.class)
6162
public ResponseEntity<ErrorResponse> handleNoResourceFoundException(NoResourceFoundException exception) {
62-
return toErrorResponse(EtcErrorCode.NO_RESOURCE_FOUND);
63+
return toErrorResponse(EtcErrorCode.NO_RESOURCE_FOUND, exception);
6364
}
6465

6566
@ExceptionHandler(MissingRequestCookieException.class)
6667
public ResponseEntity<ErrorResponse> handleMissingRequestCookieException(MissingRequestCookieException exception) {
67-
return toErrorResponse(EtcErrorCode.NO_COOKIE_FOUND);
68+
return toErrorResponse(EtcErrorCode.NO_COOKIE_FOUND, exception);
6869
}
6970

7071
@ExceptionHandler(MissingRequestHeaderException.class)
7172
public ResponseEntity<ErrorResponse> handleMissingRequestHeaderException(MissingRequestHeaderException exception) {
72-
return toErrorResponse(EtcErrorCode.NO_HEADER_FOUND);
73+
return toErrorResponse(EtcErrorCode.NO_HEADER_FOUND, exception);
7374
}
7475

7576
@ExceptionHandler(MissingServletRequestParameterException.class)
7677
public ResponseEntity<ErrorResponse> handleMissingServletRequestParameterException(
7778
MissingServletRequestParameterException exception) {
78-
return toErrorResponse(EtcErrorCode.NO_PARAMETER_FOUND);
79+
return toErrorResponse(EtcErrorCode.NO_PARAMETER_FOUND, exception);
7980
}
8081

8182
@ExceptionHandler(HandlerMethodValidationException.class)
8283
public ResponseEntity<ErrorResponse> handleHandlerMethodValidationException(
8384
HandlerMethodValidationException exception) {
84-
return toErrorResponse(EtcErrorCode.VALIDATION_ERROR);
85+
return toErrorResponse(EtcErrorCode.VALIDATION_ERROR, exception);
8586
}
8687

8788
@ExceptionHandler(BusinessException.class)
8889
public ResponseEntity<ErrorResponse> handleBusinessException(BusinessException exception) {
89-
log.error("[BusinessException] handled: {}", exception.getErrorCode());
90+
log.warn("[BusinessException] Code: {}, Message: {}", exception.getErrorCode(), exception.getMessage());
9091
ErrorResponse response = new ErrorResponse(exception.getErrorCode());
9192
return ResponseEntity.status(exception.getStatus())
9293
.body(response);
@@ -95,10 +96,17 @@ public ResponseEntity<ErrorResponse> handleBusinessException(BusinessException e
9596
@ExceptionHandler(Exception.class)
9697
public ResponseEntity<ErrorResponse> handleException(Exception exception) {
9798
log.error("[Unhandled Exception] {}: {}", exception.getClass().getSimpleName(), exception.getMessage(), exception);
98-
return toErrorResponse(EtcErrorCode.INTERNAL_SERVER_ERROR);
99+
return toErrorResponse(EtcErrorCode.INTERNAL_SERVER_ERROR, null);
99100
}
100101

101-
private ResponseEntity<ErrorResponse> toErrorResponse(EtcErrorCode errorCode) {
102+
private ResponseEntity<ErrorResponse> toErrorResponse(EtcErrorCode errorCode, Exception exception) {
103+
if (exception != null) {
104+
if (errorCode.getStatus().is4xxClientError()) {
105+
log.warn("[Client Error] {}: {}", exception.getClass().getSimpleName(), exception.getMessage());
106+
} else {
107+
log.error("[Server Error] {}: {}", exception.getClass().getSimpleName(), exception.getMessage(), exception);
108+
}
109+
}
102110
return ResponseEntity.status(errorCode.getStatus())
103111
.body(new ErrorResponse(errorCode));
104112
}

0 commit comments

Comments
 (0)