Skip to content

Commit

Permalink
fix error messages (#3277)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthyk committed Aug 23, 2021
1 parent fd359a0 commit b213983
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/route.js
Expand Up @@ -143,20 +143,21 @@ function buildRouting (options) {
}
}

const path = opts.url || opts.path

if (!opts.handler) {
throw new Error(`Missing handler function for ${opts.method}:${opts.url} route.`)
throw new Error(`Missing handler function for ${opts.method}:${path} route.`)
}

if (opts.errorHandler !== undefined && typeof opts.errorHandler !== 'function') {
throw new Error(`Error Handler for ${opts.method}:${opts.url} route, if defined, must be a function`)
throw new Error(`Error Handler for ${opts.method}:${path} route, if defined, must be a function`)
}

validateBodyLimitOption(opts.bodyLimit)

const prefix = this[kRoutePrefix]

this.after((notHandledErr, done) => {
const path = opts.url || opts.path
if (path === '/' && prefix.length && opts.method !== 'HEAD') {
switch (opts.prefixTrailingSlash) {
case 'slash':
Expand Down
22 changes: 22 additions & 0 deletions test/route.test.js
Expand Up @@ -1246,3 +1246,25 @@ test('Will not try to re-createprefixed HEAD route if it already exists and expo

t.ok(true)
})

test('Correct error message is produced if "path" option is used', t => {
t.plan(2)

const fastify = Fastify()

t.throws(() => {
fastify.route({
method: 'GET',
path: '/test'
})
}, new Error('Missing handler function for GET:/test route.'))

t.throws(() => {
fastify.route({
method: 'POST',
url: '/test',
handler: function () {},
errorHandler: ''
})
}, new Error('Error Handler for POST:/test route, if defined, must be a function'))
})

0 comments on commit b213983

Please sign in to comment.