refactor: WebLogMessageConverter에 예외 상황을 처리하는 별도의 메서드 추가 및 기존 ExceptionHandler 적용

This commit is contained in:
이상진 2025-10-07 16:08:25 +09:00
parent 6fa8c76b87
commit b22d587757
3 changed files with 40 additions and 5 deletions

View File

@ -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
)

View File

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

View File

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