Skip to content

Commit

Permalink
✨ 에러 핸들링 구현 (#21)
Browse files Browse the repository at this point in the history
* ✨ 에러 핸들링 구현

* ✨ 에러 핸들링 구현

* 🩹 리뷰 반영
  • Loading branch information
waterfogSW authored Oct 3, 2024
1 parent c0163b6 commit 40f3d4a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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<ErrorResponse> {
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<ErrorResponse> {
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<ErrorResponse> {
return ResponseEntity.status(status).body(response)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "인증 코드가 만료되었습니다.",
Expand Down
2 changes: 1 addition & 1 deletion openapi
Submodule openapi updated 1 files
+99 −24 openapi.yaml
Original file line number Diff line number Diff line change
@@ -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 {

Expand Down

0 comments on commit 40f3d4a

Please sign in to comment.