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

fix error messages #3277

Merged
merged 1 commit into from Aug 23, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'))
})