summaryrefslogtreecommitdiff
path: root/frontend/src/sagas/errors.ts
diff options
context:
space:
mode:
authorjbion <joffrey.bion@amadeus.com>2019-05-05 11:22:58 +0200
committerjbion <joffrey.bion@amadeus.com>2019-05-06 18:33:14 +0200
commit347877687301ec43367105a7f8c49fe16129fc00 (patch)
tree4999cf1978527658f70a21ce59d5f5d08a7169fd /frontend/src/sagas/errors.ts
parentConvert reducers to typescript (diff)
downloadseven-wonders-347877687301ec43367105a7f8c49fe16129fc00.tar.gz
seven-wonders-347877687301ec43367105a7f8c49fe16129fc00.tar.bz2
seven-wonders-347877687301ec43367105a7f8c49fe16129fc00.zip
Convert redux sagas to TypeScript
Diffstat (limited to 'frontend/src/sagas/errors.ts')
-rw-r--r--frontend/src/sagas/errors.ts36
1 files changed, 36 insertions, 0 deletions
diff --git a/frontend/src/sagas/errors.ts b/frontend/src/sagas/errors.ts
new file mode 100644
index 00000000..b27dfa95
--- /dev/null
+++ b/frontend/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