-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControllerExceptionAdvice.java
More file actions
99 lines (85 loc) Β· 4.36 KB
/
ControllerExceptionAdvice.java
File metadata and controls
99 lines (85 loc) Β· 4.36 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
package org.runnect.server.common.advice;
import io.sentry.Sentry;
import java.util.Objects;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import lombok.RequiredArgsConstructor;
import org.runnect.server.common.constant.ErrorStatus;
import org.runnect.server.common.dto.ApiResponseDto;
import org.runnect.server.common.exception.BasicException;
import org.runnect.server.config.slack.SlackApi;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingRequestHeaderException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
@Component
@RequiredArgsConstructor
public class ControllerExceptionAdvice {
private final SlackApi slackApi;
/**
* 400 BAD_REQUEST
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MethodArgumentNotValidException.class)
protected ApiResponseDto handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
FieldError fieldError = Objects.requireNonNull(e.getFieldError());
return ApiResponseDto.error(ErrorStatus.VALIDATION_REQUEST_MISSING_EXCEPTION, String.format("%s. (%s)", fieldError.getDefaultMessage(), fieldError.getField()));
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingRequestHeaderException.class)
protected ApiResponseDto handleMissingRequestHeaderException(final MissingRequestHeaderException e) {
return ApiResponseDto.error(ErrorStatus.VALIDATION_REQUEST_HEADER_MISSING_EXCEPTION, String.format("%s. (%s)", ErrorStatus.VALIDATION_REQUEST_HEADER_MISSING_EXCEPTION.getMessage(), e.getHeaderName()));
}
// validation μ€ν¨μ 컀μ€ν
μλ¬ response
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
protected ApiResponseDto handleConstraintViolationException(final ConstraintViolationException e) {
return ApiResponseDto.error(ErrorStatus.VALIDATION_REQUEST_HEADER_MISSING_EXCEPTION, String.format("%s. (%s)", ErrorStatus.VALIDATION_REQUEST_HEADER_MISSING_EXCEPTION.getMessage(), e.getConstraintViolations()));
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(IllegalArgumentException.class)
protected ApiResponseDto handleConstraintViolationException(final IllegalArgumentException e) {
return ApiResponseDto.error(ErrorStatus.VALIDATION_REQUEST_HEADER_MISSING_EXCEPTION, String.format("%s. (%s)", ErrorStatus.INVALID_PARAMETER_EXCEPTION.getMessage(), e.getMessage()));
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestParameterException.class)
protected ApiResponseDto handleMissingRequestParameterException(final MissingServletRequestParameterException e) {
return ApiResponseDto.error(ErrorStatus.VALIDATION_REQUEST_PARAMETER_MISSING_EXCEPTION, String.format("%s. (%s)", ErrorStatus.VALIDATION_REQUEST_PARAMETER_MISSING_EXCEPTION.getMessage(), e.getParameterName()));
}
/**
* 500 Internal Server Error
*
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
protected ApiResponseDto<Object> handleException(final Exception error, final HttpServletRequest request) {
try {
slackApi.sendAlert(error, request);
} catch (Exception e) {
log.error("Slack μλ¦Ό μ μ‘ μ€ν¨", e);
}
try {
Sentry.captureException(error);
} catch (Exception e) {
log.error("Sentry μ μ‘ μ€ν¨", e);
}
return ApiResponseDto.error(ErrorStatus.INTERNAL_SERVER_ERROR);
}
/**
* custom error
*/
@ExceptionHandler(BasicException.class)
protected ResponseEntity<ApiResponseDto> handleBasicException(BasicException e) {
return ResponseEntity.status(e.getHttpStatus())
.body(ApiResponseDto.error(e.getErrorStatus(), e.getMessage()));
}
}