Skip to content

Commit

Permalink
test: cover errorHandler
Browse files Browse the repository at this point in the history
  • Loading branch information
salmanm committed Oct 16, 2020
1 parent 6514687 commit bd4caaf
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 5 deletions.
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,19 @@ function fastifyStatic (fastify, opts, next) {
prefix = opts.prefix[opts.prefix.length - 1] === '/' ? opts.prefix : (opts.prefix + '/')
}

const errorHandler = (error, request, reply) => {
if (error && error.code === 'ERR_STREAM_PREMATURE_CLOSE') {
reply.request.raw.destroy()
return
}

fastify.errorHandler(error, request, reply)
}

// Set the schema hide property if defined in opts or true by default
const routeOpts = {
schema: { hide: typeof opts.schemaHide !== 'undefined' ? opts.schemaHide : true },
errorHandler: fastify.errorHandler ? (error, request, reply) => {
if (error && error.code !== 'ERR_STREAM_PREMATURE_CLOSE') {
fastify.errorHandler(error, request, reply)
}
} : undefined
errorHandler: fastify.errorHandler ? errorHandler : undefined
}

if (opts.decorateReply !== false) {
Expand Down
76 changes: 76 additions & 0 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2175,3 +2175,79 @@ t.test('inject support', async (t) => {
t.strictEqual(response.statusCode, 200)
t.strictEqual(response.body.toString(), indexContent)
})

t.test('routes should use custom errorHandler premature stream close', t => {
t.plan(3)

const pluginOptions = {
root: path.join(__dirname, '/static'),
prefix: '/static/'
}

const fastify = Fastify()

if (!fastify.errorHandler) {
fastify.errorHandler = (_, __, reply) => {
reply.send('default-error-obj')
}
}

fastify.addHook('onRoute', function (routeOptions) {
t.ok(routeOptions.errorHandler instanceof Function)

routeOptions.onRequest = (request, reply, done) => {
const fakeError = new Error()
fakeError.code = 'ERR_STREAM_PREMATURE_CLOSE'
done(fakeError)
}
})

fastify.register(fastifyStatic, pluginOptions)
t.tearDown(fastify.close.bind(fastify))

fastify.inject({
method: 'GET',
url: '/static/index.html'
}, (err, response) => {
t.error(err)
t.equal(response, null)
})
})

t.test('routes should fallback to default errorHandler', t => {
t.plan(3)

const pluginOptions = {
root: path.join(__dirname, '/static'),
prefix: '/static/'
}

const fastify = Fastify()

if (!fastify.errorHandler) {
fastify.errorHandler = (_, __, reply) => {
reply.send({ default: 'error-obj' })
}
}

fastify.addHook('onRoute', function (routeOptions) {
t.ok(routeOptions.errorHandler instanceof Function)

routeOptions.preHandler = (request, reply, done) => {
const fakeError = new Error()
fakeError.code = 'SOMETHING_ELSE'
done(fakeError)
}
})

fastify.register(fastifyStatic, pluginOptions)
t.tearDown(fastify.close.bind(fastify))

fastify.inject({
method: 'GET',
url: '/static/index.html'
}, (err, response) => {
t.error(err)
t.deepEqual(JSON.parse(response.payload), { default: 'error-obj' })
})
})

0 comments on commit bd4caaf

Please sign in to comment.