Skip to content

Commit

Permalink
RAC-449 feat : 500에러 탐지시 슬랙 발송 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ywj9811 committed Oct 23, 2024
1 parent 88fe559 commit 99dd2b9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.postgraduate.global.constant.ErrorCode;
import com.postgraduate.global.dto.ErrorResponse;
import com.postgraduate.global.dto.ResponseDto;
import com.postgraduate.global.slack.SlackErrorMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
Expand All @@ -17,6 +18,7 @@
@RequiredArgsConstructor
public class GlobalExceptionHandler {
private static final String LOG_FORMAT = "Code : {}, Message : {}";
private final SlackErrorMessage slackErrorMessage;

@ExceptionHandler(ApplicationException.class)
public ResponseEntity<ResponseDto<ErrorResponse>> handleApplicationException(ApplicationException ex) {
Expand All @@ -34,6 +36,7 @@ public ResponseEntity<ResponseDto<ErrorResponse>> handleArgumentValidException(M
public ResponseEntity<ResponseDto<ErrorResponse>> handleInternalServerException(Exception ex) {
log.error(LOG_FORMAT, "500", ex.getStackTrace());
log.error("errorMessage : {}", ex.getMessage());
slackErrorMessage.sendSlackServerError(ex);
return ResponseEntity.internalServerError().build();
}
}
28 changes: 26 additions & 2 deletions src/main/java/com/postgraduate/global/slack/SlackErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

import static com.postgraduate.global.slack.SlackUtils.generateSlackField;
Expand Down Expand Up @@ -63,6 +64,19 @@ public void sendSlackBizppurioError(String phoneNumber) {
}
}

public void sendSlackServerError(Exception ex) {
try {
slackClient.send(logWebHookUrl, Payload.builder()
.text("알림톡 발송 실패! 확인 요망!!")
.attachments(
List.of(generateServerErrorSlackAttachMent(ex))
)
.build());
} catch (IOException e) {
log.error("slack 전송 오류");
}
}

private Attachment generateMentoringErrorSlackAttachment(Long mentoringId, Throwable ex) {
String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS").format(LocalDateTime.now());
return Attachment.builder()
Expand All @@ -74,7 +88,6 @@ private Attachment generateMentoringErrorSlackAttachment(Long mentoringId, Throw
))
.build();
}

private Attachment generateSalaryErrorSlackAttachment(Long seniorId, Throwable ex) {
String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS").format(LocalDateTime.now());
return Attachment.builder()
Expand All @@ -86,7 +99,6 @@ private Attachment generateSalaryErrorSlackAttachment(Long seniorId, Throwable e
))
.build();
}

private Attachment generateBizppurioErrorSlackAttachment(String phoneNumber) {
String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS").format(LocalDateTime.now());
return Attachment.builder()
Expand All @@ -97,4 +109,16 @@ private Attachment generateBizppurioErrorSlackAttachment(String phoneNumber) {
))
.build();
}

private Attachment generateServerErrorSlackAttachMent(Exception ex) {
String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:SS").format(LocalDateTime.now());
return Attachment.builder()
.color("ff0000")
.title(requestTime + "에 발생한 500 에러")
.fields(List.of(
generateSlackField("500 에러 발생", ex.getMessage()),
generateSlackField("StackTrace", Arrays.toString(ex.getStackTrace()))
))
.build();
}
}

0 comments on commit 99dd2b9

Please sign in to comment.