Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use addEventProcessor for compatibility with sentry 4 and 5 #20

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
@@ -40,34 +40,35 @@ exports.register = (server, options) => {

// catch request errors/warnings/etc (default: only errors) and capture them with sentry
server.events.on({ name: 'request', channels: opts.channels }, async (request, event) => {
// format a sentry event from the request and triggered event
const sentryEvent = await Sentry.Parsers.parseError(event.error);
Sentry.Handlers.parseRequest(sentryEvent, request.raw.req);
Sentry.withScope(scope => { // thus use a temp scope and re-assign it
scope.addEventProcessor(_sentryEvent => {
fiws marked this conversation as resolved.
Show resolved Hide resolved
// format a sentry event from the request and triggered event
const sentryEvent = Sentry.Handlers.parseRequest(_sentryEvent, request.raw.req);
fiws marked this conversation as resolved.
Show resolved Hide resolved

// overwrite events request url if a baseUrl is provided
if (opts.baseUri) {
if (opts.baseUri.slice(-1) === '/') opts.baseUri = opts.baseUri.slice(0, -1);
sentryEvent.request.url = opts.baseUri + request.path;
}
// overwrite events request url if a baseUrl is provided
if (opts.baseUri) {
if (opts.baseUri.slice(-1) === '/') opts.baseUri = opts.baseUri.slice(0, -1);
sentryEvent.request.url = opts.baseUri + request.path;
}

// set severity according to the filters channel
sentryEvent.level = event.channel;
// set severity according to the filters channel
sentryEvent.level = event.channel;

// use request credentials for capturing user
if (opts.trackUser) sentryEvent.user = request.auth && request.auth.credentials;
if (sentryEvent.user) {
Object.keys(sentryEvent.user) // hide credentials
.filter(prop => /^(p(ass)?w(or)?(d|t)?|secret)?$/i.test(prop))
.forEach(prop => delete sentryEvent.user[prop]);
}
// use request credentials for capturing user
if (opts.trackUser) sentryEvent.user = request.auth && request.auth.credentials;
if (sentryEvent.user) {
Object.keys(sentryEvent.user) // hide credentials
.filter(prop => /^(p(ass)?w(or)?(d|t)?|secret)?$/i.test(prop))
.forEach(prop => delete sentryEvent.user[prop]);
}

// some SDK identificator
sentryEvent.sdk = { name: 'sentry.javascript.node.hapi', version };
// some SDK identificator
sentryEvent.sdk = { name: 'sentry.javascript.node.hapi', version };
return sentryEvent;
});

// @sentry/node.captureEvent does not support scope parameter, if it's not from Sentry.Hub(?)
Sentry.withScope(scope => { // thus use a temp scope and re-assign it
Hoek.applyToDefaults(scope, request.sentryScope);
Sentry.captureEvent(sentryEvent);
Sentry.captureException(event.error);
});
});

5 changes: 1 addition & 4 deletions schema.js
Original file line number Diff line number Diff line change
@@ -9,14 +9,11 @@ const levels = Object.values(Severity).filter(level => typeof level === 'string'
const sentryClient = joi.object().keys({
configureScope: joi.func().minArity(1),
Scope: joi.func().required(),
Parsers: joi.object().keys({
parseError: joi.func().minArity(1).required(),
}).unknown().required(),
Handlers: joi.object().keys({
parseRequest: joi.func().minArity(2).required(),
}).unknown().required(),
withScope: joi.func().minArity(1).required(),
captureEvent: joi.func().minArity(1).required(),
captureException: joi.func().minArity(1).required(),
}).unknown();

const sentryOptions = joi.object().keys({
20 changes: 10 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
@@ -20,9 +20,9 @@ test('requires a dsn or a Scope (sentry opts vs. sentry client)', async t => {
client: {},
},
}), {
name: 'ValidationError',
message: /Invalid hapi-sentry options/,
});
name: 'ValidationError',
message: /Invalid hapi-sentry options/,
});

t.deepEqual(err.details.map(d => d.message), [
'"dsn" is required',
@@ -33,24 +33,24 @@ test('requires a dsn or a Scope (sentry opts vs. sentry client)', async t => {
test('uses a custom sentry client', async t => {
const { server } = t.context;

const error = new Error('Error to be thrown');

server.route({
method: 'GET',
path: '/route',
handler() {
throw new Error('This will be overwritten by custom sentry client');
throw error;
},
});

const deferred = defer();
const parsedError = { request: {}, test: 'testEvent' };
const customSentry = {
Scope: class Scope {},
/* eslint-disable no-unused-vars */ // arity needed to pass joi validation
Parsers: { parseError: x => parsedError },
Handlers: { parseRequest: (x, y) => {} },
withScope: cb => cb({}),
Handlers: { parseRequest: (x, y) => { } },
withScope: cb => cb({ addEventProcessor: () => { } }),
/* eslint-enable no-unused-var */
captureEvent: deferred.resolve,
captureException: deferred.resolve,
};

// check exposing of custom client
@@ -70,7 +70,7 @@ test('uses a custom sentry client', async t => {
});

const event = await deferred.promise;
t.is(event, parsedError);
t.is(event, error);
});

test('exposes the sentry client', async t => {