Skip to content

Promises should be rejected with Error instances #48

@tduyduc

Description

@tduyduc

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions