Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hapi-sentry

Package Overview
Dependencies
Maintainers
2
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-sentry

hapi plugin for @sentry/node

  • 4.0.1-2
  • next
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

hapi-sentry

package on npm GitHub Workflow Status node 14+ required GitHub license

A hapi plugin for request error logging to Sentry.

Usage

Use the hapi plugin like this:

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:

propertytypedescription
baseUristringuri to be used as base for captured urls
scope.tagsobjectAn array of tags to be sent with every event
scope.tags.namestringThe name of a tag
scope.tags.valueanyThe value of a tag
scope.extraobjectAn object of arbitrary format to be sent as extra data on every event
clientobjectrequired A @sentry/node instance which was already initialized (using Sentry.init) OR an options object to be passed to an internally initialized @sentry/node (client.dsn is only required in the latter case)
client.dsnstring/falserequired The Dsn used to connect to Sentry and identify the project. If false, the SDK will not send any data to Sentry.
client.debugbooleanTurn debug mode on/off
client.releasestringTag events with the version/release identifier of your application
client.environmentstringThe current environment of your application (e.g. 'production')
client.sampleRatenumberA global sample rate to apply to all events (0 - 1)
client.maxBreadcrumbsnumberThe maximum number of breadcrumbs sent with events. Default: 100
client.attachStacktraceanyAttaches stacktraces to pure capture message / log integrations
client.sendDefaultPiibooleanIf this flag is enabled, certain personally identifiable information is added by active integrations
client.serverNamestringOverwrite the server name (device name)
client.beforeSendfuncA callback invoked during event submission, allowing to optionally modify the event before it is sent to Sentry
client.beforeBreadcrumbfuncA callback invoked when adding a breadcrumb, allowing to optionally modify it before adding it to future events.
trackUserbooleanWhether or not to track the user via the per-request scope. Default: true
catchLogErrorsboolean/arrayHandles capturing server.log and request.log events. Default: false
useDomainPerRequestbooleanWhether or not to use Domains 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. 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 for all events and the client 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:

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 by accessing request.sentryScope. Just use some of the Scopes methods to add breadcrumbs, set extra, fingerprint or level information, etc. like this:

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.:

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:

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

Keywords

FAQs

Package last updated on 17 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc