diff --git a/bootstrap/api/src/main/kotlin/com/threedays/bootstrap/api/support/ControllerAdvice.kt b/bootstrap/api/src/main/kotlin/com/threedays/bootstrap/api/support/ControllerAdvice.kt new file mode 100644 index 0000000..d4a2b3f --- /dev/null +++ b/bootstrap/api/src/main/kotlin/com/threedays/bootstrap/api/support/ControllerAdvice.kt @@ -0,0 +1,67 @@ +package com.threedays.bootstrap.api.support + +import com.threedays.oas.model.ErrorResponse +import com.threedays.support.common.base.exception.CustomException +import com.threedays.support.common.exception.NotFoundException +import io.github.oshai.kotlinlogging.KotlinLogging +import org.springframework.http.HttpStatus +import org.springframework.http.ResponseEntity +import org.springframework.web.bind.annotation.ExceptionHandler +import org.springframework.web.bind.annotation.RestControllerAdvice +import java.time.OffsetDateTime + +@RestControllerAdvice +class ControllerAdvice { + + companion object { + + private val logger = KotlinLogging.logger {} + private const val COMMON_ERROR_TYPE = "COMMON" + private const val ILLEGAL_ARGUMENT_ERROR_CODE = "1001" + private const val NOT_FOUND_ERROR_CODE = "1002" + private const val INTERNAL_SERVER_ERROR_CODE = "1003" + + } + + @ExceptionHandler(CustomException::class) + fun handleCustomException(e: CustomException): ResponseEntity { + logger.error(e) { "CustomException" } + + val response: ErrorResponse = createErrorResponse(e.type, e.code) + + return createResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR, response) + } + + @ExceptionHandler(Exception::class) + fun handleException(e: Exception): ResponseEntity { + logger.error(e) { e::class.simpleName } + + val (status: HttpStatus, errorCode: String) = when (e) { + is IllegalArgumentException -> HttpStatus.BAD_REQUEST to ILLEGAL_ARGUMENT_ERROR_CODE + is NotFoundException -> HttpStatus.NOT_FOUND to NOT_FOUND_ERROR_CODE + else -> HttpStatus.INTERNAL_SERVER_ERROR to INTERNAL_SERVER_ERROR_CODE + } + + val response: ErrorResponse = createErrorResponse(COMMON_ERROR_TYPE, errorCode) + return createResponseEntity(status, response) + } + + private fun createErrorResponse( + type: String, + code: String + ): ErrorResponse { + return ErrorResponse( + time = OffsetDateTime.now(), + type = type, + code = code + ) + } + + private fun createResponseEntity( + status: HttpStatus, + response: ErrorResponse + ): ResponseEntity { + return ResponseEntity.status(status).body(response) + } + +} diff --git a/domain/src/main/kotlin/com/threedays/domain/auth/exception/AuthException.kt b/domain/src/main/kotlin/com/threedays/domain/auth/exception/AuthException.kt index 264a709..506a7d8 100644 --- a/domain/src/main/kotlin/com/threedays/domain/auth/exception/AuthException.kt +++ b/domain/src/main/kotlin/com/threedays/domain/auth/exception/AuthException.kt @@ -5,7 +5,7 @@ import com.threedays.support.common.base.exception.CustomException sealed class AuthException( codeNumber: Int, override val message: String = DEFAULT_MESSAGE, -) : CustomException("Auth", codeNumber, message) { +) : CustomException("AUTH", codeNumber, message) { data class AuthCodeExpiredException( override val message: String = "인증 코드가 만료되었습니다.", diff --git a/openapi b/openapi index 88f3df7..4982296 160000 --- a/openapi +++ b/openapi @@ -1 +1 @@ -Subproject commit 88f3df7e98a50f36e976683956d72681ac54d0d9 +Subproject commit 4982296c3a4915e5c5dbd6032908e92acec235e3 diff --git a/support/common/src/main/kotlin/com/threedays/support/common/base/exception/CustomException.kt b/support/common/src/main/kotlin/com/threedays/support/common/base/exception/CustomException.kt index 24d82d1..af96669 100644 --- a/support/common/src/main/kotlin/com/threedays/support/common/base/exception/CustomException.kt +++ b/support/common/src/main/kotlin/com/threedays/support/common/base/exception/CustomException.kt @@ -1,14 +1,13 @@ package com.threedays.support.common.base.exception abstract class CustomException( - codePrefix: String = DEFAULT_CODE_PREFIX, + val type: String = DEFAULT_CODE_PREFIX, codeNumber: Int, override val message: String = DEFAULT_MESSAGE, ) : RuntimeException(message) { - val code: String = "$codePrefix-${ + val code: String = codeNumber.toString().padStart(DEFAULT_CODE_NUMBER_LENGTH, DEFAULT_CODE_NUMBER_PAD_CHAR) - }" companion object {