Skip to content

Commit 2d22b33

Browse files
committed
[BOOK-79] refactor: global-util - HttpRequestMethodNotSupportedException 추가 (#15)
1 parent b5856e1 commit 2d22b33

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

global-utils/src/main/kotlin/org/yapp/globalutils/exception/CommonErrorCode.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ enum class CommonErrorCode(
1616
BAD_REQUEST(HttpStatus.BAD_REQUEST, "COMMON_003", "BAD REQUEST"),
1717
INVALID_REQUEST(HttpStatus.BAD_REQUEST, "COMMON_004", "INVALID REQUEST"),
1818
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON_005", "INTERNAL SERVER ERROR"),
19-
MALFORMED_JSON(HttpStatus.BAD_REQUEST, "COMMON_006", "MALFORMED JSON");
19+
MALFORMED_JSON(HttpStatus.BAD_REQUEST, "COMMON_006", "MALFORMED JSON"),
20+
METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "COMMON_007", "METHOD NOT ALLOWED");
21+
2022

2123
override fun getHttpStatus(): HttpStatus = httpStatus
2224
override fun getCode(): String = code

global-utils/src/main/kotlin/org/yapp/globalutils/exception/GlobalExceptionHandler.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import mu.KotlinLogging
55
import org.springframework.http.ResponseEntity
66
import org.springframework.http.converter.HttpMessageNotReadableException
77
import org.springframework.validation.BindException
8+
import org.springframework.web.HttpRequestMethodNotSupportedException
89
import org.springframework.web.bind.MethodArgumentNotValidException
910
import org.springframework.web.bind.annotation.ExceptionHandler
1011
import org.springframework.web.bind.annotation.RestControllerAdvice
@@ -99,6 +100,24 @@ class GlobalExceptionHandler {
99100
return ResponseEntity(error, commonErrorCode.getHttpStatus())
100101
}
101102

103+
/**
104+
* 잘못된 HTTP 메서드로 인한 HttpRequestMethodNotSupportedException를 처리합니다.
105+
*/
106+
@ExceptionHandler(HttpRequestMethodNotSupportedException::class)
107+
protected fun handleHttpRequestMethodNotSupportedException(ex: HttpRequestMethodNotSupportedException): ResponseEntity<ErrorResponse> {
108+
val commonErrorCode = CommonErrorCode.METHOD_NOT_ALLOWED
109+
110+
log.warn { "HTTP method not supported: ${ex.method} for ${ex.supportedHttpMethods?.joinToString()}" }
111+
112+
val error = ErrorResponse.builder()
113+
.status(commonErrorCode.getHttpStatus().value())
114+
.message("HTTP method '${ex.method}' not supported for this endpoint. Supported methods: ${ex.supportedHttpMethods?.joinToString()}")
115+
.code(commonErrorCode.getCode())
116+
.build()
117+
118+
return ResponseEntity(error, commonErrorCode.getHttpStatus())
119+
}
120+
102121

103122
/**
104123
* 메서드 파라미터 검증 실패(@RequestParam, @PathVariable 등)

0 commit comments

Comments
 (0)