# hapi-sentry

> hapi plugin for @sentry/node

Latest version **4.0.1-2** (published 2024-01-17) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install hapi-sentry
pnpm add hapi-sentry
yarn add hapi-sentry
bun add hapi-sentry
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 4.0.1-2 |
| Published | 2024-01-17 |
| First published | 2018-11-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 24.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 25 |
| Author | Christian Hotz |
| Maintainers | fiws, guischdi |
| Keywords | hapi, sentry, plugin |

## Links

- npm: https://www.npmjs.com/package/hapi-sentry
- Repository: https://github.com/hydra-newmedia/hapi-sentry
- Homepage: https://github.com/hydra-newmedia/hapi-sentry#readme
- Issues: https://github.com/hydra-newmedia/hapi-sentry/issues
- npm.io page: https://npm.io/package/hapi-sentry

## Dependencies (4)

- [zod](https://npm.io/package/zod.md) ^3.22.4
- [@hapi/hoek](https://npm.io/package/@hapi/hoek.md) ^11.0.2
- [@sentry/node](https://npm.io/package/@sentry/node.md) ^7.38.0
- [@sentry/types](https://npm.io/package/@sentry/types.md) ^7.38.0

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 4.0.1-2 (latest) — 2024-01-17
- 4.0.1-1 — 2024-01-17
- 4.0.0 — 2023-06-12
- 4.0.0-0 — 2023-02-27
- 3.2.0 — 2021-03-17
- 3.1.0 — 2020-09-09
- 3.0.0 — 2020-01-20
- 2.1.0 — 2019-10-22
- 2.0.3 — 2019-08-15
- 2.0.2 — 2019-07-10
- 2.0.1 — 2019-07-01
- 2.0.0 — 2019-07-01
- 1.2.1 — 2019-06-07
- 1.2.0 — 2019-06-05
- 1.1.0 — 2019-02-11
- … 4 more at https://npm.io/package/hapi-sentry/versions

## README

# hapi-sentry

[![package on npm](https://img.shields.io/npm/v/hapi-sentry.svg)](https://www.npmjs.com/package/hapi-sentry)
[![GitHub Workflow Status](https://github.com/hydra-newmedia/hapi-sentry/actions/workflows/nodejs.yml/badge.svg)](https://github.com/hydra-newmedia/hapi-sentry/actions/workflows/nodejs.yml)
![node 14+ required](https://img.shields.io/badge/node-14%2B-brightgreen.svg)
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/hydra-newmedia/hapi-sentry/master/LICENSE)

A hapi [plugin](https://hapijs.com/api#plugins) for
request error logging to [Sentry](https://sentry.io/).

## Usage

Use the hapi plugin like this:
```JavaScript
const server = hapi.server();
await server.register({
  plugin: require('hapi-sentry'),
  options: {
    client: { dsn: 'dsn-here' },
  },
});
```

This setup will:
* Initialize Sentry regularly, which should capture all global errors and unhandled promise rejects
* Capture all unhandled exceptions thrown or returned in routes
* Use request data and `request.auth.credentials` to enhance errors from routes

You can use the following options to customize this behavior further.

## Options

The plugin options, you can pass in while registering are the following:

| property                  | type          | description                                                                                                                  |
|:--------------------------|:--------------|:-----------------------------------------------------------------------------------------------------------------------------|
| `baseUri`                 | string        | [uri](https://github.com/hapijs/joi/blob/master/API.md#stringurioptions) to be used as base for captured urls                |
| `scope.tags`              | object        | An array of tags to be sent with every event                                                                                 |
| `scope.tags.name`         | string        | The name of a tag                                                                                                            |
| `scope.tags.value`        | any           | The value of a tag                                                                                                           |
| `scope.extra`             | object        | An object of arbitrary format to be sent as extra data on every event                                                        |
| `client`                  | object        | **required** A [@sentry/node](https://www.npmjs.com/package/@sentry/node) instance which was already initialized (using `Sentry.init`) OR an options object to be passed to an internally initialized [@sentry/node](https://www.npmjs.com/package/@sentry/node) (`client.dsn` is only required in the latter case) |
| `client.dsn`              | string/false  | **required** The Dsn used to connect to Sentry and identify the project. If false, the SDK will not send any data to Sentry. |
| `client.debug`            | boolean       | Turn debug mode on/off                                                                                                       |
| `client.release`          | string        | Tag events with the version/release identifier of your application                                                           |
| `client.environment`      | string        | The current environment of your application (e.g. `'production'`)                                                            |
| `client.sampleRate`       | number        | A global sample rate to apply to all events (0 - 1)                                                                          |
| `client.maxBreadcrumbs`   | number        | The maximum number of breadcrumbs sent with events. Default: `100`                                                           |
| `client.attachStacktrace` | any           | Attaches stacktraces to pure capture message / log integrations                                                              |
| `client.sendDefaultPii`   | boolean       | If this flag is enabled, certain personally identifiable information is added by active integrations                         |
| `client.serverName`       | string        | Overwrite the server name (device name)                                                                                      |
| `client.beforeSend`       | func          | A callback invoked during event submission, allowing to optionally modify the event before it is sent to Sentry              |
| `client.beforeBreadcrumb` | func          | A callback invoked when adding a breadcrumb, allowing to optionally modify it before adding it to future events.             |
| `trackUser`               | boolean       | Whether or not to track the user via the per-request scope. Default: `true`                                                  |
| `catchLogErrors`          | boolean/array | Handles [capturing server.log and request.log events](#capturing-serverlog-and-requestlog-events). Default: `false`          |
| `useDomainPerRequest`     | boolean       | Whether or not to use [Domains](https://nodejs.org/docs/latest-v12.x/api/domain.html) for seperating request processing. Only activate this feature, if you really need to seperate breadcrumbs, etc. of requests. It utilizes a deprecated Node.js feature which reduces [performance](https://github.com/hydra-newmedia/hapi-sentry/pull/21#issuecomment-574602486). Default: `false` |

The `baseUri` option is used internally to get a correct URL in sentry issues.
The `scope` option is used to set up a global
[`Scope`](http://getsentry.github.io/sentry-javascript/classes/hub.scope.html)
for all events and the
[`client`](http://getsentry.github.io/sentry-javascript/interfaces/node.nodeoptions.html) option
is used as a Sentry instance or to initialize an internally used Sentry instance.

The internally used client (initialized in either way) is accessible through
`server.plugins['hapi-sentry'].client`.

## Using your own Sentry instance

You can pass a `Sentry` instance to  the `client` option if you already initialized your own like this:

```js
const server = hapi.server();
const Sentry = require('sentry');
Sentry.init({ dsn: 'dsn-here' });
await server.register({ plugin: require('hapi-sentry'), options: { client: Sentry } });
```

## Scope

You can alter the scope of an event in every
hapi [route handler](https://hapijs.com/api#route.options.handler)
by accessing `request.sentryScope`.
Just use some of the [`Scope`](http://getsentry.github.io/sentry-javascript/classes/hub.scope.html)s
methods to add breadcrumbs, set extra, fingerprint or level information, etc. like this:

```JavaScript
server.route({
  method: 'GET',
  path: '/your/route',
  handler(request) {
    try {
      // ... some logic here
    } catch (error) {
      request.sentryScope.setExtra('someErrorSpecificInfo', 'yourInformation');
      throw error;
    }
  },
});
```

## Capturing server.log and request.log events

You can enable capturing of `request.log` and `server.log` events using the `catchLogErrors` option.
All events which are `Error` objects and are tagged by one of `['error', 'fatal', 'fail']` are
automatically being tracked when `catchLogErrors` is set to `true`,  e.g.:

```js
request.log(['error', 'foo'], new Error('Oh no!'));
server.log(['error', 'foo'], new Error('No no!'));
```

The considered tags can be changed by setting `catchLogErrors` to a custom array of tags like
`['error', 'warn', 'failure']`.

## Capturing the request body

`hapi-sentry` currently does not capture the body for performance reasons. You can use the following snippet to capture the body in all sentry errors:

```js
server.ext({
  type: 'onPostAuth',
  method(request, h) {
    request.payload && request.sentryScope.setExtra('payload', request.payload);
    return h.continue;
  },
});
```

---
_Source: https://npm.io/package/hapi-sentry · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
