-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGlobalExceptionHandler.java
56 lines (43 loc) · 1.83 KB
/
GlobalExceptionHandler.java
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
package ddangkong.controller.exception;
import ddangkong.service.excpetion.BusinessLogicException;
import ddangkong.service.excpetion.ViolateDataException;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
private static final String SERVER_ERROR_MESSAGE = "서버 오류가 발생했습니다. 관리자에게 문의하세요.";
@ExceptionHandler
public ErrorResponse handleBindingException(BindException e) {
log.warn(e.getMessage());
return new ErrorResponse(e.getBindingResult());
}
@ExceptionHandler
public ErrorResponse handleConstraintViolationException(ConstraintViolationException e) {
log.warn(e.getMessage());
return new ErrorResponse(e.getConstraintViolations());
}
@ExceptionHandler
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleBusinessLogicException(BusinessLogicException e) {
log.warn(e.getMessage());
return new ErrorResponse(e.getMessage());
}
@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleViolateDataException(ViolateDataException e) {
log.error(e.getMessage());
return new ErrorResponse(SERVER_ERROR_MESSAGE);
}
@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleException(Exception e) {
log.error(e.getMessage());
return new ErrorResponse(SERVER_ERROR_MESSAGE);
}
}