Skip to content

Commit

Permalink
fix: Explicitly look for instance of Error. (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Sep 8, 2021
1 parent c3789e4 commit 849df82
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/helpers/errors.test.ts
@@ -1,5 +1,11 @@
import { isError, __testing__ } from './errors';

class MyError extends Error {
constructor(msg: string) {
super(msg);
}
}

describe('errors', () => {
test.each`
value | expected
Expand All @@ -10,7 +16,9 @@ describe('errors', () => {
${{ message: '', name: '' }} | ${true}
${{ message: '', name: '', stack: '' }} | ${true}
${{ message: '', name: '', stack: 5 }} | ${false}
`('isError', ({ value, expected }) => {
${new Error('test error')} | ${true}
${new MyError('test error')} | ${true}
`('isError $value', ({ value, expected }) => {
expect(isError(value)).toBe(expected);
});

Expand Down
1 change: 1 addition & 0 deletions src/helpers/errors.ts
Expand Up @@ -18,6 +18,7 @@ const allowStringOrUndefined: AllowedTypes = {
// };

export function isError(e: unknown): e is Error {
if (e instanceof Error) return true;
if (!e || typeof e !== 'object') return false;
const ex = <Error>e;
return typeof ex.name == 'string' && typeof ex.message == 'string' && typeof ex.stack in allowStringOrUndefined;
Expand Down

0 comments on commit 849df82

Please sign in to comment.