Skip to content

Promises should be rejected with Error instances #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
tduyduc opened this issue Mar 1, 2025 · 0 comments
Open

Promises should be rejected with Error instances #48

tduyduc opened this issue Mar 1, 2025 · 0 comments
Labels
bug Something isn't working

Comments

@tduyduc
Copy link

tduyduc commented Mar 1, 2025

Issue Description

Promise rejections in the source code are currently using (primitive) strings. For example:

An example of Promise rejection with a string

This would make exception handling more cumbersome (because an additional type check is needed) and more difficult (all stack trace information is destroyed).

Rationale / Further Reading

Example

import AppleAuth from 'apple-auth';

declare var auth: AppleAuth;
try {
  await auth.accessToken(code);
} catch (error) {
  typeof error; // Actual: "string" here. Doesn't have any stack trace or `message` property from a standard Error.
}

try {
  null.x;
} catch (error) {
  error instanceof Error; // Expected: an instance of Error (or a subclass).
}

Proposed Solution

All exceptions from Promise constructors should be instances of Error. Using the example of src/apple-auth.js from the above image:

 reject(
-  `AppleAuth Error - An error occurred while getting response from Apple's servers: 
-  ${response}${responseData ? (" | " + responseData) : ""}`
+  new Error(
+    `AppleAuth Error - An error occurred while getting response from Apple's servers: 
+    ${response}${responseData ? (" | " + responseData) : ""}`
+  )
 );

I am able to make a pull request for these changes.

@dosubot dosubot bot added the bug Something isn't working label Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant