From 1e846788a80f268564aa7dc63880bdcd11d85b87 Mon Sep 17 00:00:00 2001 From: calixteman Date: Mon, 12 Oct 2026 17:26:25 +0200 Subject: [PATCH] Fix script error reporting Handle thrown null values and failures in string conversion or stack access. Report field errors through the document, since fields without widgets have no send callback. This prevents error reporting from skipping subsequent actions, field formatting, and OpenAction. --- src/scripting_api/app_utils.js | 17 ++++-- src/scripting_api/doc.js | 6 +-- src/scripting_api/field.js | 12 +++-- test/unit/scripting_spec.js | 97 ++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 10 deletions(-) diff --git a/src/scripting_api/app_utils.js b/src/scripting_api/app_utils.js index 610cdfddd..0c70b46ca 100644 --- a/src/scripting_api/app_utils.js +++ b/src/scripting_api/app_utils.js @@ -21,9 +21,20 @@ const FORMS_VERSION = 21.00720099; const USERACTIVATION_CALLBACKID = 0; const USERACTIVATION_MAXTIME_VALIDITY = 5000; -function serializeError(error) { - const value = `${error.toString()}\n${error.stack}`; - return { command: "error", value }; +function serializeError(error, prefix = "") { + // Scripts can throw non-Error values. + let message; + try { + message = String(error); + } catch { + message = "Unknown error"; + } + let stack; + try { + stack = error?.stack; + } catch {} + const value = typeof stack === "string" ? `${message}\n${stack}` : message; + return { command: "error", value: `${prefix}${value}` }; } // Helpers for simple `Map.prototype.getOrInsertComputed()` invocations, diff --git a/src/scripting_api/doc.js b/src/scripting_api/doc.js index 10b3a0fa9..85b9c96cb 100644 --- a/src/scripting_api/doc.js +++ b/src/scripting_api/doc.js @@ -197,9 +197,9 @@ class Doc extends PDFObject { try { this._globalEval(action); } catch (error) { - const serializedError = serializeError(error); - serializedError.value = `Error when executing "${name}" for document\n${serializedError.value}`; - this._send(serializedError); + this._send( + serializeError(error, `Error when executing "${name}" for document\n`) + ); } } } diff --git a/src/scripting_api/field.js b/src/scripting_api/field.js index c39223da4..1b6861410 100644 --- a/src/scripting_api/field.js +++ b/src/scripting_api/field.js @@ -545,12 +545,16 @@ class Field extends PDFObject { const actions = this._actions.get(eventName); for (const action of actions) { try { - // Action evaluation must happen in the global scope + // Evaluate actions in the global scope. this._globalEval(action); } catch (error) { - const serializedError = serializeError(error); - serializedError.value = `Error when executing "${eventName}" for field "${this._id}"\n${serializedError.value}`; - this._send(serializedError); + // Fields without widgets have no send callback. + this._document.obj._send( + serializeError( + error, + `Error when executing "${eventName}" for field "${this._id}"\n` + ) + ); } } diff --git a/test/unit/scripting_spec.js b/test/unit/scripting_spec.js index d548d8281..ce7e9bf15 100644 --- a/test/unit/scripting_spec.js +++ b/test/unit/scripting_spec.js @@ -540,6 +540,103 @@ describe("Scripting", function () { }); }); + it("should report null and run the next action", async () => { + const refId = getId(); + sandbox.createSandbox({ + appInfo: { language: "en-US", platform: "Linux x86_64" }, + objects: { + field: [ + { + id: refId, + value: "", + actions: { + custom: ["throw null;", "globalThis.nextAction = true;"], + }, + type: "text", + }, + ], + }, + calculationOrder: [], + }); + + await sandbox.dispatchEventInSandbox({ id: refId, name: "custom" }); + expect(send_queue.get("error")).toEqual({ + command: "error", + value: `Error when executing "custom" for field "${refId}"\nnull`, + }); + send_queue.delete("error"); + expect(await myeval(`globalThis.nextAction`)).toEqual(true); + }); + + it("should report errors when string conversion or stack access throws", async () => { + const refId = getId(); + const cases = { + throwingToString: ["({ toString() { throw 1; } })", "Unknown error"], + nullPrototype: ["Object.create(null)", "Unknown error"], + throwingStack: ["({ get stack() { throw 2; } })", "[object Object]"], + }; + const actions = Object.create(null); + for (const [name, [thrown]] of Object.entries(cases)) { + actions[name] = [`throw ${thrown};`]; + } + sandbox.createSandbox({ + appInfo: { language: "en-US", platform: "Linux x86_64" }, + objects: { + field: [{ id: refId, value: "", actions, type: "text" }], + }, + calculationOrder: [], + }); + + for (const [name, [, expected]] of Object.entries(cases)) { + send_queue.delete("error"); + await sandbox.dispatchEventInSandbox({ id: refId, name }); + expect(send_queue.get("error")) + .withContext(name) + .toEqual({ + command: "error", + value: `Error when executing "${name}" for field "${refId}"\n${expected}`, + }); + } + send_queue.delete("error"); + }); + + it("should report errors from fields without widgets", async () => { + const refIds = [getId(), getId()]; + sandbox.createSandbox({ + appInfo: { language: "en-US", platform: "Linux x86_64" }, + docInfo: { actions: { OpenAction: ["globalThis.openAction = true;"] } }, + objects: { + A: [ + { + id: refIds[0], + type: "", + kidIds: [refIds[1]], + actions: { Format: [`throw new Error("Broken");`] }, + }, + ], + "A.B": [ + { + id: refIds[1], + value: "", + actions: { Format: ["globalThis.kidFormat = true;"] }, + type: "text", + }, + ], + }, + calculationOrder: [], + }); + + await sandbox.dispatchEventInSandbox({ id: "doc", name: "Open" }); + expect(send_queue.get("error").value.split("\n").slice(0, 2)).toEqual([ + `Error when executing "Format" for field "${refIds[0]}"`, + "Error: Broken", + ]); + send_queue.delete("error"); + expect( + await myeval(`[globalThis.kidFormat, globalThis.openAction]`) + ).toEqual([true, true]); + }); + it("should trigger an event added with setAction", async () => { const refId = getId(); const data = {