Skip to content

Commit

Permalink
feat: pass on sentry integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
MoSattler committed May 9, 2024
1 parent 3ca8282 commit 599e66b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 4 deletions.
11 changes: 7 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ const extractRequestData = require('./lib/extractRequestData.js')
* dsn: string,
* tracing: boolean = false,
* errorHandler?: (error: Error, request: import('fastify').FastifyRequest, reply: import('fastify').FastifyReply) => void,
* errorFilter?: (error: Error, request: import('fastify').FastifyRequest) => boolean
* errorFilter?: (error: Error, request: import('fastify').FastifyRequest) => boolean,
* integrations?: Sentry.NodeOptions['integrations']
* }} opts
* @param {function} next
* @returns {void}
Expand All @@ -26,7 +27,8 @@ function sentryConnector(fastify, opts, next) {
new Sentry.Integrations.Http({ tracing: true }),
new Tracing.Integrations.Express({
fastify
})
}),
...(opts.integrations ?? [])
]
}

Expand Down Expand Up @@ -70,8 +72,9 @@ function sentryConnector(fastify, opts, next) {
})

fastify.setErrorHandler((error, request, reply) => {
if (opts.errorFilter && ! opts.errorFilter(error, request)) {
process.env.NODE_ENV !== 'production' && console.warn('Error not reported to Sentry', error)
if (opts.errorFilter && !opts.errorFilter(error, request)) {
process.env.NODE_ENV !== 'production' &&
console.warn('Error not reported to Sentry', error)
} else {
Sentry.withScope((scope) => {
if (request && request.user && request.user.sub) {
Expand Down
32 changes: 32 additions & 0 deletions test/integrations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

/* eslint-disable node/no-unpublished-require */
const Fastify = require('fastify')
const tap = require('tap')
const sinon = require('sinon')
const fastifySentry = require('../index')

/**
* TestIntegration class .
* @implements {import("@sentry/types").Integration}
*/
class TestIntegration {
constructor() {
this.setupOnce = sinon.spy();
}
}

tap.test('adding more integrations', async (test) => {
test.teardown(() => fastify.close())

const testIntegration = new TestIntegration()

const fastify = Fastify()
fastify.register(fastifySentry, {
dsn: 'https://[email protected]/0000000',
integrations: [testIntegration]
})

await fastify.ready()
test.ok(testIntegration.setupOnce.called, 'setupOnce should have been called')
})

0 comments on commit 599e66b

Please sign in to comment.