summaryrefslogtreecommitdiff
path: root/sw-ui/src
diff options
context:
space:
mode:
authorjoffrey-bion <joffrey.bion@gmail.com>2020-11-29 15:50:47 +0100
committerjoffrey-bion <joffrey.bion@gmail.com>2020-11-29 15:50:47 +0100
commitaf34c335ff2e6f3d904bedea94ec99143576eb62 (patch)
tree7711b12e7a97e0bcb00f8aca80a88a9556af0976 /sw-ui/src
parentAdd fatal error dialog support (diff)
downloadseven-wonders-af34c335ff2e6f3d904bedea94ec99143576eb62.tar.gz
seven-wonders-af34c335ff2e6f3d904bedea94ec99143576eb62.tar.bz2
seven-wonders-af34c335ff2e6f3d904bedea94ec99143576eb62.zip
Trigger fatal error dialog on connection drop
Resolves: https://github.com/joffrey-bion/seven-wonders/issues/33
Diffstat (limited to 'sw-ui/src')
-rw-r--r--sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt57
1 files changed, 36 insertions, 21 deletions
diff --git a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt
index 3f25e826..6339f051 100644
--- a/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt
+++ b/sw-ui/src/main/kotlin/org/luxons/sevenwonders/ui/redux/sagas/Sagas.kt
@@ -1,13 +1,14 @@
package org.luxons.sevenwonders.ui.redux.sagas
import kotlinx.browser.window
-import kotlinx.coroutines.CoroutineStart
-import kotlinx.coroutines.ExperimentalCoroutinesApi
-import kotlinx.coroutines.coroutineScope
+import kotlinx.coroutines.*
import kotlinx.coroutines.flow.collect
-import kotlinx.coroutines.launch
+import org.hildan.krossbow.stomp.ConnectionException
+import org.hildan.krossbow.stomp.MissingHeartBeatException
+import org.hildan.krossbow.stomp.WebSocketClosedUnexpectedly
import org.luxons.sevenwonders.client.SevenWondersClient
import org.luxons.sevenwonders.client.SevenWondersSession
+import org.luxons.sevenwonders.ui.redux.FatalError
import org.luxons.sevenwonders.ui.redux.RequestChooseName
import org.luxons.sevenwonders.ui.redux.SetCurrentPlayerAction
import org.luxons.sevenwonders.ui.redux.SwState
@@ -20,27 +21,41 @@ import webpack.isProdEnv
typealias SwSagaContext = SagaContext<SwState, RAction, WrapperAction>
@OptIn(ExperimentalCoroutinesApi::class)
-suspend fun SwSagaContext.rootSaga() = coroutineScope {
- val action = next<RequestChooseName>()
- val serverUrl = sevenWondersWebSocketUrl()
- val session = SevenWondersClient().connect(serverUrl)
- console.info("Connected to Seven Wonders web socket API")
+suspend fun SwSagaContext.rootSaga() = try {
+ coroutineScope {
+ val action = next<RequestChooseName>()
+ val serverUrl = sevenWondersWebSocketUrl()
+ val session = SevenWondersClient().connect(serverUrl)
+ console.info("Connected to Seven Wonders web socket API")
- launch(start = CoroutineStart.UNDISPATCHED) {
- serverErrorSaga(session)
- }
+ launch(start = CoroutineStart.UNDISPATCHED) {
+ serverErrorSaga(session)
+ }
- val player = session.chooseName(action.playerName, null)
- dispatch(SetCurrentPlayerAction(player))
+ val player = session.chooseName(action.playerName, null)
+ dispatch(SetCurrentPlayerAction(player))
- routerSaga(Route.GAME_BROWSER) {
- when (it) {
- Route.HOME -> homeSaga(session)
- Route.LOBBY -> lobbySaga(session)
- Route.GAME_BROWSER -> gameBrowserSaga(session)
- Route.GAME -> gameSaga(session)
+ routerSaga(Route.GAME_BROWSER) {
+ when (it) {
+ Route.HOME -> homeSaga(session)
+ Route.LOBBY -> lobbySaga(session)
+ Route.GAME_BROWSER -> gameBrowserSaga(session)
+ Route.GAME -> gameSaga(session)
+ }
}
}
+} catch (e: Exception) {
+ console.error(e)
+ dispatchFatalError(e)
+}
+
+private fun SwSagaContext.dispatchFatalError(throwable: Throwable) {
+ when (throwable) {
+ is ConnectionException -> dispatch(FatalError(throwable.message ?: "Couldn't connect to the server."))
+ is MissingHeartBeatException -> dispatch(FatalError("The server doesn't seem to be responding."))
+ is WebSocketClosedUnexpectedly -> dispatch(FatalError("The connection to the server was closed unexpectedly."))
+ else -> dispatch(FatalError("An unexpected error occurred: ${throwable.message}"))
+ }
}
private fun sevenWondersWebSocketUrl(): String {
@@ -54,7 +69,7 @@ private fun sevenWondersWebSocketUrl(): String {
private suspend fun serverErrorSaga(session: SevenWondersSession) {
session.watchErrors().collect { err ->
- // TODO use blueprintjs toaster
+ // These are not an error for the user, but rather for the programmer
console.error("${err.code}: ${err.message}")
console.error(JSON.stringify(err))
}
bgstack15