Skip to content

Commit 80c05e2

Browse files
bmiddhaCopilot
andauthored
refactor(rushell): expose parse error cause (#5932)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: 8ebd5bf2-c44b-42d5-be25-e7936d4b0a14
1 parent 02a5f0a commit 80c05e2

3 files changed

Lines changed: 50 additions & 2 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/rushell",
5+
"comment": "Expose a ParseError's underlying error through the standard Error.cause property while retaining innerError as an alias.",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/rushell"
10+
}

libraries/rushell/src/ParseError.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ export class ParseError extends Error {
1919
public readonly unformattedMessage: string;
2020

2121
/**
22-
* The underlying error, if this error is resulted from an earlier error.
22+
* The underlying error, if this error resulted from an earlier error.
23+
*
24+
* @remarks
25+
* This property is a backwards-compatible alias for {@link Error.cause}.
2326
*/
2427
public readonly innerError: Error | undefined;
2528

2629
public constructor(message: string, range: TextRange, innerError?: Error) {
27-
super(_formatMessage(message, range));
30+
super(_formatMessage(message, range), innerError === undefined ? undefined : { cause: innerError });
2831

2932
// Boilerplate for extending a system class
3033
//
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
2+
// See LICENSE in the project root for license information.
3+
4+
import { ParseError } from '../ParseError';
5+
import { TextRange } from '../TextRange';
6+
7+
test('omits cause when no inner error is supplied', () => {
8+
const error: ParseError = new ParseError('Parse failed', TextRange.empty);
9+
10+
expect(error.message).toBe('Parse failed');
11+
expect(error.name).toBe('Error');
12+
expect(error.stack).toContain('Error: Parse failed');
13+
expect(Object.hasOwn(error, 'cause')).toBe(false);
14+
expect(error.cause).toBeUndefined();
15+
expect(Object.getOwnPropertyDescriptor(error, 'innerError')).toEqual({
16+
configurable: true,
17+
enumerable: true,
18+
value: undefined,
19+
writable: true
20+
});
21+
});
22+
23+
test('exposes the inner error as the standard cause and legacy alias', () => {
24+
const innerError: Error = new Error('Inner failure');
25+
const error: ParseError = new ParseError('Parse failed', TextRange.empty, innerError);
26+
27+
expect(error.cause).toBe(innerError);
28+
expect(error.innerError).toBe(innerError);
29+
expect(Object.getOwnPropertyDescriptor(error, 'cause')).toEqual({
30+
configurable: true,
31+
enumerable: false,
32+
value: innerError,
33+
writable: true
34+
});
35+
});

0 commit comments

Comments
 (0)