Skip to content

Commit eb68c43

Browse files
committed
feat: CircuitBreaker fallback api에서 예외를 던지게 수정 QUZ-122
1 parent 12684cd commit eb68c43

File tree

3 files changed

+75
-28
lines changed

3 files changed

+75
-28
lines changed
Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package com.grepp.quizy.api
22

3-
import com.grepp.quizy.common.api.ApiResponse
4-
import org.springframework.http.HttpStatus
5-
import org.springframework.http.ResponseEntity
3+
import com.grepp.quizy.exception.CustomCircuitBreakerException
64
import org.springframework.web.bind.annotation.GetMapping
75
import org.springframework.web.bind.annotation.RequestMapping
86
import org.springframework.web.bind.annotation.RestController
@@ -11,42 +9,27 @@ import org.springframework.web.bind.annotation.RestController
119
@RequestMapping("/fallback")
1210
class GatewayFallbackController {
1311
@GetMapping("/user")
14-
fun userServiceFallback(): ResponseEntity<ApiResponse<Unit>> {
15-
return ResponseEntity
16-
.status(HttpStatus.SERVICE_UNAVAILABLE)
17-
.body(ApiResponse.error(HttpStatus.SERVICE_UNAVAILABLE.name, "User service is temporarily unavailable"))
12+
fun userServiceFallback() {
13+
throw CustomCircuitBreakerException.UserServiceUnavailableException
1814
}
1915

2016
@GetMapping("/quiz")
21-
fun quizServiceFallback(): ResponseEntity<ApiResponse<Unit>> {
22-
return ResponseEntity
23-
.status(HttpStatus.SERVICE_UNAVAILABLE)
24-
.body(ApiResponse.error(HttpStatus.SERVICE_UNAVAILABLE.name, "Quiz service is temporarily unavailable"))
17+
fun quizServiceFallback() {
18+
throw CustomCircuitBreakerException.QuizServiceUnavailableException
2519
}
2620

2721
@GetMapping("/game")
28-
fun gameServiceFallback(): ResponseEntity<ApiResponse<Unit>> {
29-
return ResponseEntity
30-
.status(HttpStatus.SERVICE_UNAVAILABLE)
31-
.body(ApiResponse.error(HttpStatus.SERVICE_UNAVAILABLE.name, "Game service is temporarily unavailable"))
22+
fun gameServiceFallback() {
23+
throw CustomCircuitBreakerException.GameServiceUnavailableException
3224
}
3325

3426
@GetMapping("/ws")
35-
fun webSocketServiceFallback(): ResponseEntity<ApiResponse<Unit>> {
36-
return ResponseEntity
37-
.status(HttpStatus.SERVICE_UNAVAILABLE)
38-
.body(
39-
ApiResponse.error(
40-
HttpStatus.SERVICE_UNAVAILABLE.name,
41-
"Game webSocket service is temporarily unavailable"
42-
)
43-
)
27+
fun webSocketServiceFallback() {
28+
throw CustomCircuitBreakerException.WsUnavailableException
4429
}
4530

4631
@GetMapping("/matching")
47-
fun matchingServiceFallback(): ResponseEntity<ApiResponse<Unit>> {
48-
return ResponseEntity
49-
.status(HttpStatus.SERVICE_UNAVAILABLE)
50-
.body(ApiResponse.error(HttpStatus.SERVICE_UNAVAILABLE.name, "Matching service is temporarily unavailable"))
32+
fun matchingServiceFallback() {
33+
throw CustomCircuitBreakerException.MatchingServiceUnavailableException
5134
}
5235
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.grepp.quizy.exception
2+
3+
import com.grepp.quizy.common.exception.BaseErrorCode
4+
import com.grepp.quizy.common.exception.ErrorReason
5+
import org.springframework.http.HttpStatus
6+
7+
enum class CircuitBreakerErrorCode(
8+
private val status: Int,
9+
private val errorCode: String,
10+
private val message: String,
11+
) : BaseErrorCode {
12+
13+
GAME_UNAVAILABLE(HttpStatus.SERVICE_UNAVAILABLE.value(), "CIRCUIT_BREAKER_503", "게임 서비스를 이용할 수 없습니다."),
14+
MATCHING_UNAVAILABLE(HttpStatus.SERVICE_UNAVAILABLE.value(), "CIRCUIT_BREAKER_503", "매칭 서비스를 이용할 수 없습니다."),
15+
QUIZ_UNAVAILABLE(HttpStatus.SERVICE_UNAVAILABLE.value(), "CIRCUIT_BREAKER_503", "퀴즈 서비스를 이용할 수 없습니다."),
16+
USER_UNAVAILABLE(HttpStatus.SERVICE_UNAVAILABLE.value(), "CIRCUIT_BREAKER_503", "사용자 서비스를 이용할 수 없습니다."),
17+
WS_UNAVAILABLE(HttpStatus.SERVICE_UNAVAILABLE.value(), "CIRCUIT_BREAKER_503", "웹소켓 서비스를 이용할 수 없습니다."),
18+
;
19+
20+
override val errorReason: ErrorReason
21+
get() = ErrorReason(status, errorCode, message)
22+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.grepp.quizy.exception
2+
3+
import com.grepp.quizy.common.exception.WebException
4+
5+
sealed class CustomCircuitBreakerException(errorCode: CircuitBreakerErrorCode) : WebException(errorCode) {
6+
data object GameServiceUnavailableException :
7+
CustomCircuitBreakerException(CircuitBreakerErrorCode.GAME_UNAVAILABLE) {
8+
private fun readResolve(): Any = GameServiceUnavailableException
9+
10+
val EXCEPTION: CustomCircuitBreakerException = GameServiceUnavailableException
11+
}
12+
13+
data object UserServiceUnavailableException :
14+
CustomCircuitBreakerException(CircuitBreakerErrorCode.USER_UNAVAILABLE) {
15+
private fun readResolve(): Any = UserServiceUnavailableException
16+
17+
val EXCEPTION: CustomCircuitBreakerException = UserServiceUnavailableException
18+
}
19+
20+
data object QuizServiceUnavailableException :
21+
CustomCircuitBreakerException(CircuitBreakerErrorCode.QUIZ_UNAVAILABLE) {
22+
private fun readResolve(): Any = QuizServiceUnavailableException
23+
24+
val EXCEPTION: CustomCircuitBreakerException = QuizServiceUnavailableException
25+
}
26+
27+
data object MatchingServiceUnavailableException :
28+
CustomCircuitBreakerException(CircuitBreakerErrorCode.MATCHING_UNAVAILABLE) {
29+
private fun readResolve(): Any = MatchingServiceUnavailableException
30+
31+
val EXCEPTION: CustomCircuitBreakerException = MatchingServiceUnavailableException
32+
}
33+
34+
data object WsUnavailableException :
35+
CustomCircuitBreakerException(CircuitBreakerErrorCode.WS_UNAVAILABLE) {
36+
private fun readResolve(): Any = WsUnavailableException
37+
38+
val EXCEPTION: CustomCircuitBreakerException = WsUnavailableException
39+
}
40+
}
41+
42+

0 commit comments

Comments
 (0)