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

Add error utils #146

Merged
merged 13 commits into from
Oct 17, 2023
Prev Previous commit
Next Next commit
Fix build
  • Loading branch information
mcmire committed Oct 16, 2023
commit 176db3afc23375a94c48a7807f05745ff458f4f7
20 changes: 12 additions & 8 deletions src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ErrorWithCause } from 'pony-cause';

import { hasProperty, isNullOrUndefined, isObject } from './misc';
import { isNullOrUndefined, isObject } from './misc';

/**
* Type guard for determining whether the given value is an instance of Error.
Expand Down Expand Up @@ -92,14 +92,18 @@ export function wrapError<Throwable>(
message: string,
): Error & { code?: string } {
if (isError(originalError)) {
// @ts-expect-error This is okay
const error: Error & { code: string } =
'cause' in Error.prototype
? // This is getting tested using different Node versions
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
new Error(message, {
cause: originalError,
})
// 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 });

if (isErrorWithCode(originalError)) {
Expand Down