mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-14 10:35:11 +00:00
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:
committed by
David Lechner
parent
79db359398
commit
e7d44ea9a9
@@ -5,6 +5,7 @@
|
||||
|
||||
import { END, eventChannel } from 'redux-saga';
|
||||
import { call, cancel, put, spawn, takeEvery, takeMaybe } from 'typed-redux-saga/macro';
|
||||
import { ensureError } from '../utils';
|
||||
import {
|
||||
BootloaderConnectionAction,
|
||||
BootloaderConnectionActionType,
|
||||
@@ -35,7 +36,7 @@ function* write(
|
||||
}
|
||||
yield* put(didSend());
|
||||
} catch (err) {
|
||||
yield* put(didFailToSend(err));
|
||||
yield* put(didFailToSend(ensureError(err)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,7 +65,7 @@ function* connect(_action: BootloaderConnectionAction): Generator {
|
||||
// this can happen if the use cancels the dialog
|
||||
yield* put(didFailToConnect(Reason.Canceled));
|
||||
} else {
|
||||
yield* put(didFailToConnect(Reason.Unknown, err));
|
||||
yield* put(didFailToConnect(Reason.Unknown, ensureError(err)));
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -88,7 +89,7 @@ function* connect(_action: BootloaderConnectionAction): Generator {
|
||||
server = yield* call([device.gatt, 'connect']);
|
||||
} catch (err) {
|
||||
disconnectChannel.close();
|
||||
yield* put(didFailToConnect(Reason.Unknown, err));
|
||||
yield* put(didFailToConnect(Reason.Unknown, ensureError(err)));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -103,7 +104,7 @@ function* connect(_action: BootloaderConnectionAction): Generator {
|
||||
// https://chromium-review.googlesource.com/c/chromium/src/+/2214098
|
||||
yield* put(didFailToConnect(Reason.GattServiceNotFound));
|
||||
} else {
|
||||
yield* put(didFailToConnect(Reason.Unknown, err));
|
||||
yield* put(didFailToConnect(Reason.Unknown, ensureError(err)));
|
||||
}
|
||||
return;
|
||||
}
|
||||
@@ -117,7 +118,7 @@ function* connect(_action: BootloaderConnectionAction): Generator {
|
||||
} catch (err) {
|
||||
server.disconnect();
|
||||
yield* takeMaybe(disconnectChannel);
|
||||
yield* put(didFailToConnect(Reason.Unknown, err));
|
||||
yield* put(didFailToConnect(Reason.Unknown, ensureError(err)));
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -147,7 +148,7 @@ function* connect(_action: BootloaderConnectionAction): Generator {
|
||||
notificationChannel.close();
|
||||
server.disconnect();
|
||||
yield* takeMaybe(disconnectChannel);
|
||||
yield* put(didFailToConnect(Reason.Unknown, err));
|
||||
yield* put(didFailToConnect(Reason.Unknown, ensureError(err)));
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 { isWindows } from '../utils/os';
|
||||
import {
|
||||
BootloaderConnectionActionType,
|
||||
@@ -172,7 +172,7 @@ function* decodeResponse(action: BootloaderConnectionDidReceiveAction): Generato
|
||||
);
|
||||
}
|
||||
} catch (err) {
|
||||
yield* put(didError(err));
|
||||
yield* put(didError(ensureError(err)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user