From 83741c4b8511ec5a0d031f2e1898d8f0188bc52a Mon Sep 17 00:00:00 2001 From: saschabuehrle Date: Tue, 24 Mar 2026 10:48:53 +0100 Subject: [PATCH] fix: handle webhook errors without toJSON (fixes #511) --- src/webhook.js | 15 ++++++++++++--- test/webhook.spec.js | 26 ++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/webhook.js b/src/webhook.js index 0d6df7d4..221c178b 100644 --- a/src/webhook.js +++ b/src/webhook.js @@ -33,11 +33,20 @@ export default class Webhook { config.core.setOutput("response", JSON.stringify(response.data)); config.core.debug(JSON.stringify(response.data)); } catch (/** @type {any} */ err) { - const response = err.toJSON(); + const response = + typeof err?.toJSON === "function" + ? err.toJSON() + : { + message: err?.message, + status: err?.response?.status, + }; + config.core.setOutput("ok", response.status === 200); config.core.setOutput("response", JSON.stringify(response.message)); - config.core.debug(response); - throw new SlackError(config.core, response.message); + config.core.debug(JSON.stringify(response)); + throw new SlackError(config.core, response.message ?? "Request failed", { + cause: err, + }); } } diff --git a/test/webhook.spec.js b/test/webhook.spec.js index 09867e68..4f34fab7 100644 --- a/test/webhook.spec.js +++ b/test/webhook.spec.js @@ -160,6 +160,32 @@ describe("webhook", () => { assert.equal(mocks.core.setOutput.getCall(0).lastArg, false); assert.equal(mocks.core.setOutput.getCall(1).firstArg, "response"); }); + + it("handles errors without toJSON", async () => { + mocks.core.getInput + .withArgs("webhook") + .returns("\"https://hooks.slack.com\""); + mocks.core.getInput.withArgs("webhook-type").returns("incoming-webhook"); + mocks.core.getInput.withArgs("payload").returns("text: test"); + mocks.core.getBooleanInput.withArgs("errors").returns(true); + mocks.axios.post.returns(Promise.reject(new TypeError("Invalid URL"))); + + try { + await send(mocks.core); + assert.fail("Failed to throw for non-axios error"); + } catch (err) { + if (err instanceof SlackError) { + assert.ok(err.message.includes("Invalid URL")); + } else { + assert.fail(err); + } + } + + assert.equal(mocks.core.setOutput.getCall(0).firstArg, "ok"); + assert.equal(mocks.core.setOutput.getCall(0).lastArg, false); + assert.equal(mocks.core.setOutput.getCall(1).firstArg, "response"); + assert.equal(mocks.core.setOutput.getCall(1).lastArg, '"Invalid URL"'); + }); }); describe("proxies", () => {