From 5330821a96a5fd13927ceccdec99d9aebef0fee4 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Wed, 4 Jan 2023 11:08:21 -0600 Subject: [PATCH] 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). --- src/pybricksMicropython/python-worker.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/pybricksMicropython/python-worker.ts b/src/pybricksMicropython/python-worker.ts index da00fa36..6079c3b9 100644 --- a/src/pybricksMicropython/python-worker.ts +++ b/src/pybricksMicropython/python-worker.ts @@ -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; + +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;