Skip to content

Commit

Permalink
refactor: SlackNotification 어노테이션 기반으로 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kmebin committed Dec 5, 2023
1 parent 2232a93 commit 29ef195
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -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 {

}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down

This file was deleted.

0 comments on commit 29ef195

Please sign in to comment.