Skip to content
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

feat: support native ES Error #325

Merged
merged 6 commits into from
Jul 30, 2024
Merged
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
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
"test:only": "wireit"
},
"dependencies": {
"@salesforce/ts-types": "^2.0.10"
"@salesforce/ts-types": "^2.0.11"
},
"devDependencies": {
"@salesforce/dev-scripts": "^10.2.2",
"@salesforce/dev-scripts": "^10.2.5",
"lodash-cli": "^4.17.5",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
"typescript": "^5.5.4"
},
"wireit": {
"build": {
Expand Down
22 changes: 10 additions & 12 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/

import { inspect } from 'node:util';
/**
* Any `Error` compatible with the `NamedError` type signature.
*/
type NamedErrorLike = Error & {
readonly name: string;
readonly cause?: NamedErrorLike;
readonly fullStack?: string;
};
type NamedErrorLike =
Copy link
Contributor

Choose a reason for hiding this comment

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

widen the type, should be non-breaking. The "native" Error might not have a name, and now that's allowed as a cause.

| Error
| NamedError
| {
readonly name: string;
readonly cause?: NamedErrorLike;
readonly fullStack?: string;
};

export class NamedError extends Error {
public readonly name: string;
Expand All @@ -32,12 +35,7 @@ export class NamedError extends Error {
}

public get fullStack(): string | undefined {
let stack = this.stack;
const causedStack = this.cause?.fullStack ?? this.cause?.stack;
if (causedStack) {
stack = `${stack ? stack + '\n' : ''}Caused by: ${causedStack}`;
}
return stack;
return inspect(this);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/throttledPromiseAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export class ThrottledPromiseAll<T, O = T> {
const r = await Promise.race(concurrencyPool.values());
const rIndex = r?.index ?? -1;
if (!concurrencyPool.has(rIndex)) {
throw new Error(`PromiseQueue: Could not find index ${r?.index} in pool`);
throw new Error(`PromiseQueue: Could not find index ${r?.index ?? '<undefined>'} in pool`);
}
concurrencyPool.delete(rIndex);
this.#results.push(r);
Expand Down
20 changes: 10 additions & 10 deletions test/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ describe('NamedError', () => {
const three = new NamedError('ThreeError', two);
three.stack = `${three.name}:\n at test:3:1`;

expect(three.fullStack).to.equal(
`ThreeError:
at test:3:1
Caused by: TwoError: message two
at test:2:1
Caused by: OneError:
at test:1:1
Caused by: Error: message zero
at test:0:1`
);
[
'NamedError [ThreeError]',
'at test:3:1',
'cause: NamedError [TwoError]: message two',
'at test:2:1',
'cause: NamedError [OneError]',
'at test:1:1',
'cause: Error: message zero',
'at test:0:1',
].map((line) => expect(three.fullStack).to.contain(line));
});
});

Expand Down
Loading