Express.js
Decipher’s Express.js SDK allows for automatic capture of errors including helpful details like logs and raw payload data.
Step 1: Sign in to Decipher
Sign in to Decipher using your work email address and a customerId
will be automatically created for you, which you’ll use in the next step.
Step 2: Integrate the SDK
Install the SDK
npm install @decipher-sdk/express@latest
Instrumenting your Express app
Import Decipher
, initialize it, and call app.use
on it as below.
IMPORTANT: You must add
Decipher.Handlers.requestHandler()
before any other middleware andDecipher.Handlers.errorHandler
immediately after your controllers/endpoints to ensure errors are captured correctly.
// ...other imports
const Decipher = require('@decipher-sdk/express');
const app = express();
// A codebase name of your choice to identify errors in Decipher.
Decipher.init({ codebaseId: "CODEBASE_NAME_OF_MY_CHOICE", customerId: "MY_CUSTOMER_ID_FROM_STEP_1"})
// IMPORTANT: The Decipher Request Handler must be the first middleware on the app.
app.use(Decipher.Handlers.requestHandler());
// Your controllers
app.get("/", ...)
app.get("/submit", ...)
// IMPORTANT: The Decipher error handler must be immediately after the controllers.
app.use(Decipher.Handlers.errorHandler);
That’s it! Decipher will now monitor and surface all your errors and exceptions on the dashboard.
Capturing error details on handled exceptions
If you want to capture error details like the stack trace for handled exceptions (e.g. in a try
/catch
block) you can use Decipher.captureError(error)
:
// ...other imports
const Decipher = require('@decipher-sdk/express');
app.get('/order', function (req, res) {
try {
thisFunctionMightThrowErrors()
} catch (error) {
Decipher.captureError(error)
res.status(500).send('An error occurred');
}
});
Step 3 [IMPORTANT]: Add your sourcemaps
When your code runs on production, it is likely minified and/or bundled, so we need to upload sourcemaps to see stack traces with the right code.
Follow the Uploading Sourcemaps instructions to get unminified stack traces in your errors.
Boom! Decipher will now include the stack trace and other context to help you and Decipher’s AI debug.