diff --git a/src/main/java/com/moabam/global/common/annotation/SlackNotification.java b/src/main/java/com/moabam/global/common/annotation/SlackNotification.java new file mode 100644 index 00000000..17863400 --- /dev/null +++ b/src/main/java/com/moabam/global/common/annotation/SlackNotification.java @@ -0,0 +1,12 @@ +package com.moabam.global.common.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target({ElementType.METHOD}) +@Retention(RetentionPolicy.RUNTIME) +public @interface SlackNotification { + +} diff --git a/src/main/java/com/moabam/global/common/util/SlackNotificationAspect.java b/src/main/java/com/moabam/global/common/util/SlackNotificationAspect.java new file mode 100644 index 00000000..e50f41c7 --- /dev/null +++ b/src/main/java/com/moabam/global/common/util/SlackNotificationAspect.java @@ -0,0 +1,29 @@ +package com.moabam.global.common.util; + +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Component; + +import com.moabam.api.infrastructure.slack.SlackService; + +import jakarta.servlet.http.HttpServletRequest; +import lombok.RequiredArgsConstructor; + +@Aspect +@Component +@Profile({"dev", "prod"}) +@RequiredArgsConstructor +public class SlackNotificationAspect { + + private final SlackService slackService; + + @Around("@annotation(com.moabam.global.common.annotation.SlackNotification) && args(request, exception)") + public Object sendSlack(ProceedingJoinPoint joinPoint, HttpServletRequest request, Exception exception) + throws Throwable { + slackService.send(request, exception); + + return joinPoint.proceed(); + } +} diff --git a/src/main/java/com/moabam/global/error/handler/GlobalExceptionHandler.java b/src/main/java/com/moabam/global/error/handler/GlobalExceptionHandler.java index 84570ec4..b753e30c 100644 --- a/src/main/java/com/moabam/global/error/handler/GlobalExceptionHandler.java +++ b/src/main/java/com/moabam/global/error/handler/GlobalExceptionHandler.java @@ -7,7 +7,6 @@ import java.util.Map; import java.util.Optional; -import org.springframework.core.annotation.Order; import org.springframework.http.HttpStatus; import org.springframework.validation.FieldError; import org.springframework.web.bind.MethodArgumentNotValidException; @@ -17,6 +16,7 @@ import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; import org.springframework.web.multipart.MaxUploadSizeExceededException; +import com.moabam.global.common.annotation.SlackNotification; import com.moabam.global.error.exception.BadRequestException; import com.moabam.global.error.exception.ConflictException; import com.moabam.global.error.exception.FcmException; @@ -27,10 +27,18 @@ import com.moabam.global.error.exception.UnauthorizedException; import com.moabam.global.error.model.ErrorResponse; -@Order(1) +import jakarta.servlet.http.HttpServletRequest; + @RestControllerAdvice public class GlobalExceptionHandler { + @SlackNotification + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) + @ExceptionHandler(Exception.class) + protected ErrorResponse handleException(HttpServletRequest request, Exception exception) { + return new ErrorResponse(exception.getMessage(), null); + } + @ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler(NotFoundException.class) protected ErrorResponse handleNotFoundException(MoabamException exception) { diff --git a/src/main/java/com/moabam/global/error/handler/SlackExceptionHandler.java b/src/main/java/com/moabam/global/error/handler/SlackExceptionHandler.java deleted file mode 100644 index d4f80ba6..00000000 --- a/src/main/java/com/moabam/global/error/handler/SlackExceptionHandler.java +++ /dev/null @@ -1,31 +0,0 @@ -package com.moabam.global.error.handler; - -import org.springframework.context.annotation.Profile; -import org.springframework.core.annotation.Order; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.ResponseStatus; -import org.springframework.web.bind.annotation.RestControllerAdvice; - -import com.moabam.api.infrastructure.slack.SlackService; -import com.moabam.global.error.model.ErrorResponse; - -import jakarta.servlet.http.HttpServletRequest; -import lombok.RequiredArgsConstructor; - -@Order(2) -@RestControllerAdvice -@Profile({"dev", "prod"}) -@RequiredArgsConstructor -public class SlackExceptionHandler { - - private final SlackService slackService; - - @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) - @ExceptionHandler(Exception.class) - protected ErrorResponse handleException(HttpServletRequest request, Exception exception) throws Exception { - slackService.send(request, exception); - - return new ErrorResponse(exception.getMessage(), null); - } -}