From edc109efb53f5c13beecaa51fa075b5c2379a4e7 Mon Sep 17 00:00:00 2001 From: devxb Date: Mon, 28 Oct 2024 22:52:27 +0900 Subject: [PATCH] =?UTF-8?q?refactor:=20IllegalArgumentException=EC=9D=98?= =?UTF-8?q?=20=EA=B2=BD=EC=9A=B0=20=EC=82=AC=EC=9C=A0=EC=99=80=20=ED=95=A8?= =?UTF-8?q?=EA=BB=98=20400=20=EC=9D=91=EB=8B=B5=EC=9D=B4=20=EB=82=B4?= =?UTF-8?q?=EB=A0=A4=EA=B0=80=EB=8F=84=EB=A1=9D=20=EC=88=98=EC=A0=95?= =?UTF-8?q?=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gitanimals/shop/controller/BuySaleController.kt | 7 ++++++- .../shop/controller/response/ErrorResponse.kt | 11 +++++++++++ .../kotlin/org/gitanimals/shop/infra/RestRenderApi.kt | 11 ++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/main/kotlin/org/gitanimals/shop/controller/response/ErrorResponse.kt diff --git a/src/main/kotlin/org/gitanimals/shop/controller/BuySaleController.kt b/src/main/kotlin/org/gitanimals/shop/controller/BuySaleController.kt index 9e11ab6..ee029e6 100644 --- a/src/main/kotlin/org/gitanimals/shop/controller/BuySaleController.kt +++ b/src/main/kotlin/org/gitanimals/shop/controller/BuySaleController.kt @@ -1,11 +1,13 @@ package org.gitanimals.shop.controller import org.gitanimals.shop.app.BuyBackgroundFacade +import org.gitanimals.shop.controller.response.ErrorResponse import org.gitanimals.shop.controller.request.BuyBackgroundRequest import org.gitanimals.shop.controller.response.BackgroundResponse import org.gitanimals.shop.domain.SaleService import org.gitanimals.shop.domain.SaleType import org.springframework.http.HttpHeaders +import org.springframework.http.HttpStatus import org.springframework.web.bind.annotation.* @RestController @@ -18,7 +20,6 @@ class BuySaleController( fun getBackgrounds(): BackgroundResponse = BackgroundResponse.from(saleService.findAllByType(SaleType.BACKGROUND)) - @PostMapping("/shops/backgrounds") fun buyBackground( @RequestHeader(HttpHeaders.AUTHORIZATION) token: String, @@ -26,4 +27,8 @@ class BuySaleController( ) { buyBackgroundFacade.buyBackground(token, buyBackgroundRequest.type) } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler(IllegalArgumentException::class) + fun handleIllegalArgumentException(exception: IllegalArgumentException): ErrorResponse = ErrorResponse.from(exception) } diff --git a/src/main/kotlin/org/gitanimals/shop/controller/response/ErrorResponse.kt b/src/main/kotlin/org/gitanimals/shop/controller/response/ErrorResponse.kt new file mode 100644 index 0000000..7841643 --- /dev/null +++ b/src/main/kotlin/org/gitanimals/shop/controller/response/ErrorResponse.kt @@ -0,0 +1,11 @@ +package org.gitanimals.shop.controller.response + +data class ErrorResponse( + val message: String, +) { + + companion object { + fun from(exception: Exception): ErrorResponse = + ErrorResponse(exception.message ?: exception.localizedMessage) + } +} diff --git a/src/main/kotlin/org/gitanimals/shop/infra/RestRenderApi.kt b/src/main/kotlin/org/gitanimals/shop/infra/RestRenderApi.kt index c3a3205..a032f84 100644 --- a/src/main/kotlin/org/gitanimals/shop/infra/RestRenderApi.kt +++ b/src/main/kotlin/org/gitanimals/shop/infra/RestRenderApi.kt @@ -48,7 +48,12 @@ class RestRenderApi( .header(HttpHeaders.AUTHORIZATION, token) .header("Internal-Secret", internalSecret) .exchange { _, response -> - require(response.statusCode.is2xxSuccessful) { "Cannot add background by backgroundName: \"$backgroundName\"" } + if (response.statusCode.is4xxClientError) { + response.bodyTo(ErrorResponse::class.java)?.let { + throw IllegalArgumentException(it.message) + } + } + check(response.statusCode.is2xxSuccessful) { "Cannot add background by backgroundName: \"$backgroundName\"" } } } @@ -138,4 +143,8 @@ class RestRenderApi( val visible: Boolean, val dropRate: String, ) + + data class ErrorResponse( + val message: String, + ) }