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 isFastifyError #86

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ function createError (code, message, statusCode = 500, Base = Error) {
return FastifyError
}

function isFastifyError (obj) {
return (
Object.prototype.toString.call(obj) === '[object Error]' &&
obj.name === 'FastifyError'
Uzlopak marked this conversation as resolved.
Show resolved Hide resolved
)
}

module.exports = createError
module.exports.default = createError
module.exports.createError = createError
module.exports.isFastifyError = isFastifyError
15 changes: 15 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

const test = require('tap').test
const createError = require('..')
const isFastifyError = require('..').isFastifyError

test('Create error with zero parameter', t => {
t.plan(6)
Expand Down Expand Up @@ -154,3 +155,17 @@ test('Create the error without the new keyword', t => {
t.equal(err.statusCode, 500)
t.ok(err.stack)
})

test('isFastifyError', t => {
t.plan(8)

const NewError = createError('CODE', 'Not available')
t.equal(isFastifyError(NewError()), true)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should consider the createError instances as "application error" instead of Fastify Error

t.equal(isFastifyError(new NewError()), true)
t.equal(isFastifyError(undefined), false)
t.equal(isFastifyError(null), false)
t.equal(isFastifyError({}), false)
t.equal(isFastifyError('FastifyError'), false)
t.equal(isFastifyError(new Error('Generic Error')), false)
t.equal(isFastifyError(new Error('FastifyError')), false)
})
3 changes: 3 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ declare namespace createError {
(a?: any, b?: any, c?: any): FastifyError;
readonly prototype: FastifyError;
}
function isFastifyError (obj: any): obj is createError.FastifyError
}

declare function isFastifyError (obj: any): obj is createError.FastifyError
10 changes: 8 additions & 2 deletions types/index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import createError, { FastifyError, FastifyErrorConstructor } from '..'
import createError, { FastifyError, FastifyErrorConstructor, isFastifyError } from '..'
import { expectType } from 'tsd'

const CustomError = createError('ERROR_CODE', 'message')
Expand All @@ -7,4 +7,10 @@ const err = new CustomError()
expectType<FastifyError>(err)
expectType<string>(err.code)
expectType<string>(err.message)
expectType<number>(err.statusCode!)
expectType<number>(err.statusCode!)

expectType<boolean>(isFastifyError(err))

if (isFastifyError(err)) {
expectType<FastifyError>(err)
}