src/utils: add ensureError() function

Typescript v4.4 no longer assumes that the error in catch is an Error
object [1]. This adds a helper function to ensure that caught errors
at least match the Error interface. If not, it creates a new Error
object with the value as the message.

[1]: https://devblogs.microsoft.com/typescript/announcing-typescript-4-4-beta/#using-unknown-in-catch-variables
This commit is contained in:
David Lechner
2021-08-30 13:06:01 -05:00
committed by David Lechner
parent 79db359398
commit e7d44ea9a9
8 changed files with 78 additions and 34 deletions
+2 -2
View File
@@ -12,7 +12,7 @@ import {
takeEvery,
} from 'typed-redux-saga/macro';
import { Action } from '../actions';
import { hex } from '../utils';
import { ensureError, hex } from '../utils';
import {
BlePybricksServiceActionType,
BlePybricksServiceCommandAction,
@@ -101,7 +101,7 @@ function* decodeResponse(action: BlePybricksServiceDidNotifyEventAction): Genera
);
}
} catch (err) {
yield* put(eventProtocolError(err));
yield* put(eventProtocolError(ensureError(err)));
}
}