Skip to content

fix: safely extract error message from unknown error type - #2724

Closed
dajiaohuang wants to merge 2 commits into
slackapi:mainfrom
dajiaohuang:fix/safe-error-message-extraction
Closed

fix: safely extract error message from unknown error type#2724
dajiaohuang wants to merge 2 commits into
slackapi:mainfrom
dajiaohuang:fix/safe-error-message-extraction

Conversation

@dajiaohuang

Copy link
Copy Markdown

Summary

The code in packages/oauth/src/install-provider.ts assumed that caught errors (e) are always Error objects and accessed e.message directly. If e was a primitive value (string, number) or undefined, this could result in undefined being passed to GenerateInstallUrlError.

Problem

At line 407:

throw new GenerateInstallUrlError((e as any).message);

If e is not an Error object (e.g., throw "something went wrong" or throw null), then (e as any).message would be undefined.

Fix

Used the same safe pattern already used elsewhere in this file (line 289):

const errorMessage = e instanceof Error ? e.message : String(e);
throw new GenerateInstallUrlError(errorMessage);

This ensures a valid string is always passed to GenerateInstallUrlError, whether e is an Error object or a primitive value.

Testing

  • Run existing tests: npm test --workspace=packages/oauth

…ponse

The JSON.parse at line 802 was not wrapped in a try-catch, which could
cause an unhandled exception if the response body is not valid JSON.
This is inconsistent with the similar operation at line 811 which is
properly wrapped.

Added try-catch to handle parse failures gracefully, returning
{ ok: false, error: <error message> } instead of throwing.
Before this fix, the code assumed `e` is an Error object and accessed
`e.message` directly. If `e` was a primitive value or undefined,
this could result in undefined being passed to GenerateInstallUrlError.

Now we use the same pattern as line 289 in this file:
`e instanceof Error ? e.message : String(e)`

This ensures a valid string is always passed to GenerateInstallUrlError.
@dajiaohuang
dajiaohuang requested a review from a team as a code owner September 2, 2026 10:06
@changeset-bot

changeset-bot Bot commented Sep 2, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 42b4af7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@salesforce-cla

salesforce-cla Bot commented Sep 2, 2026

Copy link
Copy Markdown

Thanks for the contribution! Before we can merge this, we need @dajiaohuang to sign the Salesforce Inc. Contributor License Agreement.

} catch (_) {
// failed to parse the response body as JSON
data = { ok: false, error: new TextDecoder().decode(buffer) };
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see this is a duplicate of your other PR, https://github.com/slackapi/node-slack-sdk/pull/2723/changes. I'm going to close the other one

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same nit as posted to the other PR, could move const text = new TextDecoder().decode(buffer); above to avoid decoding twice on error

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And the need for signing the CLA

@vegeris

vegeris commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

@vegeris vegeris closed this Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants