summaryrefslogtreecommitdiff
path: root/sw-ui/src/sagas/errors.ts
diff options
context:
space:
mode:
authorJoffrey BION <joffrey.bion@gmail.com>2019-05-16 23:48:38 +0200
committerJoffrey BION <joffrey.bion@gmail.com>2019-05-16 23:48:38 +0200
commit2382a452456e4bdef4584e1046925e372624cb79 (patch)
tree0e49b2e5d81facb55fb8b08228abeb218a27d466 /sw-ui/src/sagas/errors.ts
parentRemove GRADLE_METADATA feature to avoid breaking frontend build (diff)
downloadseven-wonders-2382a452456e4bdef4584e1046925e372624cb79.tar.gz
seven-wonders-2382a452456e4bdef4584e1046925e372624cb79.tar.bz2
seven-wonders-2382a452456e4bdef4584e1046925e372624cb79.zip
Rationalize module names
Diffstat (limited to 'sw-ui/src/sagas/errors.ts')
-rw-r--r--sw-ui/src/sagas/errors.ts36
1 files changed, 36 insertions, 0 deletions
diff --git a/sw-ui/src/sagas/errors.ts b/sw-ui/src/sagas/errors.ts
new file mode 100644
index 00000000..b27dfa95
--- /dev/null
+++ b/sw-ui/src/sagas/errors.ts
@@ -0,0 +1,36 @@
+import {Toaster} from '@blueprintjs/core';
+import {Channel, eventChannel} from 'redux-saga';
+import {apply, cancelled, take} from 'redux-saga/effects';
+import {ApiError} from '../api/model';
+import {SevenWondersSession} from '../api/sevenWondersApi';
+
+const ErrorToaster = Toaster.create();
+
+export function* errorHandlingSaga(session: SevenWondersSession): any {
+ const errorChannel: Channel<ApiError> = yield eventChannel(session.watchErrors());
+ try {
+ while (true) {
+ const error: ApiError = yield take(errorChannel);
+ yield* handleOneError(error);
+ }
+ } finally {
+ if (yield cancelled()) {
+ console.log('Error management saga cancelled');
+ yield apply(errorChannel, errorChannel.close);
+ }
+ }
+}
+
+function* handleOneError(err: ApiError): any {
+ console.error('Error received on web socket channel', err);
+ const msg = buildMsg(err);
+ yield apply(ErrorToaster, ErrorToaster.show, [{ intent: 'danger', icon: 'error', message: msg }]);
+}
+
+function buildMsg(err: ApiError): string {
+ if (err.details.length > 0) {
+ return err.details.map(d => d.message).join('\n');
+ } else {
+ return err.message;
+ }
+}
bgstack15