Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slack 메시지에 요청 url과 메서드 확인할 수 있도록 구현 #211

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.sentry.Sentry;
import jakarta.persistence.EntityExistsException;
import jakarta.persistence.EntityNotFoundException;
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -129,11 +130,11 @@ public ResponseEntity<ErrorResponse> handleAccessDeniedException(AccessDeniedExc
}

@ExceptionHandler(Exception.class)
public ResponseEntity<ErrorResponse> handleException(Exception e) {
public ResponseEntity<ErrorResponse> handleException(HttpServletRequest request, Exception e) {
ErrorResponse errorResponse = ErrorResponse.of(HttpStatus.INTERNAL_SERVER_ERROR,
e.getMessage());
log.error(e.getMessage(), e.fillInStackTrace());
slackClient.sendErrorMessage(e, Sentry.captureException(e));
slackClient.sendErrorMessage(e, Sentry.captureException(e), request);

return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.slack.api.model.block.element.ButtonElement;
import com.slack.api.model.block.element.ImageElement;
import io.sentry.protocol.SentryId;
import jakarta.servlet.http.HttpServletRequest;
import java.io.IOException;
import lombok.RequiredArgsConstructor;
import sixgaezzang.sidepeek.config.properties.SentryAlertProperties;
Expand All @@ -27,21 +28,22 @@ public class SlackClient {
private final SlackProperties slackProperties;
private final SentryAlertProperties sentryAlertProperties;

public void sendErrorMessage(Exception exception, SentryId sentryId) {
public void sendErrorMessage(Exception exception, SentryId sentryId, HttpServletRequest request) {
HeaderBlock headerBlock = header(header -> header
.text(plainText("🚨 " + getExceptionClassName(exception) + " 감지")));

SectionBlock messageSectionBlock = section(section -> section
.text(markdownText("*Environment:* " + sentryAlertProperties.environment()
+ "\n*Message:* " + exception.getMessage()
+ "\n*Sentry Issue ID:* " + sentryId + "\n"))
+ "\n*Request Method:* " + request.getMethod()
+ "\n*Request Path:* " + request.getRequestURI()
+ "\n*Error Message:* " + exception.getMessage()))
.accessory(ImageElement.builder()
.imageUrl(slackProperties.getErrorImageUrl())
.altText(slackProperties.getErrorImageAlt())
.build()));

SectionBlock linkButtonSectionBlock = section(section -> section
.text(markdownText("*Sentry Issue* 보러가기 \uD83D\uDC49"))
.text(markdownText("*Sentry Issue(ID: " + sentryId + ")* 보러가기 \uD83D\uDC49"))
.accessory(ButtonElement.builder()
.text(plainText("Click 👀"))
.value("sentry_issue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import static sixgaezzang.sidepeek.media.exception.message.MediaErrorMessage.FILE_IS_EMPTY;

import io.sentry.Sentry;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
Expand All @@ -16,13 +19,17 @@
import org.springframework.web.multipart.MultipartException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import sixgaezzang.sidepeek.common.exception.ErrorResponse;
import sixgaezzang.sidepeek.common.util.component.SlackClient;
import software.amazon.awssdk.services.s3.model.S3Exception;

@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE)
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
@Slf4j
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class MediaExceptionHandler {

private final SlackClient slackClient;

@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public ResponseEntity<ErrorResponse> handleHttpMediaTypeNotSupportedException(
HttpMediaTypeNotSupportedException e) {
Expand All @@ -45,11 +52,11 @@ public ResponseEntity<ErrorResponse> handleMultipartRequestExceptions(Exception
}

@ExceptionHandler(S3Exception.class)
public ResponseEntity<ErrorResponse> handleS3Exception(S3Exception e) {
public ResponseEntity<ErrorResponse> handleS3Exception(HttpServletRequest request, S3Exception e) {
ErrorResponse errorResponse = ErrorResponse.of(HttpStatus.BAD_GATEWAY,
CANNOT_READ_FILE);
log.error(e.getMessage(), e.fillInStackTrace());
Sentry.captureException(e);
slackClient.sendErrorMessage(e, Sentry.captureException(e), request);

return ResponseEntity
.status(HttpStatus.BAD_GATEWAY)
Expand Down
Loading