From dcc536ccfc898878a230511ef7b41f8a553c193d Mon Sep 17 00:00:00 2001 From: Igor Savin Date: Mon, 4 Dec 2023 12:02:34 +0200 Subject: [PATCH] add isError --- package.json | 14 ++++++------ src/utils/typeUtils.spec.ts | 45 +++++++++++++++++++++++++++++++++++++ src/utils/typeUtils.ts | 6 +++++ 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index f1b8a21..64516ae 100644 --- a/package.json +++ b/package.json @@ -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.8", "prettier": "^3.0.3", - "typescript": "^5.2.2", + "typescript": "^5.3.2", "vitest": "^0.34.6", "zod": "^3.22.4" } diff --git a/src/utils/typeUtils.spec.ts b/src/utils/typeUtils.spec.ts index 873aedb..9ce58e1 100644 --- a/src/utils/typeUtils.spec.ts +++ b/src/utils/typeUtils.spec.ts @@ -3,6 +3,7 @@ import { PublicNonRecoverableError } from '../errors/PublicNonRecoverableError' import { hasMessage, + isError, isInternalError, isObject, isPublicNonRecoverableError, @@ -10,6 +11,50 @@ import { } 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({ diff --git a/src/utils/typeUtils.ts b/src/utils/typeUtils.ts index 27d8cfc..74551b0 100644 --- a/src/utils/typeUtils.ts +++ b/src/utils/typeUtils.ts @@ -22,6 +22,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]' + ) +} + export function isPublicNonRecoverableError(error: unknown): error is InternalError { return isObject(error) && error.name === 'PublicNonRecoverableError' }