-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
117 lines (99 loc) · 3.81 KB
/
GlobalExceptionHandler.java
File metadata and controls
117 lines (99 loc) · 3.81 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
package com.sprint.mission.discodeit.advice;
import static com.sprint.mission.discodeit.exception.ErrorCode.INTERNAL_SERVER_ERROR;
import static com.sprint.mission.discodeit.exception.ErrorCode.*;
import static org.springframework.http.HttpStatus.*;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import com.sprint.mission.discodeit.exception.DiscodeitException;
import com.sprint.mission.discodeit.exception.ErrorCode;
import com.sprint.mission.discodeit.exception.ErrorResponse;
import lombok.extern.slf4j.Slf4j;
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception e) {
// Log the eception (you can use a logging framework like SLF4J)
log.error("Unexpected Error 발생", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResponse.builder()
.timestamp(Instant.now())
.code(INTERNAL_SERVER_ERROR.name())
.message("Unexpected Error 발생: " + e.getMessage())
.details(Map.of())
.exceptionType(e.getClass().getSimpleName())
.status(HttpStatus.INTERNAL_SERVER_ERROR.value())
.build());
}
@ExceptionHandler(DiscodeitException.class)
public ResponseEntity<ErrorResponse> handleNoSuchElementException(DiscodeitException e) {
log.error("DiscodeitException Error 발생", e);
HttpStatus status = determineHttpStatus(e);
// 에러 메시지 추출 및 응답 생성
return ResponseEntity.status(status).body(ErrorResponse.builder()
.timestamp(e.getTimestamp())
.code(e.getErrorCode().name())
.message(e.getMessage())
.details(e.getDetails())
.exceptionType(e.getClass().getSimpleName())
.status(status.value())
.build());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ErrorResponse> handleValidationExceptions(
MethodArgumentNotValidException e) {
log.error("요청 유효성 검사 실패: {}", e.getMessage());
Map<String, Object> validationErrorsDetail = new HashMap<>();
e.getBindingResult().getAllErrors().forEach(error -> {
String fieldName = ((FieldError)error).getField();
String errorMessage = error.getDefaultMessage();
validationErrorsDetail.put(fieldName, errorMessage);
});
return ResponseEntity
.status(BAD_REQUEST)
.body(ErrorResponse.builder()
.timestamp(Instant.now())
.code(VALIDATION_ERROR.name())
.message(VALIDATION_ERROR.getMessage())
.details(validationErrorsDetail)
.exceptionType(e.getClass().getSimpleName())
.status(BAD_REQUEST.value())
.build());
}
private HttpStatus determineHttpStatus(DiscodeitException exception) {
ErrorCode errorCode = exception.getErrorCode();
return switch (errorCode) {
case USER_NOT_FOUND,
CHANNEL_NOT_FOUND,
USER_STATUS_NOT_FOUND,
BINARY_CONTENT_NOT_FOUND,
AUTHOR_NOT_FOUND,
MESSAGE_NOT_FOUND,
READ_STATUS_NOT_FOUND -> NOT_FOUND;
case DUPLICATE_USERNAME,
DUPLICATE_USER_EMAIL,
INVALID_USER_PARAM,
INVALID_REQUEST,
PRIVATE_CHANNEL_UPDATE_NOT_ALLOWED,
PARTICIPANTS_EMPTY,
READ_STATUS_DUPLICATE,
DUPLICATE_USERNAME_OR_EMAIL,
VALIDATION_ERROR,
WRONG_PASSWORD -> BAD_REQUEST;
case INVALID_USER_CREDENTIALS,
LOGIN_FAIL -> UNAUTHORIZED;
case NOT_AUTHORIZED -> FORBIDDEN;
case INTERNAL_SERVER_ERROR,
URI_CREATE_FAIL,
SAVE_TO_FILE_STORAGE_FAIL,
BINARY_CONTENT_READ_FAIL,
ACCESS_TOKEN_CREATE_FAIL -> HttpStatus.INTERNAL_SERVER_ERROR;
};
}
}