-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
57 lines (44 loc) · 2.08 KB
/
GlobalExceptionHandler.java
File metadata and controls
57 lines (44 loc) · 2.08 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
package com.example.enjoy.exception;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.server.ResponseStatusException;
import java.util.HashMap;
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
@ExceptionHandler(ResponseStatusException.class)
public ResponseEntity<?> handleResponseStatusException(ResponseStatusException e) {
log.error("ResponseStatusException 발생: 상태코드={}, 메시지={}",
e.getStatusCode(), e.getReason());
return ResponseEntity
.status(e.getStatusCode())
.body(new HashMap<String, Object>() {{
put("status", e.getStatusCode().value());
put("message", e.getReason());
}});
}
@ExceptionHandler(CustomException.class)
public ResponseEntity<?> handleCustomException(CustomException e) {
ErrorCode errorCode = e.getErrorCode();
log.error("CustomException 발생: 상태코드={}, 에러코드={}, 메시지={}",
errorCode.getStatus(), errorCode.name(), e.getMessage());
return createErrorResponse(errorCode, e.getMessage());
}
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<?> handleRuntimeException(RuntimeException e) {
log.error("RuntimeException 발생: {}, 타입: {}",
e.getMessage(), e.getClass().getSimpleName());
ErrorCode errorCode = ErrorCode.INTERNAL_SERVER_ERROR;
return createErrorResponse(errorCode, e.getMessage());
}
private ResponseEntity<?> createErrorResponse(ErrorCode errorCode, String message) {
return ResponseEntity.status(errorCode.getStatus())
.body(new HashMap<String, Object>() {{
put("status", errorCode.getStatus());
put("code", errorCode.name());
put("message", message);
}});
}
}