Connect Hapi to Bugsink
When a Hapi request fails, Bugsink shows you the exception, the application frame that raised it, and the HTTP request that reached the route.
This guide covers the initial setup and Hapi’s automatic capture of route errors. You’ll connect an existing application, then make one request fail to confirm that it reaches Bugsink.
To follow along, create a Bugsink project and copy its DSN. The quickstart guide shows where to find it.
Install the SDK
Install the open-source Sentry SDK alongside Hapi. Bugsink accepts the Sentry event protocol, so the SDK can send events directly to your project:
npm install --save-exact @sentry/node@11.14.0npm install --save-exact @sentry/node@11.14.0
Initialize the SDK before Hapi
Create an instrumentation module which initializes the SDK from the DSN in the environment:
import * as Sentry from "@sentry/node"; Sentry.init({ dsn: process.env.SENTRY_DSN, });import * as Sentry from "@sentry/node"; Sentry.init({ dsn: process.env.SENTRY_DSN, });
Load it with Node’s --import option before the server module. The SDK needs to
initialize before Node loads Hapi:
"scripts": { "start": "node --import ./instrument.mjs server.mjs" },"scripts": { "start": "node --import ./instrument.mjs server.mjs" },
Set SENTRY_DSN in the environment where the server runs:
export SENTRY_DSN=https://key@bugsink.example/1export SENTRY_DSN=https://key@bugsink.example/1
Attach the Hapi error handler
Register your routes first. Then attach the SDK’s Hapi error handler before starting the server:
await Sentry.setupHapiErrorHandler(server); await server.start();await Sentry.setupHapiErrorHandler(server); await server.start();
Hapi still converts an uncaught route exception into its normal HTTP 500 response. The SDK observes it without requiring a capture call in the route.
Trigger an unhandled route exception
To verify the setup, this checkout route throws when a product is missing:
server.route({ method: "GET", path: "/checkout/{sku}", handler: (request) => { const product = inventory.get(request.params.sku); if (!product) { throw new ProductNotFound(`Product ${request.params.sku} is not in inventory`); } return { reserved: product.name }; }, });server.route({ method: "GET", path: "/checkout/{sku}", handler: (request) => { const product = inventory.get(request.params.sku); if (!product) { throw new ProductNotFound(`Product ${request.params.sku} is not in inventory`); } return { reserved: product.name }; }, });
Start the application as usual, then request a missing product:
npm start :::bash curl http://127.0.0.1:3000/checkout/hapi-missingnpm start :::bash curl http://127.0.0.1:3000/checkout/hapi-missing
Hapi returns an HTTP 500 response and the SDK reports the exception.
See the error in Bugsink
Open your Bugsink project and select the new ProductNotFound issue. Its event
points to the failing route line and includes the GET request.
ProductNotFound
That’s it: uncaught Hapi route errors will now be reported to Bugsink. Your routes remain ordinary application code, while Bugsink receives their JavaScript stack and request context.
For handled exceptions, messages, tags, structured context, and breadcrumbs, continue with the Node.js guide.
