From c346b6d60c87f5f2f63d7beef559599f4a48c3b3 Mon Sep 17 00:00:00 2001 From: devmizz Date: Sat, 1 Jun 2024 03:28:12 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20ExceptionHandlerFilter=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../filter/ExceptionHandlerFilter.java | 24 +++++++++++++++---- .../example/dto/request/SignUpRequest.java | 2 +- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/app/api/src/main/java/org/example/filter/ExceptionHandlerFilter.java b/app/api/src/main/java/org/example/filter/ExceptionHandlerFilter.java index 90796597..3c36499d 100644 --- a/app/api/src/main/java/org/example/filter/ExceptionHandlerFilter.java +++ b/app/api/src/main/java/org/example/filter/ExceptionHandlerFilter.java @@ -10,6 +10,8 @@ import java.io.IOException; import java.util.UUID; import lombok.extern.slf4j.Slf4j; +import org.example.exception.GlobalError; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.filter.OncePerRequestFilter; @@ -27,6 +29,11 @@ protected void doFilterInternal( filterChain.doFilter(request, response); } catch (BusinessException e) { String errorId = UUID.randomUUID().toString(); + ErrorResponse responseBody = ErrorResponse.businessErrorResponseBuilder() + .errorId(errorId) + .error(e.error) + .build(); + log.error(errorId, e); response.setStatus(e.error.getHttpStatus()); @@ -34,15 +41,24 @@ protected void doFilterInternal( response.getWriter().write( new ObjectMapper().writeValueAsString( - ErrorResponse.businessErrorResponseBuilder() - .errorId(errorId) - .error(e.error) - .build() + responseBody ) ); } catch (RuntimeException e) { String errorId = UUID.randomUUID().toString(); + ErrorResponse responseBody = ErrorResponse.businessErrorResponseBuilder() + .errorId(errorId) + .error(GlobalError.INTERNAL_SERVER_ERROR) + .build(); + log.error(errorId, e); + + response.setStatus(500); + response.setContentType("application/json; charset=UTF-8"); + + response.getWriter().write( + new ObjectMapper().writeValueAsString(responseBody) + ); } } } diff --git a/app/api/user-api/src/main/java/org/example/dto/request/SignUpRequest.java b/app/api/user-api/src/main/java/org/example/dto/request/SignUpRequest.java index 3dea6f18..06f97172 100644 --- a/app/api/user-api/src/main/java/org/example/dto/request/SignUpRequest.java +++ b/app/api/user-api/src/main/java/org/example/dto/request/SignUpRequest.java @@ -5,7 +5,7 @@ public record SignUpRequest( - @NotNull + @NotNull(message = "닉네임은 필수 입력값입니다.") String nickname ) {