slackapi / slackapi/slack-github-action

`TypeError: err.toJSON is not a function` when webhook URL is invalid

Open
#511 2 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
1.3k
Forks
200
Avg merge
2h 59m
Merged PRs (30d)
6

Description

This took a bit for me to track down (my own mistake), thanks to the real error not being shown in the action. If, for example, I accidentally quote the webhook URL, like:

           - name: Notify Slack
              uses: slackapi/slack-github-action@v2
              with:
                  errors: true
                  payload: <redacted>
                  webhook: ${{ toJSON(secrets.SLACK_WEBHOOK_URL) }}
                  webhook-type: webhook-trigger

I get an exception thrown:

Run slackapi/slack-github-action@v2
Error: TypeError: err.toJSON is not a function

Error: err.toJSON is not a function
TypeError: err.toJSON is not a function
    at Webhook.post (file:///home/runner/work/_actions/slackapi/slack-github-action/v2/src/webhook.js:36:1)
    at post (file:///home/runner/work/_actions/slackapi/slack-github-action/v2/src/send.js:35:1)
    at send (file:///home/runner/work/_actions/slackapi/slack-github-action/v2/src/send.js:15:1)
    at file:///home/runner/work/_actions/slackapi/slack-github-action/v2/src/index.js:9:1
file:///home/runner/work/_actions/slackapi/slack-github-action/v2/src/webhook.js:36
      const response = err.toJSON();
^
TypeError: err.toJSON is not a function
    at Webhook.post (file:///home/runner/work/_actions/slackapi/slack-github-action/v2/src/webhook.js:36:1)
    at post (file:///home/runner/work/_actions/slackapi/slack-github-action/v2/src/send.js:35:1)
    at send (file:///home/runner/work/_actions/slackapi/slack-github-action/v2/src/send.js:15:1)
    at file:///home/runner/work/_actions/slackapi/slack-github-action/v2/src/index.js:9:1

It looks like the catch block is assuming the exception will always have a toJSON function, but in my case it obviously doesn't.

The fix of course, was to remove the erroneous toJSON(...) wrapper from the webhook input, but it would have saved me a lot of time if the exception was handled better by thi action.

Note, the err.toJSON in the exception is unrelated to the toJSON in my input - its only coincidental that we both used (different) toJSON functions (JavaScript vs GitHub Expression respectively).

Cheers.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/webhook.js at the catch block reported at line 36, then review how it is called from src/send.js and src/index.js. Ensure an invalid webhook URL reports the underlying error without assuming the exception has toJSON; done means the original failure is shown instead of a secondary TypeError.

Written by the indexing model from the issue text.

Assessment

Tech stack
github-actions, javascript
Domain
ci-cd, devops
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.