Uncaught errors are likely to cause memory leaks, file descriptor leaks and other major production issues. Domains were introduced to try fixing this issue, but they did not. Given the fact that it is not possible to process all uncaught errors sensibly, the best way to deal with them at the moment is to crash. In case of promises, make sure to handle errors correctly.
Fastify follows an all-or-nothing approach and aims to be lean and optimal as much as possible. Thus, the developer is responsible for making sure that the errors are handled properly. Most of the errors are usually a result of unexpected input data, so we recommend specifying a JSON.schema validation for your input data.
Fastify tries to catch as many uncaught errors it can without hindering performance. This includes:
- synchronous routes, e.g.
app.get('/', () => { throw new Error('kaboom') })
async
routes, e.g.app.get('/', async () => { throw new Error('kaboom') })
In those two cases, the error will safely be caught by the promise and routed to the default error handler of Fastify for a generic Internal Server Error
response. For customizing this behaviour, you should use setErrorHandler
.
The router received an invalid url.
The parser for this content type was already registered.
The Content-Type
should be a string.
The content type cannot be an empty string.
An invalid handler was passed for the content type.
The provided parse type is not supported. Accepted values are string
or buffer
.
The request body is larger than the provided limit.
The received media type is not supported (i.e. there is no suitable Content-Type
parser for it).
Request body size did not match Content-Length.
A decorator with the same name is already registered.
The decorator cannot be registered due to a missing dependency.
The hook name must be a string.
The hook callback must be a function.
The logger accepts either a 'stream'
or a 'file'
as the destination.
A response was already sent.
You cannot use send
inside the onError
hook.
Reply payload can either be a string
or a Buffer
.
The schema provided does not have $id
property.
A schema with the same $id
already exists.
The JSON schema provided for validation to a route is not valid.
The JSON schema provided for serialization of a route response is not valid.
A promise may not be fulfilled with 'undefined' when statusCode is not 204.
Undefined error has occurred.