Spaces:
Runtime error
Runtime error
File size: 4,084 Bytes
23ac194 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
<h1 align="center">Fastify</h1>
## Lifecycle
<a id="lifecycle"></a>
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
<a id="reply-lifecycle"></a>
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](./Server.md#setreplyserializer) if set
- or by the [serializer compiler](./Server.md#setserializercompiler) when a JSON
schema has been set for the returning HTTP status code
- or by the default `JSON.stringify` function
|