Skip to content

Commit 652c0d6

Browse files
feat: support native ES Error (#325)
* chore: updates from devScripts * fix(deps): bump deps * fix: fallback text for template literal * fix: widen error type to support native Error.cause * refactor: use native format for stack/cause recursion * fix: use inspect --------- Co-authored-by: mshanemc <shane.mclaughlin@salesforce.com>
1 parent 685baf7 commit 652c0d6

File tree

5 files changed

+281
-247
lines changed

5 files changed

+281
-247
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
"test:only": "wireit"
3030
},
3131
"dependencies": {
32-
"@salesforce/ts-types": "^2.0.10"
32+
"@salesforce/ts-types": "^2.0.11"
3333
},
3434
"devDependencies": {
35-
"@salesforce/dev-scripts": "^10.2.2",
35+
"@salesforce/dev-scripts": "^10.2.5",
3636
"lodash-cli": "^4.17.5",
3737
"ts-node": "^10.9.2",
38-
"typescript": "^5.4.5"
38+
"typescript": "^5.5.4"
3939
},
4040
"wireit": {
4141
"build": {

src/errors.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
7+
import { inspect } from 'node:util';
88
/**
99
* Any `Error` compatible with the `NamedError` type signature.
1010
*/
11-
type NamedErrorLike = Error & {
12-
readonly name: string;
13-
readonly cause?: NamedErrorLike;
14-
readonly fullStack?: string;
15-
};
11+
type NamedErrorLike =
12+
| Error
13+
| NamedError
14+
| {
15+
readonly name: string;
16+
readonly cause?: NamedErrorLike;
17+
readonly fullStack?: string;
18+
};
1619

1720
export class NamedError extends Error {
1821
public readonly name: string;
@@ -32,12 +35,7 @@ export class NamedError extends Error {
3235
}
3336

3437
public get fullStack(): string | undefined {
35-
let stack = this.stack;
36-
const causedStack = this.cause?.fullStack ?? this.cause?.stack;
37-
if (causedStack) {
38-
stack = `${stack ? stack + '\n' : ''}Caused by: ${causedStack}`;
39-
}
40-
return stack;
38+
return inspect(this);
4139
}
4240
}
4341

src/throttledPromiseAll.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class ThrottledPromiseAll<T, O = T> {
167167
const r = await Promise.race(concurrencyPool.values());
168168
const rIndex = r?.index ?? -1;
169169
if (!concurrencyPool.has(rIndex)) {
170-
throw new Error(`PromiseQueue: Could not find index ${r?.index} in pool`);
170+
throw new Error(`PromiseQueue: Could not find index ${r?.index ?? '<undefined>'} in pool`);
171171
}
172172
concurrencyPool.delete(rIndex);
173173
this.#results.push(r);

test/errors.test.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ describe('NamedError', () => {
2020
const three = new NamedError('ThreeError', two);
2121
three.stack = `${three.name}:\n at test:3:1`;
2222

23-
expect(three.fullStack).to.equal(
24-
`ThreeError:
25-
at test:3:1
26-
Caused by: TwoError: message two
27-
at test:2:1
28-
Caused by: OneError:
29-
at test:1:1
30-
Caused by: Error: message zero
31-
at test:0:1`
32-
);
23+
[
24+
'NamedError [ThreeError]',
25+
'at test:3:1',
26+
'cause: NamedError [TwoError]: message two',
27+
'at test:2:1',
28+
'cause: NamedError [OneError]',
29+
'at test:1:1',
30+
'cause: Error: message zero',
31+
'at test:0:1',
32+
].map((line) => expect(three.fullStack).to.contain(line));
3333
});
3434
});
3535

0 commit comments

Comments
 (0)