From b22d587757f8e8f23801dcdb8add22a58ce0bcfd Mon Sep 17 00:00:00 2001 From: pricelees Date: Tue, 7 Oct 2025 16:08:25 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20WebLogMessageConverter=EC=97=90=20?= =?UTF-8?q?=EC=98=88=EC=99=B8=20=EC=83=81=ED=99=A9=EC=9D=84=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=ED=95=98=EB=8A=94=20=EB=B3=84=EB=8F=84=EC=9D=98=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20?= =?UTF-8?q?=EA=B8=B0=EC=A1=B4=20ExceptionHandler=20=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/exception/GlobalExceptionhandler.kt | 7 ++---- .../web/support/log/WebLogMessageConverter.kt | 16 ++++++++++++++ .../support/log/WebLogMessageConverterTest.kt | 22 +++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/common/web/src/main/kotlin/com/sangdol/common/web/exception/GlobalExceptionhandler.kt b/common/web/src/main/kotlin/com/sangdol/common/web/exception/GlobalExceptionhandler.kt index a790a4d2..cab24523 100644 --- a/common/web/src/main/kotlin/com/sangdol/common/web/exception/GlobalExceptionhandler.kt +++ b/common/web/src/main/kotlin/com/sangdol/common/web/exception/GlobalExceptionhandler.kt @@ -1,6 +1,5 @@ package com.sangdol.common.web.exception -import com.sangdol.common.log.constant.LogType import com.sangdol.common.types.exception.CommonErrorCode import com.sangdol.common.types.exception.ErrorCode import com.sangdol.common.types.exception.RoomescapeException @@ -88,13 +87,11 @@ class GlobalExceptionHandler( errorResponse: CommonErrorResponse, exception: Exception ) { - val type = if (httpStatus.isClientError()) LogType.APPLICATION_FAILURE else LogType.UNHANDLED_EXCEPTION val actualException: Exception? = if (errorResponse.message == exception.message) null else exception - val logMessage = messageConverter.convertToResponseMessage( - type = type, + val logMessage = messageConverter.convertToErrorResponseMessage( servletRequest = servletRequest, - httpStatusCode = httpStatus.value(), + httpStatus = httpStatus, responseBody = errorResponse, exception = actualException ) diff --git a/common/web/src/main/kotlin/com/sangdol/common/web/support/log/WebLogMessageConverter.kt b/common/web/src/main/kotlin/com/sangdol/common/web/support/log/WebLogMessageConverter.kt index 48653a11..3e856dea 100644 --- a/common/web/src/main/kotlin/com/sangdol/common/web/support/log/WebLogMessageConverter.kt +++ b/common/web/src/main/kotlin/com/sangdol/common/web/support/log/WebLogMessageConverter.kt @@ -2,6 +2,7 @@ package com.sangdol.common.web.support.log import com.fasterxml.jackson.databind.ObjectMapper import com.sangdol.common.log.constant.LogType +import com.sangdol.common.types.web.HttpStatus import jakarta.servlet.http.HttpServletRequest class WebLogMessageConverter( @@ -49,4 +50,19 @@ class WebLogMessageConverter( return objectMapper.writeValueAsString(payload) } + + fun convertToErrorResponseMessage( + servletRequest: HttpServletRequest, + httpStatus: HttpStatus, + responseBody: Any? = null, + exception: Exception? = null, + ): String { + val type = if (httpStatus.isClientError()) { + LogType.APPLICATION_FAILURE + } else { + LogType.UNHANDLED_EXCEPTION + } + + return convertToResponseMessage(type, servletRequest, httpStatus.value(), responseBody, exception) + } } diff --git a/common/web/src/test/kotlin/com/sangdol/common/web/support/log/WebLogMessageConverterTest.kt b/common/web/src/test/kotlin/com/sangdol/common/web/support/log/WebLogMessageConverterTest.kt index 4c0951fb..613e104f 100644 --- a/common/web/src/test/kotlin/com/sangdol/common/web/support/log/WebLogMessageConverterTest.kt +++ b/common/web/src/test/kotlin/com/sangdol/common/web/support/log/WebLogMessageConverterTest.kt @@ -168,5 +168,27 @@ class WebLogMessageConverterTest : FunSpec({ this["exception"] shouldBe null } } + + test("4xx 에러가 발생하면 ${LogType.APPLICATION_FAILURE} 타입으로 변환한다.") { + val result = converter.convertToErrorResponseMessage( + servletRequest = servletRequest, + httpStatus = HttpStatus.BAD_REQUEST, + ) + + assertSoftly(objectMapper.readValue(result, LinkedHashMap::class.java)) { + this["type"] shouldBe LogType.APPLICATION_FAILURE.name + } + } + + test("5xx 에러가 발생하면 ${LogType.UNHANDLED_EXCEPTION} 타입으로 변환한다.") { + val result = converter.convertToErrorResponseMessage( + servletRequest = servletRequest, + httpStatus = HttpStatus.INTERNAL_SERVER_ERROR, + ) + + assertSoftly(objectMapper.readValue(result, LinkedHashMap::class.java)) { + this["type"] shouldBe LogType.UNHANDLED_EXCEPTION.name + } + } } })