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 + } + } } })