Skip to content

Improve errors user experience #2842

@delvedor

Description

@delvedor

Fastify has many assertions around in the codebase to make sure that the user didn't misconfigure something, for instance, it checks if a route has been declared twice or if a plugin is not a function.
This is great, we vastly reduce the possibility of weird runtime issues, but for historical reasons the errors we are emitting are everything but user friendly.

We should improve this situation, as it is a big pain point in our user experience.

Example of double route declaration:

'use strict'
const fastify = require('fastify')()
fastify.get('/', () => {})
fastify.get('/', () => {})
fastify.listen(3000, console.log)
▶ node index.js
AssertionError [ERR_ASSERTION]: Method 'GET' already declared for route '/'
    at Router._insert (.../fst-err/node_modules/find-my-way/index.js:304:9)
    at Router._on (.../fst-err/node_modules/find-my-way/index.js:197:8)
    at Router.on (.../fst-err/node_modules/find-my-way/index.js:74:8)
    at Object.afterRouteAdded (.../fst-err/node_modules/fastify/lib/route.js:253:16)
    at .../fst-err/node_modules/fastify/lib/route.js:187:25
    at Object._encapsulateThreeParam (.../fst-err/node_modules/avvio/boot.js:545:7)
    at Boot.timeoutCall (.../fst-err/node_modules/avvio/boot.js:447:5)
    at Boot.callWithCbOrNextTick (.../fst-err/node_modules/avvio/boot.js:428:19)
    at Boot._after (.../fst-err/node_modules/avvio/boot.js:273:26)
    at Plugin.exec (.../fst-err/node_modules/avvio/plugin.js:131:17) {
  generatedMessage: false,
  code: 'ERR_ASSERTION',
  actual: false,
  expected: true,
  operator: '=='
}

What I would expect instead is:

FastifyError: Method 'GET' already declared for route '/'
   at fastify.get('/', () => {}) (.../fst-err/index.js:4:9)

Which is quite complex, given that our route declaration is asynchronous.

Another example, this time with bad plugin type:

'use strict'
const fastify = require('fastify')()
fastify.register(null)
fastify.listen(3000, console.log)
▶ node index.js
.../fst-err/node_modules/avvio/boot.js:200
    throw new Error('plugin must be a function or a promise')
    ^

Error: plugin must be a function or a promise
    at assertPlugin (.../fst-err/node_modules/avvio/boot.js:200:11)
    at Boot._addPlugin (.../fst-err/node_modules/avvio/boot.js:233:12)
    at Boot.use (.../fst-err/node_modules/avvio/boot.js:209:25)
    at Object.server.<computed> [as register] (.../fst-err/node_modules/avvio/boot.js:33:14)
    at Object.<anonymous> (.../fst-err/index.js:5:9)
    at Module._compile (internal/modules/cjs/loader.js:1063:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)

As you can see, the error is not being helpful at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussionIssues or PRs with this label will never stalefeature requestNew feature to be added

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions