From d4b5682b45241305f1db3a844473d694485385ee Mon Sep 17 00:00:00 2001 From: kmebin Date: Mon, 4 Dec 2023 01:59:50 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20log=20AOP=20=EC=A0=9C=EA=B1=B0=20?= =?UTF-8?q?=EB=B0=8F=20SlackExceptionHandler=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../moabam/global/common/util/LogAspect.java | 28 ---------------- .../error/handler/GlobalExceptionHandler.java | 32 +++++++++---------- .../error/handler/SlackExceptionHandler.java | 9 ++++-- 3 files changed, 23 insertions(+), 46 deletions(-) delete mode 100644 src/main/java/com/moabam/global/common/util/LogAspect.java diff --git a/src/main/java/com/moabam/global/common/util/LogAspect.java b/src/main/java/com/moabam/global/common/util/LogAspect.java deleted file mode 100644 index 54b0eddc..00000000 --- a/src/main/java/com/moabam/global/common/util/LogAspect.java +++ /dev/null @@ -1,28 +0,0 @@ -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.stereotype.Component; - -import com.moabam.global.error.exception.UnauthorizedException; - -import lombok.extern.slf4j.Slf4j; - -@Aspect -@Slf4j -@Component -public class LogAspect { - - @Around("execution(* com.moabam.global.error.handler.GlobalExceptionHandler.*(..))") - public Object printExceptionLog(ProceedingJoinPoint joinPoint) throws Throwable { - Object[] args = joinPoint.getArgs(); - Exception exception = (Exception)args[0]; - - if (!(exception instanceof UnauthorizedException)) { - log.error("===== EXCEPTION LOG =====", 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 1b2c6540..766a7e01 100644 --- a/src/main/java/com/moabam/global/error/handler/GlobalExceptionHandler.java +++ b/src/main/java/com/moabam/global/error/handler/GlobalExceptionHandler.java @@ -31,32 +31,32 @@ public class GlobalExceptionHandler { @ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler(NotFoundException.class) - protected ErrorResponse handleNotFoundException(MoabamException moabamException) { - return new ErrorResponse(moabamException.getMessage(), null); + protected ErrorResponse handleNotFoundException(MoabamException exception) { + return new ErrorResponse(exception.getMessage(), null); } @ResponseStatus(HttpStatus.UNAUTHORIZED) @ExceptionHandler(UnauthorizedException.class) - protected ErrorResponse handleUnauthorizedException(MoabamException moabamException) { - return new ErrorResponse(moabamException.getMessage(), null); + protected ErrorResponse handleUnauthorizedException(MoabamException exception) { + return new ErrorResponse(exception.getMessage(), null); } @ResponseStatus(HttpStatus.NOT_FOUND) @ExceptionHandler(ForbiddenException.class) - protected ErrorResponse handleForbiddenException(MoabamException moabamException) { - return new ErrorResponse(moabamException.getMessage(), null); + protected ErrorResponse handleForbiddenException(MoabamException exception) { + return new ErrorResponse(exception.getMessage(), null); } @ResponseStatus(HttpStatus.CONFLICT) @ExceptionHandler(ConflictException.class) - protected ErrorResponse handleConflictException(MoabamException moabamException) { - return new ErrorResponse(moabamException.getMessage(), null); + protected ErrorResponse handleConflictException(MoabamException exception) { + return new ErrorResponse(exception.getMessage(), null); } @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(BadRequestException.class) - protected ErrorResponse handleBadRequestException(MoabamException moabamException) { - return new ErrorResponse(moabamException.getMessage(), null); + protected ErrorResponse handleBadRequestException(MoabamException exception) { + return new ErrorResponse(exception.getMessage(), null); } @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @@ -64,14 +64,14 @@ protected ErrorResponse handleBadRequestException(MoabamException moabamExceptio FcmException.class, TossPaymentException.class }) - protected ErrorResponse handleFcmException(MoabamException moabamException) { - return new ErrorResponse(moabamException.getMessage(), null); + protected ErrorResponse handleFcmException(MoabamException exception) { + return new ErrorResponse(exception.getMessage(), null); } @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MoabamException.class) - protected ErrorResponse handleMoabamException(MoabamException moabamException) { - return new ErrorResponse(moabamException.getMessage(), null); + protected ErrorResponse handleMoabamException(MoabamException exception) { + return new ErrorResponse(exception.getMessage(), null); } @ResponseStatus(HttpStatus.BAD_REQUEST) @@ -95,7 +95,7 @@ protected ErrorResponse handleMethodArgumentNotValidException(MethodArgumentNotV @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MethodArgumentTypeMismatchException.class) - public ErrorResponse handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException exception) { + protected ErrorResponse handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException exception) { String typeName = Optional.ofNullable(exception.getRequiredType()) .map(Class::getSimpleName) .orElse(""); @@ -106,7 +106,7 @@ public ErrorResponse handleMethodArgumentTypeMismatchException(MethodArgumentTyp @ExceptionHandler(MaxUploadSizeExceededException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) - public ErrorResponse handleMaxSizeException(MaxUploadSizeExceededException exception) { + protected ErrorResponse handleMaxSizeException(MaxUploadSizeExceededException exception) { String message = String.format(S3_INVALID_IMAGE_SIZE.getMessage()); return new ErrorResponse(message, null); diff --git a/src/main/java/com/moabam/global/error/handler/SlackExceptionHandler.java b/src/main/java/com/moabam/global/error/handler/SlackExceptionHandler.java index 3751dc5f..b144c62b 100644 --- a/src/main/java/com/moabam/global/error/handler/SlackExceptionHandler.java +++ b/src/main/java/com/moabam/global/error/handler/SlackExceptionHandler.java @@ -1,10 +1,13 @@ package com.moabam.global.error.handler; import org.springframework.context.annotation.Profile; +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; @@ -16,9 +19,11 @@ public class SlackExceptionHandler { private final SlackService slackService; + @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Exception.class) - void handleException(HttpServletRequest request, Exception exception) throws Exception { + protected ErrorResponse handleException(HttpServletRequest request, Exception exception) throws Exception { slackService.send(request, exception); - throw exception; + + return new ErrorResponse(exception.getMessage(), null); } }