From 238000e14ec7fbb397a184838e5b1eddaeeb4c1b Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Mon, 23 Dec 2024 11:05:10 +0100 Subject: [PATCH] Make `MessageHandler.prototype.send` able to handle `Error`s properly Currently the "DocException" message requires manual handling of `Error`s on both the main- and worker-threads, which leads to some code duplication. Note that the `MessageHandler` implementation already deals with `Error`s when its `sendWithPromise` respectively `sendWithStream` methods are used, and it's easy enough to extend this to also apply to the `send` method. Finally, changes the `wrapReason` helper function to use a slightly shorter parameter name and removes unused entries from the `CallbackKind`/`StreamKind` enumerations. --- src/core/worker.js | 16 +------------- src/display/api.js | 28 ++---------------------- src/shared/message_handler.js | 40 ++++++++++++++++------------------- 3 files changed, 21 insertions(+), 63 deletions(-) diff --git a/src/core/worker.js b/src/core/worker.js index 7ae22ee45afdb..f2f9ea0560c3f 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -18,14 +18,10 @@ import { assert, getVerbosityLevel, info, - InvalidPDFException, isNodeJS, - MissingPDFException, PasswordException, setVerbosityLevel, stringToPDFString, - UnexpectedResponseException, - UnknownErrorException, VerbosityLevel, warn, } from "../shared/util.js"; @@ -347,18 +343,8 @@ class WorkerMessageHandler { finishWorkerTask(task); handler.send("DocException", ex); }); - } else if ( - ex instanceof InvalidPDFException || - ex instanceof MissingPDFException || - ex instanceof UnexpectedResponseException || - ex instanceof UnknownErrorException - ) { - handler.send("DocException", ex); } else { - handler.send( - "DocException", - new UnknownErrorException(ex.message, ex.toString()) - ); + handler.send("DocException", ex); } } diff --git a/src/display/api.js b/src/display/api.js index 1594d7cb2bfd1..c2fabc71a64c4 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -24,17 +24,13 @@ import { FeatureTest, getVerbosityLevel, info, - InvalidPDFException, isNodeJS, MAX_IMAGE_SIZE_TO_CACHE, - MissingPDFException, PasswordException, RenderingIntentFlag, setVerbosityLevel, shadow, stringToBytes, - UnexpectedResponseException, - UnknownErrorException, unreachable, warn, } from "../shared/util.js"; @@ -2731,28 +2727,8 @@ class WorkerTransport { loadingTask._capability.resolve(new PDFDocumentProxy(pdfInfo, this)); }); - messageHandler.on("DocException", function (ex) { - let reason; - switch (ex.name) { - case "PasswordException": - reason = new PasswordException(ex.message, ex.code); - break; - case "InvalidPDFException": - reason = new InvalidPDFException(ex.message); - break; - case "MissingPDFException": - reason = new MissingPDFException(ex.message); - break; - case "UnexpectedResponseException": - reason = new UnexpectedResponseException(ex.message, ex.status); - break; - case "UnknownErrorException": - reason = new UnknownErrorException(ex.message, ex.details); - break; - default: - unreachable("DocException - expected a valid Error."); - } - loadingTask._capability.reject(reason); + messageHandler.on("DocException", ex => { + loadingTask._capability.reject(ex); }); messageHandler.on("PasswordRequest", exception => { diff --git a/src/shared/message_handler.js b/src/shared/message_handler.js index 0ed90ee5e8435..25bca2a70b9e7 100644 --- a/src/shared/message_handler.js +++ b/src/shared/message_handler.js @@ -16,6 +16,7 @@ import { AbortException, assert, + InvalidPDFException, MissingPDFException, PasswordException, UnexpectedResponseException, @@ -24,13 +25,11 @@ import { } from "./util.js"; const CallbackKind = { - UNKNOWN: 0, DATA: 1, ERROR: 2, }; const StreamKind = { - UNKNOWN: 0, CANCEL: 1, CANCEL_COMPLETE: 2, CLOSE: 3, @@ -43,31 +42,25 @@ const StreamKind = { function onFn() {} -function wrapReason(reason) { - if ( - !( - reason instanceof Error || - (typeof reason === "object" && reason !== null) - ) - ) { - unreachable( - 'wrapReason: Expected "reason" to be a (possibly cloned) Error.' - ); +function wrapReason(ex) { + if (!(ex instanceof Error || (typeof ex === "object" && ex !== null))) { + unreachable("wrapReason: Expected a (possibly cloned) Error."); } - switch (reason.name) { + switch (ex.name) { case "AbortException": - return new AbortException(reason.message); + return new AbortException(ex.message); + case "InvalidPDFException": + return new InvalidPDFException(ex.message); case "MissingPDFException": - return new MissingPDFException(reason.message); + return new MissingPDFException(ex.message); case "PasswordException": - return new PasswordException(reason.message, reason.code); + return new PasswordException(ex.message, ex.code); case "UnexpectedResponseException": - return new UnexpectedResponseException(reason.message, reason.status); + return new UnexpectedResponseException(ex.message, ex.status); case "UnknownErrorException": - return new UnknownErrorException(reason.message, reason.details); - default: - return new UnknownErrorException(reason.message, reason.toString()); + return new UnknownErrorException(ex.message, ex.details); } + return new UnknownErrorException(ex.message, ex.toString()); } class MessageHandler { @@ -149,7 +142,7 @@ class MessageHandler { this.#createStreamSink(data); return; } - action(data.data); + action(data.reason ? wrapReason(data.reason) : data.data); } on(actionName, handler) { @@ -173,12 +166,15 @@ class MessageHandler { * @param {Array} [transfers] - List of transfers/ArrayBuffers. */ send(actionName, data, transfers) { + const isErr = data instanceof Error; + this.comObj.postMessage( { sourceName: this.sourceName, targetName: this.targetName, action: actionName, - data, + data: isErr ? null : data, + reason: isErr ? wrapReason(data) : null, }, transfers );