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

Docs for managing errors in an async hook #2176

Merged
merged 1 commit into from
Apr 4, 2020
Merged
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
10 changes: 9 additions & 1 deletion docs/Hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ By using hooks you can interact directly with the lifecycle of Fastify. There ar
- [onError](#onerror)
- [onSend](#onsend)
- [onResponse](#onresponse)
- [Manage Errors from a hook](#manage-errors-from-a-hook)
- [Respond to a request from a hook](#respond-to-a-request-from-a-hook)
- [Application Hooks](#application-hooks)
- [onClose](#onclose)
- [onRoute](#onroute)
Expand Down Expand Up @@ -206,9 +208,15 @@ fastify.addHook('preHandler', (request, reply, done) => {
done(new Error('Some error'))
})
```

*The error will be handled by [`Reply`](https://github.com/fastify/fastify/blob/master/docs/Reply.md#errors).*

Or if you're using `async/await` you can just throw an error:
```js
fastify.addHook('onResponse', async (request, reply) => {
throw new Error('Some error')
})
```

### Respond to a request from a hook

If needed, you can respond to a request before you reach the route handler,
Expand Down