Skip to content

Commit

Permalink
feat: 누락된 에러 분기 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
CChuYong committed May 22, 2024
1 parent 2847b8c commit 91def9b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ open class DefaultException(
class UnknownErrorException : DefaultException(ErrorCode.UNKNOWN_SERVER_ERROR)
class UnauthorizedException : DefaultException(ErrorCode.UNAUTHORIZED)
class InvalidInputException : DefaultException(ErrorCode.INVALID_INPUT)
class UnknownResourceException : DefaultException(ErrorCode.UNKNOWN_RESOURCE)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ enum class ErrorCode(
UNKNOWN_SERVER_ERROR("DE0001", "알 수 없는 오류가 발생했습니다"),
UNAUTHORIZED("DE0002", "인가되지 않은 접근입니다"),
INVALID_INPUT("DE0003", "입력값(바디 혹은 파라미터)가 누락되었습니다"),
UNKNOWN_RESOURCE("DE0004", "해당 리소스를 찾을 수 없습니다"),
INVALID_METHOD("DE0005", "요청 메서드가 잘못되었습니다. API 문서를 확인하세요"),

/**
* 인증(Authentication) 관련 오류
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.http.converter.HttpMessageNotReadableException
import org.springframework.security.access.AccessDeniedException
import org.springframework.web.HttpRequestMethodNotSupportedException
import org.springframework.web.bind.MethodArgumentNotValidException
import org.springframework.web.bind.annotation.ExceptionHandler
import org.springframework.web.bind.annotation.RestControllerAdvice
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException
import org.springframework.web.servlet.resource.NoResourceFoundException
import org.springframework.web.util.BindErrorUtils
import java.io.IOException

@RestControllerAdvice
class WebExceptionHandler {
Expand Down Expand Up @@ -56,14 +59,43 @@ class WebExceptionHandler {
)
}

@ExceptionHandler(
value = [
NoResourceFoundException::class,
HttpRequestMethodNotSupportedException::class,
]
)
fun handleUnknownResource(
exception: Exception,
): ResponseEntity<ErrorResponse> {
val errorCode = when(exception) {
is NoResourceFoundException -> ErrorCode.UNKNOWN_RESOURCE
is HttpRequestMethodNotSupportedException -> ErrorCode.INVALID_METHOD
else -> ErrorCode.UNKNOWN_SERVER_ERROR
}
return ResponseEntity
.status(HttpStatus.NOT_FOUND)
.body(
ErrorResponse.fromErrorCode(
errorCode = errorCode,
)
)
}

@ExceptionHandler(value = [Throwable::class])
fun handleUnhandledException(
exception: Throwable,
request: HttpServletRequest,
): ResponseEntity<ErrorResponse> {
if (exception is IOException) { //클라이언트에서 끊어버린 경우
return ResponseEntity
.internalServerError()
.build()
}

logger.error("[UnhandledException] " + exception.stackTraceToString())
return ResponseEntity
.badRequest()
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(ErrorResponse.fromErrorCode(ErrorCode.UNKNOWN_SERVER_ERROR))
}

Expand Down

0 comments on commit 91def9b

Please sign in to comment.