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 https://github.com/pybricks/support/issues/772 (but
there is no known reproducible case to check it).
This commit is contained in:
David Lechner
2023-01-04 11:20:52 -06:00
committed by David Lechner
parent 8ed49b6f55
commit 5330821a96
+10 -7
View File
@@ -1,12 +1,12 @@
// SPDX-License-Identifier: MIT
// Copyright (c) 2022 The Pybricks Authors
// Copyright (c) 2022-2023 The Pybricks Authors
// This file runs as a web worker.
// NB: We need to be very careful about imports here since many libraries for
// web aren't compatible with web workers!
import { loadPyodide, version as pyodideVersion } from 'pyodide';
import { PyodideInterface, loadPyodide, version as pyodideVersion } from 'pyodide';
import { ensureError } from '../utils';
import {
pythonMessageComplete,
@@ -24,6 +24,12 @@ import {
pythonMessageWriteUserFile,
} from './python-message';
type PythonError = InstanceType<PyodideInterface['PythonError']>;
function isPythonError(err: Error): err is PythonError {
return err.constructor.name === 'PythonError';
}
/**
* Wrapper around {@link ensureError} that also converts KeyboardInterrupt to
* AbortError.
@@ -33,11 +39,8 @@ import {
function fixUpError(err: unknown): Error {
const error = ensureError(err);
if (
error.constructor.name === 'PythonError' &&
error.message.match(/KeyboardInterrupt/)
) {
return new DOMException('cancelled', 'AbortError');
if (isPythonError(error) && error.type === 'KeyboardInterrupt') {
return new DOMException('cancelled via KeyboardInterrupt', 'AbortError');
}
return error;