-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomExceptionHandler.java
More file actions
51 lines (48 loc) · 2.48 KB
/
CustomExceptionHandler.java
File metadata and controls
51 lines (48 loc) · 2.48 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
package NextLevel.demo.exception;
import java.util.HashMap;
import java.util.Map;
import jakarta.validation.ConstraintViolationException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class CustomExceptionHandler {
@ExceptionHandler(CustomException.class)
public ResponseEntity<?> handleCustomException(CustomException e) {
e.printStackTrace();
Map<String, Object> map = new HashMap<>();
map.put("code", e.errorCode.CustomErrorCode);
map.put("message", e.getMessage());
return ResponseEntity.status(e.errorCode.statusCode).body(map);
}
@ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<?> handleValidationException(MethodArgumentNotValidException e) {
Map<String, Object> map = new HashMap<>();
map.put("code", ErrorCode.INPUT_REQUIRED_PARAMETER.CustomErrorCode);
map.put("message", ErrorCode.INPUT_REQUIRED_PARAMETER.errorMessage);
return ResponseEntity.status(ErrorCode.INPUT_REQUIRED_PARAMETER.statusCode).body(map);
}
@ExceptionHandler(ConstraintViolationException.class)
public ResponseEntity<?> handleValidationException(ConstraintViolationException e) {
Map<String, Object> map = new HashMap<>();
map.put("code", ErrorCode.INPUT_REQUIRED_PARAMETER.CustomErrorCode);
map.put("message", ErrorCode.INPUT_REQUIRED_PARAMETER.errorMessage);
return ResponseEntity.status(ErrorCode.INPUT_REQUIRED_PARAMETER.statusCode).body(map);
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public ResponseEntity<?> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED)
.body("요청 URL에 대한 HTTP 메소드가 올바르지 않습니다");
}
@ExceptionHandler(Exception.class)
public ResponseEntity<?> handleException(Exception e) {
e.printStackTrace();
Map<String, Object> map = new HashMap<>();
map.put("code", "05000");
map.put("message", e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(map);
}
}