Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/alex-ppg/fastify-sentry
A Fastify plugin for attaching Sentry to the framework
https://github.com/alex-ppg/fastify-sentry
Last synced: 1 day ago
JSON representation
A Fastify plugin for attaching Sentry to the framework
- Host: GitHub
- URL: https://github.com/alex-ppg/fastify-sentry
- Owner: alex-ppg
- License: gpl-3.0
- Created: 2018-12-01T21:16:18.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T06:51:45.000Z (almost 2 years ago)
- Last Synced: 2024-11-03T19:05:30.239Z (8 days ago)
- Language: JavaScript
- Size: 114 KB
- Stars: 18
- Watchers: 2
- Forks: 13
- Open Issues: 17
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fastify Sentry Plugin using the Sentry SDK
[![NPM](https://nodei.co/npm/fastify-sentry.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/fastify-sentry/)
[![CircleCI](https://circleci.com/gh/alex-ppg/fastify-sentry.svg?style=svg)](https://circleci.com/gh/alex-ppg/fastify-sentry)
## Installation
```bash
npm i fastify-sentry -s
```## Usage
```javascript
const fastify = require("fastify")();
// Should be first declaration
fastify.register(
require("fastify-sentry"),
{
dsn: "https://[email protected]/0000000",
environment: "local",
errorHandler: (err, request, reply) => {
// You can specify a custom behavior depending on the context of "request", generate a unique identifier etc.
if (request.raw.url === "/") {
reply.send({
error: 500,
message: 'The main path "/" didn\'t work!',
payload: err
});
} else {
reply.send({
error: 501,
message: "Some other path failed!",
payload: err
});
}
}
},
err => {
if (err) throw err;
}
);fastify.get("/", async (request, reply) => {
// Errors in async functions are automatically caught
throw new Error("Oops");
reply.send({ hello: "world" });
});fastify.get("/other-path", (request, reply) => {
// On the other hand, you need to pass the Error object to "reply.send" for it to be logged as Fastify does not catch errors in synchronous functions!
reply.send(new Error("I did it again!"));
});
```## Description
This plugin adds the Sentry SDK error handler by using `fastify.setErrorHandler`. This means that the Sentry SDK will only catch any errors thrown in routes with `async` functions. In order to properly log errors thrown within synchronous functions, you need to pass the error object within `reply.send`. It also adds certain metadata, namely the `path` and the `ip` parameters of `req.raw`, to both the `User` context and `Tag` context of Sentry.
## Options
| Option | Description |
| ------ | ------------------------------------------------------------------- |
| `dsn` | Required, the DSN specified by Sentry.io to properly log errors to. |## Author
[Alex Papageorgiou]([email protected])
## License
Licensed under [GPLv3](./LICENSE).