-
Notifications
You must be signed in to change notification settings - Fork 165
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
123 lines (111 loc) · 5.64 KB
/
GlobalExceptionHandler.java
File metadata and controls
123 lines (111 loc) · 5.64 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
118
119
120
121
122
123
package org.programmers.VoucherManagement.global.exception;
import com.fasterxml.jackson.core.JsonProcessingException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.programmers.VoucherManagement.global.response.ErrorCode;
import org.programmers.VoucherManagement.global.response.ErrorResponse;
import org.programmers.VoucherManagement.member.exception.MemberException;
import org.programmers.VoucherManagement.voucher.exception.VoucherException;
import org.programmers.VoucherManagement.wallet.exception.WalletException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindException;
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 org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
@RequiredArgsConstructor
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
/**
* [Exception] 객체 혹은 파라미터의 데이터 값이 유효하지 않은 경우
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
log.error("Handle MothodArgumentNotValidException", e.getMessage());
final ErrorResponse response = ErrorResponse.of(ErrorCode.INVALID_METHOD_ERROR, e.getBindingResult());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
/**
* [Exception] 클라이언트에서 request로 '파라미터로' 데이터가 넘어오지 않았을 경우
*
* @param ex MissingServletRequestParameterException
* @return ResponseEntity<ErrorResponse>
*/
@ExceptionHandler(MissingServletRequestParameterException.class)
protected ResponseEntity<ErrorResponse> handleMissingRequestHeaderExceptionException(
MissingServletRequestParameterException ex) {
log.error("Handle MissingServletRequestParameterException", ex);
final ErrorResponse response = ErrorResponse.of(ErrorCode.REQUEST_PARAM_MISSING_ERROR, ex.getMessage());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
/**
* [Exception] enum type 일치하지 않아 binding 못할 경우
* 주로 @RequestParam enum으로 binding 못했을 경우 발생
*/
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
protected ResponseEntity<ErrorResponse> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
log.error("Handle MethodArgumentTypeMismatchException", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.INVALID_INPUT_VALUE_ERROR, e.getMessage());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
/**
* [Exception] com.fasterxml.jackson.core 내에 Exception 발생하는 경우
*
* @param ex JsonProcessingException
* @return ResponseEntity<ErrorResponse>
*/
@ExceptionHandler(JsonProcessingException.class)
protected ResponseEntity<ErrorResponse> handleJsonProcessingException(JsonProcessingException ex) {
log.error("handleJsonProcessingException", ex);
final ErrorResponse response = ErrorResponse.of(ErrorCode.REQUEST_BODY_MISSING_ERROR, ex.getMessage());
return new ResponseEntity<>(response, HttpStatus.OK);
}
/**
* [Exception] @ModelAttribute 으로 binding error 발생할 경우
*/
@ExceptionHandler(BindException.class)
protected ResponseEntity<ErrorResponse> handleBindException(BindException e) {
log.error("Handle BindException : ", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.INVALID_INPUT_VALUE_ERROR, e.getBindingResult());
return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
}
/**
* [Exception] 서버에 정의되지 않은 모든 예외
*/
@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleAllException(Exception e) {
log.error("Handle Exception :", e);
final ErrorResponse response = ErrorResponse.of(ErrorCode.INTERNAL_SERVER_ERROR, e.getMessage());
return new ResponseEntity<>(response, HttpStatus.OK);
}
/**
* [Exception] 커스텀 예외 - MemberException
*/
@ExceptionHandler(MemberException.class)
public ResponseEntity<ErrorResponse> handleNotFoundException(MemberException e) {
log.error("Handle NotFoundException :", e);
final ErrorResponse response = ErrorResponse.of(e.getErrorCode(), e.getMessage());
return new ResponseEntity<>(response, HttpStatus.OK);
}
/**
* [Exception] 커스텀 예외 - VoucherException
*/
@ExceptionHandler(VoucherException.class)
public ResponseEntity<ErrorResponse> handlePermissionDeniedException(VoucherException e) {
log.error("Handle PermissionDeniedException :", e);
final ErrorResponse response = ErrorResponse.of(e.getErrorCode(), e.getMessage());
return new ResponseEntity<>(response, HttpStatus.OK);
}
/**
* [Exception] 커스텀 예외 - WalletException
*/
@ExceptionHandler(WalletException.class)
public ResponseEntity<ErrorResponse> handlePermissionDeniedException(WalletException e) {
log.error("Handle PermissionDeniedException :", e);
final ErrorResponse response = ErrorResponse.of(e.getErrorCode(), e.getMessage());
return new ResponseEntity<>(response, HttpStatus.OK);
}
}