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 isError typeguard #86

Merged
merged 2 commits into from
Dec 4, 2023
Merged
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
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@
"prepublishOnly": "npm run build"
},
"dependencies": {
"pino": "^8.16.1",
"undici": "5.27.2",
"pino": "^8.16.2",
"undici": "^5.28.2",
"undici-retry": "^3.2.0"
},
"devDependencies": {
"@types/node": "^20.8.10",
"@typescript-eslint/eslint-plugin": "^6.9.1",
"@typescript-eslint/parser": "^6.9.1",
"@types/node": "^20.10.3",
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"@vitest/coverage-v8": "^0.34.6",
"auto-changelog": "^2.4.0",
"eslint": "^8.53.0",
"eslint": "^8.55.0",
"eslint-config-prettier": "^9.0.0",
"eslint-plugin-import": "^2.29.0",
"eslint-plugin-prettier": "^5.0.1",
"eslint-plugin-vitest": "^0.3.9",
"prettier": "^3.0.3",
"typescript": "^5.2.2",
"typescript": "^5.3.2",
"vitest": "^0.34.6",
"zod": "^3.22.4",
"vite": "4.5.0"
Expand Down
45 changes: 45 additions & 0 deletions src/utils/typeUtils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,58 @@ import { PublicNonRecoverableError } from '../errors/PublicNonRecoverableError'

import {
hasMessage,
isError,
isInternalError,
isObject,
isPublicNonRecoverableError,
isStandardizedError,
} from './typeUtils'

describe('typeUtils', () => {
describe('isError', () => {
it('true for InternalError', () => {
const error = new InternalError({
message: 'dummy',
errorCode: 'code',
})

expect(isError(error)).toBe(true)
})

it('true for PublicNonRecoverableError', () => {
const error = new PublicNonRecoverableError({
message: 'dummy',
errorCode: 'code',
})

expect(isError(error)).toBe(true)
})

it('true for Error', () => {
const error = new Error('bam')

expect(isError(error)).toBe(true)
})

it('false for string', () => {
const error = 'bam'

expect(isError(error)).toBe(false)
})

it('false for a number', () => {
const error = 43

expect(isError(error)).toBe(false)
})

it('false for a plain object', () => {
const error = {}

expect(isError(error)).toBe(false)
})
})

describe('isInternalError', () => {
it('true for InternalError', () => {
const error = new InternalError({
Expand Down
6 changes: 6 additions & 0 deletions src/utils/typeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export function isInternalError(error: unknown): error is InternalError {
return isObject(error) && error.name === 'InternalError'
}

export function isError(maybeError: unknown): maybeError is Error {
return (
maybeError instanceof Error || Object.prototype.toString.call(maybeError) === '[object Error]'
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See nodejs/node#50488 (comment)
This is how Node.js itself was doing it, as instanceof can be unreliable sometimes.

)
}

export function isPublicNonRecoverableError(error: unknown): error is PublicNonRecoverableError {
return isObject(error) && error.name === 'PublicNonRecoverableError'
}