Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/webhook.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}
}

Expand Down
26 changes: 26 additions & 0 deletions test/webhook.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down