Skip to content

Commit 5330821

Browse files
committed
pybricksMicropython: use new PythonError type property
This was introduced in Pyodide 0.22 and saves us from having to parse the message for the type. Could also affect pybricks/support#772 (but there is no known reproducible case to check it).
1 parent 8ed49b6 commit 5330821

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/pybricksMicropython/python-worker.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
// SPDX-License-Identifier: MIT
2-
// Copyright (c) 2022 The Pybricks Authors
2+
// Copyright (c) 2022-2023 The Pybricks Authors
33

44
// This file runs as a web worker.
55

66
// NB: We need to be very careful about imports here since many libraries for
77
// web aren't compatible with web workers!
88

9-
import { loadPyodide, version as pyodideVersion } from 'pyodide';
9+
import { PyodideInterface, loadPyodide, version as pyodideVersion } from 'pyodide';
1010
import { ensureError } from '../utils';
1111
import {
1212
pythonMessageComplete,
@@ -24,6 +24,12 @@ import {
2424
pythonMessageWriteUserFile,
2525
} from './python-message';
2626

27+
type PythonError = InstanceType<PyodideInterface['PythonError']>;
28+
29+
function isPythonError(err: Error): err is PythonError {
30+
return err.constructor.name === 'PythonError';
31+
}
32+
2733
/**
2834
* Wrapper around {@link ensureError} that also converts KeyboardInterrupt to
2935
* AbortError.
@@ -33,11 +39,8 @@ import {
3339
function fixUpError(err: unknown): Error {
3440
const error = ensureError(err);
3541

36-
if (
37-
error.constructor.name === 'PythonError' &&
38-
error.message.match(/KeyboardInterrupt/)
39-
) {
40-
return new DOMException('cancelled', 'AbortError');
42+
if (isPythonError(error) && error.type === 'KeyboardInterrupt') {
43+
return new DOMException('cancelled via KeyboardInterrupt', 'AbortError');
4144
}
4245

4346
return error;

0 commit comments

Comments
 (0)