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

Fix issue with source maps #156

Merged
merged 9 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 8 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
36 changes: 36 additions & 0 deletions src/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,42 @@ describe('wrapError', () => {
});
});

describe('if the original error has a length === 2', () => {
it('returns an error with cause', () => {
// Save original Error
const OriginalError = global.Error;

// Create a mock error with a cause
class MockError extends Error {
constructor(message: string, options: { cause?: Error } = {}) {
super(message);
this.cause = options.cause;
}

cause: Error | undefined;
}
// Set length to 2
Object.defineProperty(MockError, 'length', { value: 2 });

// Replace global Error with MockError
global.Error = MockError as unknown as ErrorConstructor;
BelfordZ marked this conversation as resolved.
Show resolved Hide resolved

// Define your original error and message
const originalError = new Error('original error');
const message = 'new error message';

// Call your function
const result = wrapError(originalError, message);

// Assert that the error has the expected properties
expect(result.message).toBe(message);
expect(result.cause).toBe(originalError);

// Restore the original Error constructor
global.Error = OriginalError;
});
});

describe('if the original error is an object but not an Error instance', () => {
describe('if the message is a non-empty string', () => {
it('combines a string version of the original error and message together in a new Error', () => {
Expand Down
29 changes: 16 additions & 13 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,22 @@ export function wrapError<Throwable>(
message: string,
): Error & { code?: string } {
if (isError(originalError)) {
const error: Error & { code?: string } =
Error.length === 2
? // This branch is getting tested by using the Node version that
// supports `cause` on the Error constructor.
// istanbul ignore next
// Also, for some reason `tsserver` is not complaining that the
// Error constructor doesn't support a second argument in the editor,
// but `tsc` does. I'm not sure why, but we disable this in the
// meantime.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
new Error(message, { cause: originalError })
: new ErrorWithCause(message, { cause: originalError });
let error: Error & { code?: string };
// This branch is getting tested by using the Node version that
// supports `cause` on the Error constructor.
// istanbul ignore next
BelfordZ marked this conversation as resolved.
Show resolved Hide resolved
if (Error.length === 2) {
// Also, for some reason `tsserver` is not complaining that the
// Error constructor doesn't support a second argument in the editor,
// but `tsc` does. Error causes are not supported by our current tsc target (ES2020, we need ES2022 to make this work)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
error = new Error(message, { cause: originalError });
} else {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
error = new ErrorWithCause(message, { cause: originalError });
}

if (isErrorWithCode(originalError)) {
error.code = originalError.code;
Expand Down