|
23 | 23 | import org.springframework.web.servlet.NoHandlerFoundException; |
24 | 24 | import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; |
25 | 25 |
|
| 26 | +import lombok.RequiredArgsConstructor; |
26 | 27 | import lombok.extern.slf4j.Slf4j; |
| 28 | +import ssu.eatssu.domain.slack.entity.SlackMessageFormat; |
| 29 | +import ssu.eatssu.domain.slack.service.SlackService; |
27 | 30 | import ssu.eatssu.global.handler.response.BaseException; |
28 | 31 | import ssu.eatssu.global.handler.response.BaseResponse; |
29 | 32 | import ssu.eatssu.global.handler.response.BaseResponseStatus; |
|
33 | 36 | */ |
34 | 37 | @Slf4j |
35 | 38 | @RestControllerAdvice |
| 39 | +@RequiredArgsConstructor |
36 | 40 | public class GlobalExceptionHandler extends ResponseEntityExceptionHandler { |
| 41 | + private final SlackService slackService; |
37 | 42 |
|
38 | 43 | /** |
39 | 44 | * BaseException 처리 |
40 | 45 | */ |
41 | 46 | @ExceptionHandler(BaseException.class) |
42 | 47 | public ResponseEntity<BaseResponse<Void>> handleBaseException(BaseException e) { |
43 | 48 | log.info(e.getStatus().toString()); |
| 49 | + sendErrorToSlack(e); |
44 | 50 | return ResponseEntity.status(e.getStatus().getHttpStatus()).body(BaseResponse.fail(e.getStatus())); |
45 | 51 | } |
| 52 | + @ExceptionHandler(Exception.class) |
| 53 | + public ResponseEntity<BaseResponse<Void>> handleAllUnhandledException(Exception ex) { |
| 54 | + sendErrorToSlack(ex); |
| 55 | + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) |
| 56 | + .body(BaseResponse.fail(BaseResponseStatus.INTERNAL_SERVER_ERROR)); |
| 57 | + } |
| 58 | + |
46 | 59 |
|
47 | 60 | /** |
48 | 61 | * 경로는 있으나 지원하지 않는 http method로 요청 시 |
@@ -215,4 +228,35 @@ protected ResponseEntity<Object> handleHttpMessageNotWritable( |
215 | 228 | .body(BaseResponse.fail(BaseResponseStatus.INTERNAL_SERVER_ERROR)); |
216 | 229 | } |
217 | 230 |
|
| 231 | + @Override |
| 232 | + protected ResponseEntity<Object> handleExceptionInternal( |
| 233 | + @NonNull Exception ex, |
| 234 | + Object body, |
| 235 | + @NonNull HttpHeaders headers, |
| 236 | + @NonNull HttpStatusCode statusCode, |
| 237 | + @NonNull WebRequest request) { |
| 238 | + |
| 239 | + HttpStatus status = HttpStatus.valueOf(statusCode.value()); |
| 240 | + |
| 241 | + if (status.is4xxClientError() || status.is5xxServerError()) { |
| 242 | + sendErrorToSlack(ex); |
| 243 | + } |
| 244 | + |
| 245 | + BaseResponseStatus responseStatus = status.is4xxClientError() |
| 246 | + ? BaseResponseStatus.BAD_REQUEST |
| 247 | + : BaseResponseStatus.INTERNAL_SERVER_ERROR; |
| 248 | + |
| 249 | + return ResponseEntity.status(status).body(BaseResponse.fail(responseStatus)); |
| 250 | + } |
| 251 | + |
| 252 | + private void sendErrorToSlack(Exception ex) { |
| 253 | + try { |
| 254 | + String message = SlackMessageFormat.sendServerError(ex); |
| 255 | + slackService.sendSlackMessage(message, ssu.eatssu.domain.slack.entity.SlackChannel.SERVER_ERROR); |
| 256 | + } catch (Exception slackEx) { |
| 257 | + log.warn("슬랙 전송 실패: {}", slackEx.getMessage()); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + |
218 | 262 | } |
0 commit comments