-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
48 lines (41 loc) · 2.04 KB
/
GlobalExceptionHandler.java
File metadata and controls
48 lines (41 loc) · 2.04 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
package flipnote.user.global.error;
import flipnote.user.global.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestCookieException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.List;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(BizException.class)
public ResponseEntity<ApiResponse<Void>> handleBizException(BizException e) {
log.warn("BizException: code={}, status={}, message={}",
e.getErrorCode().getCode(),
e.getErrorCode().getStatus(),
e.getErrorCode().getMessage()
);
return ResponseEntity.status(e.getErrorCode().getStatus()).body(ApiResponse.error(e.getErrorCode()));
}
@ExceptionHandler(MissingRequestCookieException.class)
public ResponseEntity<ApiResponse<Void>> handleMissingCookie(MissingRequestCookieException e) {
ApiResponse<Void> response = ApiResponse.<Void>builder()
.status(HttpStatus.BAD_REQUEST.value())
.code("MISSING_COOKIE")
.message("필수 쿠키가 누락되었습니다.")
.build();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ApiResponse<List<ApiResponse.FieldError>>> handleValidationException(MethodArgumentNotValidException e) {
return ResponseEntity.badRequest().body(ApiResponse.validationError(e.getBindingResult()));
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ApiResponse<Void>> handleException(Exception e) {
log.error("Unhandled exception", e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ApiResponse.internalError());
}
}