Skip to content

Commit

Permalink
Merge pull request #147 from MoSattler/main
Browse files Browse the repository at this point in the history
feat: pass on sentry integrations
  • Loading branch information
PatrickHeneise authored May 9, 2024
2 parents 3ca8282 + 798b594 commit ecfc206
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ jobs:
strategy:
matrix:
node_version:
- 14
- 16
- 20

steps:
- uses: actions/checkout@v2
Expand Down
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
33 changes: 33 additions & 0 deletions test/integrations.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'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.name = "TestIntegration"
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 ecfc206

Please sign in to comment.