Skip to content

Commit

Permalink
feat: CircuitBreaker fallback api에서 예외를 던지게 수정 QUZ-122
Browse files Browse the repository at this point in the history
  • Loading branch information
HMWG committed Dec 6, 2024
1 parent 12684cd commit eb68c43
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.grepp.quizy.api

import com.grepp.quizy.common.api.ApiResponse
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import com.grepp.quizy.exception.CustomCircuitBreakerException
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
Expand All @@ -11,42 +9,27 @@ import org.springframework.web.bind.annotation.RestController
@RequestMapping("/fallback")
class GatewayFallbackController {
@GetMapping("/user")
fun userServiceFallback(): ResponseEntity<ApiResponse<Unit>> {
return ResponseEntity
.status(HttpStatus.SERVICE_UNAVAILABLE)
.body(ApiResponse.error(HttpStatus.SERVICE_UNAVAILABLE.name, "User service is temporarily unavailable"))
fun userServiceFallback() {
throw CustomCircuitBreakerException.UserServiceUnavailableException
}

@GetMapping("/quiz")
fun quizServiceFallback(): ResponseEntity<ApiResponse<Unit>> {
return ResponseEntity
.status(HttpStatus.SERVICE_UNAVAILABLE)
.body(ApiResponse.error(HttpStatus.SERVICE_UNAVAILABLE.name, "Quiz service is temporarily unavailable"))
fun quizServiceFallback() {
throw CustomCircuitBreakerException.QuizServiceUnavailableException
}

@GetMapping("/game")
fun gameServiceFallback(): ResponseEntity<ApiResponse<Unit>> {
return ResponseEntity
.status(HttpStatus.SERVICE_UNAVAILABLE)
.body(ApiResponse.error(HttpStatus.SERVICE_UNAVAILABLE.name, "Game service is temporarily unavailable"))
fun gameServiceFallback() {
throw CustomCircuitBreakerException.GameServiceUnavailableException
}

@GetMapping("/ws")
fun webSocketServiceFallback(): ResponseEntity<ApiResponse<Unit>> {
return ResponseEntity
.status(HttpStatus.SERVICE_UNAVAILABLE)
.body(
ApiResponse.error(
HttpStatus.SERVICE_UNAVAILABLE.name,
"Game webSocket service is temporarily unavailable"
)
)
fun webSocketServiceFallback() {
throw CustomCircuitBreakerException.WsUnavailableException
}

@GetMapping("/matching")
fun matchingServiceFallback(): ResponseEntity<ApiResponse<Unit>> {
return ResponseEntity
.status(HttpStatus.SERVICE_UNAVAILABLE)
.body(ApiResponse.error(HttpStatus.SERVICE_UNAVAILABLE.name, "Matching service is temporarily unavailable"))
fun matchingServiceFallback() {
throw CustomCircuitBreakerException.MatchingServiceUnavailableException
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.grepp.quizy.exception

import com.grepp.quizy.common.exception.BaseErrorCode
import com.grepp.quizy.common.exception.ErrorReason
import org.springframework.http.HttpStatus

enum class CircuitBreakerErrorCode(
private val status: Int,
private val errorCode: String,
private val message: String,
) : BaseErrorCode {

GAME_UNAVAILABLE(HttpStatus.SERVICE_UNAVAILABLE.value(), "CIRCUIT_BREAKER_503", "게임 서비스를 이용할 수 없습니다."),
MATCHING_UNAVAILABLE(HttpStatus.SERVICE_UNAVAILABLE.value(), "CIRCUIT_BREAKER_503", "매칭 서비스를 이용할 수 없습니다."),
QUIZ_UNAVAILABLE(HttpStatus.SERVICE_UNAVAILABLE.value(), "CIRCUIT_BREAKER_503", "퀴즈 서비스를 이용할 수 없습니다."),
USER_UNAVAILABLE(HttpStatus.SERVICE_UNAVAILABLE.value(), "CIRCUIT_BREAKER_503", "사용자 서비스를 이용할 수 없습니다."),
WS_UNAVAILABLE(HttpStatus.SERVICE_UNAVAILABLE.value(), "CIRCUIT_BREAKER_503", "웹소켓 서비스를 이용할 수 없습니다."),
;

override val errorReason: ErrorReason
get() = ErrorReason(status, errorCode, message)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.grepp.quizy.exception

import com.grepp.quizy.common.exception.WebException

sealed class CustomCircuitBreakerException(errorCode: CircuitBreakerErrorCode) : WebException(errorCode) {
data object GameServiceUnavailableException :
CustomCircuitBreakerException(CircuitBreakerErrorCode.GAME_UNAVAILABLE) {
private fun readResolve(): Any = GameServiceUnavailableException

val EXCEPTION: CustomCircuitBreakerException = GameServiceUnavailableException
}

data object UserServiceUnavailableException :
CustomCircuitBreakerException(CircuitBreakerErrorCode.USER_UNAVAILABLE) {
private fun readResolve(): Any = UserServiceUnavailableException

val EXCEPTION: CustomCircuitBreakerException = UserServiceUnavailableException
}

data object QuizServiceUnavailableException :
CustomCircuitBreakerException(CircuitBreakerErrorCode.QUIZ_UNAVAILABLE) {
private fun readResolve(): Any = QuizServiceUnavailableException

val EXCEPTION: CustomCircuitBreakerException = QuizServiceUnavailableException
}

data object MatchingServiceUnavailableException :
CustomCircuitBreakerException(CircuitBreakerErrorCode.MATCHING_UNAVAILABLE) {
private fun readResolve(): Any = MatchingServiceUnavailableException

val EXCEPTION: CustomCircuitBreakerException = MatchingServiceUnavailableException
}

data object WsUnavailableException :
CustomCircuitBreakerException(CircuitBreakerErrorCode.WS_UNAVAILABLE) {
private fun readResolve(): Any = WsUnavailableException

val EXCEPTION: CustomCircuitBreakerException = WsUnavailableException
}
}


0 comments on commit eb68c43

Please sign in to comment.