예외 메시지 다국어 처리를 위한 키 기반 구조 도입
This commit is contained in:
@@ -1,5 +1,12 @@
|
||||
package kr.co.vividnext.sodalive.common
|
||||
|
||||
class SodaException(message: String, val errorProperty: String? = null) : RuntimeException(message)
|
||||
class SodaException(
|
||||
message: String? = null,
|
||||
val errorProperty: String? = null,
|
||||
val messageKey: String? = null
|
||||
) : RuntimeException(message)
|
||||
|
||||
class AdsChargeException(message: String) : RuntimeException(message)
|
||||
class AdsChargeException(
|
||||
message: String? = null,
|
||||
val messageKey: String? = null
|
||||
) : RuntimeException(message)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package kr.co.vividnext.sodalive.common
|
||||
|
||||
import kr.co.vividnext.sodalive.i18n.LangContext
|
||||
import kr.co.vividnext.sodalive.i18n.SodaMessageSource
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.springframework.dao.DataIntegrityViolationException
|
||||
import org.springframework.http.HttpStatus
|
||||
@@ -13,14 +15,20 @@ import org.springframework.web.multipart.MaxUploadSizeExceededException
|
||||
import org.springframework.web.server.ResponseStatusException
|
||||
|
||||
@RestControllerAdvice
|
||||
class SodaExceptionHandler {
|
||||
class SodaExceptionHandler(
|
||||
private val langContext: LangContext,
|
||||
private val messageSource: SodaMessageSource
|
||||
) {
|
||||
private val logger = LoggerFactory.getLogger(this::class.java)
|
||||
|
||||
@ExceptionHandler(SodaException::class)
|
||||
fun handleSodaException(e: SodaException) = run {
|
||||
logger.error("API error", e)
|
||||
val message = e.messageKey?.takeIf { it.isNotBlank() }?.let { messageSource.getMessage(it, langContext.lang) }
|
||||
?: e.message?.takeIf { it.isNotBlank() }
|
||||
?: messageSource.getMessage("common.error.unknown", langContext.lang)
|
||||
ApiResponse.error(
|
||||
message = e.message,
|
||||
message = message,
|
||||
errorProperty = e.errorProperty
|
||||
)
|
||||
}
|
||||
@@ -28,44 +36,53 @@ class SodaExceptionHandler {
|
||||
@ExceptionHandler(MaxUploadSizeExceededException::class)
|
||||
fun handleMaxUploadSizeExceededException(e: MaxUploadSizeExceededException) = run {
|
||||
logger.error("API error", e)
|
||||
ApiResponse.error(message = "파일용량은 최대 1024MB까지 저장할 수 있습니다.")
|
||||
val message = messageSource.getMessage("common.error.max_upload_size", langContext.lang)
|
||||
ApiResponse.error(message = message)
|
||||
}
|
||||
|
||||
@ExceptionHandler(AccessDeniedException::class)
|
||||
fun handleAccessDeniedException(e: AccessDeniedException) = run {
|
||||
logger.error("API error", e)
|
||||
ApiResponse.error(message = "권한이 없습니다.")
|
||||
val message = messageSource.getMessage("common.error.access_denied", langContext.lang)
|
||||
ApiResponse.error(message = message)
|
||||
}
|
||||
|
||||
@ExceptionHandler(InternalAuthenticationServiceException::class)
|
||||
fun handleInternalAuthenticationServiceException(e: InternalAuthenticationServiceException) = run {
|
||||
logger.error("API error", e)
|
||||
ApiResponse.error("로그인 정보를 확인해주세요.")
|
||||
val message = messageSource.getMessage("common.error.bad_credentials", langContext.lang)
|
||||
ApiResponse.error(message)
|
||||
}
|
||||
|
||||
@ExceptionHandler(BadCredentialsException::class)
|
||||
fun handleBadCredentialsException(e: BadCredentialsException) = run {
|
||||
logger.error("API error", e)
|
||||
ApiResponse.error("로그인 정보를 확인해주세요.")
|
||||
val message = messageSource.getMessage("common.error.bad_credentials", langContext.lang)
|
||||
ApiResponse.error(message)
|
||||
}
|
||||
|
||||
@ExceptionHandler(DataIntegrityViolationException::class)
|
||||
fun handleDataIntegrityViolationException(e: DataIntegrityViolationException) = run {
|
||||
logger.error("API error", e)
|
||||
ApiResponse.error("이미 등록되어 있습니다.")
|
||||
val message = messageSource.getMessage("common.error.already_registered", langContext.lang)
|
||||
ApiResponse.error(message)
|
||||
}
|
||||
|
||||
@ResponseStatus(value = HttpStatus.NOT_FOUND)
|
||||
@ExceptionHandler(AdsChargeException::class)
|
||||
fun handleAdsChargeException(e: AdsChargeException) = run {
|
||||
logger.error("API error - AdsChargeException ::: ", e)
|
||||
ApiResponse.error("잘못된 요청입니다.")
|
||||
val message = e.messageKey?.takeIf { it.isNotBlank() }?.let { messageSource.getMessage(it, langContext.lang) }
|
||||
?: e.message?.takeIf { it.isNotBlank() }
|
||||
?: messageSource.getMessage("common.error.invalid_request", langContext.lang)
|
||||
ApiResponse.error(message)
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception::class)
|
||||
fun handleException(e: Exception) = run {
|
||||
if (e is ResponseStatusException) throw e
|
||||
logger.error("API error", e)
|
||||
ApiResponse.error("알 수 없는 오류가 발생했습니다. 다시 시도해 주세요.")
|
||||
val message = messageSource.getMessage("common.error.unknown", langContext.lang)
|
||||
ApiResponse.error(message)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user