-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGlobalExceptionHandler.java
More file actions
83 lines (72 loc) · 4.4 KB
/
GlobalExceptionHandler.java
File metadata and controls
83 lines (72 loc) · 4.4 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
package kr.co.knuserver.global.handler;
import kr.co.knuserver.global.exception.BusinessException;
import kr.co.knuserver.global.exception.ApiResponse;
import kr.co.knuserver.global.exception.BusinessErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataAccessException;
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.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.resource.NoResourceFoundException;
import org.springframework.web.context.request.async.AsyncRequestNotUsableException;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<ApiResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
log.error("[400] Validation Failed: {}", e.getBindingResult().getAllErrors().getFirst().getDefaultMessage());
final ApiResponse response = ApiResponse.error(e);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
}
@ExceptionHandler(MethodArgumentTypeMismatchException.class)
protected ResponseEntity<ApiResponse> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
log.debug("[400] Type Mismatch: {}", e.getMessage());
final ApiResponse response = ApiResponse.error(BusinessErrorCode.INVALID_INPUT_VALUE);
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
}
@ExceptionHandler(ServletRequestBindingException.class)
protected ResponseEntity<ApiResponse> handleServletRequestBindingException(ServletRequestBindingException e) {
log.debug("[400] Invalid Access: {}", e.getMessage());
final ApiResponse response = ApiResponse.success("잘못된 접근입니다.");
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
}
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
protected ResponseEntity<ApiResponse> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
log.debug("[405] Method Not Allowed: {}", e.getMessage());
final ApiResponse response = ApiResponse.error(BusinessErrorCode.METHOD_NOT_ALLOWED);
return ResponseEntity.status(HttpStatus.METHOD_NOT_ALLOWED).body(response);
}
@ExceptionHandler(NoResourceFoundException.class)
protected ResponseEntity<ApiResponse> handleNoResourceFoundException(NoResourceFoundException e) {
log.debug("[404] No Resource Found: {}", e.getMessage());
final ApiResponse response = ApiResponse.error(BusinessErrorCode.BOOTH_NOT_FOUND);
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
}
@ExceptionHandler(AsyncRequestNotUsableException.class)
protected void handleAsyncRequestNotUsableException(AsyncRequestNotUsableException e) {
log.debug("[Async] 클라이언트 연결 끊김: {}", e.getMessage());
}
@ExceptionHandler(BusinessException.class)
protected ResponseEntity<ApiResponse> handleBusinessException(final BusinessException e) {
log.error("[{}] Business Exception: {}", e.getErrorCode().getStatus(), e.getMessage());
final ApiResponse response = ApiResponse.error(e.getErrorCode());
return ResponseEntity.status(e.getErrorCode().getStatus()).body(response);
}
@ExceptionHandler(DataAccessException.class)
protected ResponseEntity<ApiResponse> handleDataAccessException(DataAccessException e) {
log.error("[503] Data Access Exception", e);
final ApiResponse response = ApiResponse.error(BusinessErrorCode.REDIS_UNAVAILABLE);
return ResponseEntity.status(HttpStatus.SERVICE_UNAVAILABLE).body(response);
}
@ExceptionHandler(Exception.class)
protected ResponseEntity<ApiResponse> handleException(Exception e) {
log.error("[500] Internal Server Error", e);
final ApiResponse response = ApiResponse.error(e);
return new ResponseEntity<>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
}