diff --git a/mediapipe/tasks/web/core/task_runner.ts b/mediapipe/tasks/web/core/task_runner.ts index a3df7adf5..c2679b773 100644 --- a/mediapipe/tasks/web/core/task_runner.ts +++ b/mediapipe/tasks/web/core/task_runner.ts @@ -164,16 +164,19 @@ export abstract class TaskRunner { /** Throws the error from the error listener if an error was raised. */ private handleErrors() { - const errorCount = this.processingErrors.length; - if (errorCount === 1) { - // Re-throw error to get a more meaningful stacktrace - throw new Error(this.processingErrors[0].message); - } else if (errorCount > 1) { - throw new Error( - 'Encountered multiple errors: ' + - this.processingErrors.map(e => e.message).join(', ')); + try { + const errorCount = this.processingErrors.length; + if (errorCount === 1) { + // Re-throw error to get a more meaningful stacktrace + throw new Error(this.processingErrors[0].message); + } else if (errorCount > 1) { + throw new Error( + 'Encountered multiple errors: ' + + this.processingErrors.map(e => e.message).join(', ')); + } + } finally { + this.processingErrors = []; } - this.processingErrors = []; } /** Configures the `externalFile` option */ diff --git a/mediapipe/tasks/web/core/task_runner_test.ts b/mediapipe/tasks/web/core/task_runner_test.ts index 684beb70c..9a8aa32eb 100644 --- a/mediapipe/tasks/web/core/task_runner_test.ts +++ b/mediapipe/tasks/web/core/task_runner_test.ts @@ -139,6 +139,18 @@ describe('TaskRunner', () => { }).toThrowError(/Test error 1, Test error 2/); }); + it('clears errors once thrown', () => { + taskRunner.enqueueError('Test error'); + + expect(() => { + taskRunner.setGraph(new Uint8Array(0), /* isBinary= */ true); + }).toThrowError(/Test error/); + + expect(() => { + taskRunner.setGraph(new Uint8Array(0), /* isBinary= */ true); + }).not.toThrow(); + }); + it('verifies that at least one model asset option is provided', () => { expect(() => { taskRunner.setOptions({});