Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,11 @@ export function getErrorFromOAuthErrorResponse(
if (key !== 'message') {
Object.defineProperty(newError, key, {
value: (err as {} as {[index: string]: string})[key],
writable: false,
enumerable: true,
// Keep stack writable, configurable and non-enumerable, as on a
// regular Error, so callers can still append context to it.
writable: key === 'stack',
enumerable: key !== 'stack',
configurable: key === 'stack',
});
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -500,4 +500,24 @@ describe('getErrorFromOAuthErrorResponse', () => {
assert.strictEqual(actualError.name, expectedError.name);
assert.strictEqual(actualError.stack, expectedError.stack);
});

it('should keep the copied stack writable, configurable and non-enumerable', () => {
const originalError = new Error('Original error message');
const resp = {
error: 'invalid_grant',
error_description: 'ID Token is stale to sign-in.',
};

const actualError = getErrorFromOAuthErrorResponse(resp, originalError);
const descriptor = Object.getOwnPropertyDescriptor(actualError, 'stack');
assert.strictEqual(descriptor?.writable, true);
assert.strictEqual(descriptor?.enumerable, false);
Comment thread
Om-singhaI marked this conversation as resolved.
assert.strictEqual(descriptor?.configurable, true);
// Consumers append causal context to error.stack; in strict mode that
// throws a TypeError when the property is read-only.
assert.doesNotThrow(() => {
actualError.stack += '\nCaused by: wrapped error';
});
assert.ok(actualError.stack?.endsWith('\nCaused by: wrapped error'));
});
});
Loading