From 91def9bcf1ba3b033dec9b03c2a21f0f7fb6375b Mon Sep 17 00:00:00 2001 From: ChuYong Date: Wed, 22 May 2024 09:26:00 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EB=88=84=EB=9D=BD=EB=90=9C=20=EC=97=90?= =?UTF-8?q?=EB=9F=AC=20=EB=B6=84=EA=B8=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/exception/DefaultException.kt | 1 + .../makers/domain/exception/ErrorCode.kt | 2 ++ .../restapi/config/WebExceptionHandler.kt | 34 ++++++++++++++++++- 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/com/depromeet/makers/domain/exception/DefaultException.kt b/src/main/kotlin/com/depromeet/makers/domain/exception/DefaultException.kt index 7a64f77..074b3a6 100644 --- a/src/main/kotlin/com/depromeet/makers/domain/exception/DefaultException.kt +++ b/src/main/kotlin/com/depromeet/makers/domain/exception/DefaultException.kt @@ -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) diff --git a/src/main/kotlin/com/depromeet/makers/domain/exception/ErrorCode.kt b/src/main/kotlin/com/depromeet/makers/domain/exception/ErrorCode.kt index 871bdd6..ff1decc 100644 --- a/src/main/kotlin/com/depromeet/makers/domain/exception/ErrorCode.kt +++ b/src/main/kotlin/com/depromeet/makers/domain/exception/ErrorCode.kt @@ -11,6 +11,8 @@ enum class ErrorCode( UNKNOWN_SERVER_ERROR("DE0001", "알 수 없는 오류가 발생했습니다"), UNAUTHORIZED("DE0002", "인가되지 않은 접근입니다"), INVALID_INPUT("DE0003", "입력값(바디 혹은 파라미터)가 누락되었습니다"), + UNKNOWN_RESOURCE("DE0004", "해당 리소스를 찾을 수 없습니다"), + INVALID_METHOD("DE0005", "요청 메서드가 잘못되었습니다. API 문서를 확인하세요"), /** * 인증(Authentication) 관련 오류 diff --git a/src/main/kotlin/com/depromeet/makers/presentation/restapi/config/WebExceptionHandler.kt b/src/main/kotlin/com/depromeet/makers/presentation/restapi/config/WebExceptionHandler.kt index b993dfc..872fbb5 100644 --- a/src/main/kotlin/com/depromeet/makers/presentation/restapi/config/WebExceptionHandler.kt +++ b/src/main/kotlin/com/depromeet/makers/presentation/restapi/config/WebExceptionHandler.kt @@ -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 { @@ -56,14 +59,43 @@ class WebExceptionHandler { ) } + @ExceptionHandler( + value = [ + NoResourceFoundException::class, + HttpRequestMethodNotSupportedException::class, + ] + ) + fun handleUnknownResource( + exception: Exception, + ): ResponseEntity { + 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 { + 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)) }