Lifecycle
Following the schema of the internal lifecycle of Fastify.
On the right branch of every section there is the next phase of the lifecycle, on the left branch there is the corresponding error code that will be generated if the parent throws an error (note that all the errors are automatically handled by Fastify).
Incoming Request
โ
โโโถ Routing
โ
โโโถ Instance Logger
โ
4**/5** โโโดโโถ onRequest Hook
โ
4**/5** โโโดโโถ preParsing Hook
โ
4**/5** โโโดโโถ Parsing
โ
4**/5** โโโดโโถ preValidation Hook
โ
400 โโโดโโถ Validation
โ
4**/5** โโโดโโถ preHandler Hook
โ
4**/5** โโโดโโถ User Handler
โ
โโโถ Reply
โ
4**/5** โโโดโโถ preSerialization Hook
โ
โโโถ onSend Hook
โ
4**/5** โโโดโโถ Outgoing Response
โ
โโโถ onResponse Hook
At any point before or during the User Handler
, reply.hijack()
can be called to prevent Fastify from:
- Running all the following hooks and user handler
- Sending the response automatically
NB (*): If reply.raw
is used to send a response back to the user, onResponse
hooks will still be executed
Reply Lifecycle
Whenever the user handles the request, the result may be:
- in async handler: it returns a payload
- in async handler: it throws an
Error
- in sync handler: it sends a payload
- in sync handler: it sends an
Error
instance
If the reply was hijacked, we skip all the below steps. Otherwise, when it is being submitted, the data flow performed is the following:
โ
schema validation Error
โ
โโโถ schemaErrorFormatter
โ
reply sent โโโ JSON โโดโ Error instance
โ
โ โ
throw an Error
โ
send or return โ โ
โ โ โ
โ โผ โ
reply sent โโโ JSON โโดโ Error instance โโโถ setErrorHandler โโโโโโโ
โ
reply sent โโโ JSON โโดโ Error instance โโโถ onError Hook
โ
โโโถ reply sent
Note: reply sent
means that the JSON payload will be serialized by:
- the reply serialized if set
- or by the serializer compiler when a JSON schema has been set for the returning HTTP status code
- or by the default
JSON.stringify
function