-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
58 lines (48 loc) · 2.03 KB
/
GlobalExceptionHandler.java
File metadata and controls
58 lines (48 loc) · 2.03 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
package project.flipnote.common.exception;
import java.util.List;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import lombok.extern.slf4j.Slf4j;
import project.flipnote.common.model.response.ApiResponse;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(BizException.class)
public ResponseEntity<ApiResponse<Void>> handleBizError(BizException exception) {
log.warn("BizException handled: code={}, status={}, message={}",
exception.getErrorCode().getCode(),
exception.getErrorCode().getStatus(),
exception.getErrorCode().getMessage()
);
return ResponseEntity
.status(exception.getErrorCode().getStatus())
.body(ApiResponse.error(exception.getErrorCode()));
}
@ExceptionHandler(Exception.class)
public ResponseEntity<ApiResponse<Void>> handleGeneralError(Exception exception) {
log.error("Unhandled exception occurred", exception);
ErrorCode errorCode = CommonErrorCode.INTERNAL_SERVER_ERROR;
return ResponseEntity
.status(errorCode.getStatus())
.body(ApiResponse.error(errorCode));
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<ApiResponse<List<ApiResponse.FieldError>>> handleValidationError(
MethodArgumentNotValidException exception
) {
return ResponseEntity
.badRequest()
.body(ApiResponse.error(CommonErrorCode.INVALID_INPUT_VALUE, exception.getBindingResult()));
}
@ExceptionHandler(MissingServletRequestParameterException.class)
public ResponseEntity<String> handleMissingServletRequestParameter(
MissingServletRequestParameterException exception
) {
String missingParam = exception.getParameterName();
String message = String.format("필수 파라미터 '%s'가 없습니다.", missingParam);
return ResponseEntity.badRequest().body(message);
}
}