Skip to content

Commit

Permalink
update error tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
umakantp committed Jan 30, 2025
1 parent 244320d commit 139d673
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 60 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"**/.vscode": true,
"dist": true,
},
"eslint.enable": true,
"typescript.preferences.importModuleSpecifier": "non-relative",
"typescript.preferences.importModuleSpecifierEnding": "minimal",
"[typescript]": {
Expand Down
94 changes: 34 additions & 60 deletions src/graphql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { User } from 'src/types'
import * as jwt from 'src/utils/jwt'
import logger from 'src/utils/logger'
import sentry from 'src/utils/sentry'
import { delay } from 'src/utils/utils'

const getContext = async (req: FastifyRequest) => {
const token = req.headers.authorization?.replace('Bearer ', '')
Expand All @@ -30,29 +29,6 @@ const getContext = async (req: FastifyRequest) => {
}
}

/*
app.addHook('preHandler', (req, _res) => {
sentry.withScope(async (scope) => {
const { user } = await getContext(req)
const gqlParams = req.body as { operationName?: string, variables?: Record<string, any>, query?: string }
scope.addEventProcessor(event => sentry.Handlers.parseRequest(event, req))
let sentryUser: SentryUser = { ip_address: req.ip }
if (user) {
sentryUser = { ...sentryUser, id: user.id, name: model.user.name(user), isSuperadmin: model.user.isSuperadmin(user) }
}
scope.setUser(sentryUser)
scope.setTags({ 'graphql.name': gqlParams?.operationName || 'none' })
scope.setExtra('query', gqlParams?.query)
if (gqlParams.variables) {
scope.setContext('variables', gqlParams.variables)
}
})
})
*/

async function setup(app: FastifyInstance) {
const schema = await loadSchema()

Expand All @@ -61,47 +37,45 @@ async function setup(app: FastifyInstance) {
graphiql: process.env.MODE !== 'prod' ? true : false,
path: '/api',
context: getContext,
})

await app.ready()

app.graphql.addHook('onResolution', async (execution, context) => {
await delay(100)

sentry.withScope(async (scope) => {
const req = context.reply.request
const { user } = await getContext(req)
const gqlParams = req.body as { operationName?: string, variables?: Record<string, any>, query?: string }

let sentryUser: SentryUser = { ip_address: req.ip }
if (user) {
sentryUser = { ...sentryUser, id: user.id, name: model.user.name(user), isSuperadmin: model.user.isSuperadmin(user) }
}
scope.setUser(sentryUser)
errorFormatter: (execution, ...args) => {
sentry.withScope(async (scope) => {
const req = args[0].reply.request
const { user } = await getContext(req)
const gqlParams = req.body as { operationName?: string, variables?: Record<string, any>, query?: string }

let sentryUser: SentryUser = { ip_address: req.ip }
if (user) {
sentryUser = { ...sentryUser, id: user.id, name: model.user.name(user), isSuperadmin: model.user.isSuperadmin(user) }
}
scope.setUser(sentryUser)

scope.setTags({ 'graphql.name': gqlParams?.operationName || 'none' })
scope.setExtra('query', gqlParams?.query)
if (gqlParams.variables) {
scope.setContext('variables', gqlParams.variables)
}
scope.setTags({ 'graphql.name': gqlParams?.operationName || 'none' })
scope.setExtra('query', gqlParams?.query)
if (gqlParams.variables) {
scope.setContext('variables', gqlParams.variables)
}

if (execution.errors) {
for (const err of execution.errors) {
if (err.extensions) {
// These are input validation errors and we don't want to track them as we know
// many users will make mistakes in their input.
continue
if (execution.errors) {
for (const err of execution.errors) {
if (err.extensions) {
// These are input validation errors and we don't want to track them as we know
// many users will make mistakes in their input.
continue
}
if (err.path) {
scope.setTag('path', err.path.join('.'))
}
// scope.setTag('error.code', err.extensions?.code || err.name)
scope.setExtra('error.message', err.message)
sentry.captureException(err, scope)
}
if (err.path) {
scope.setTag('path', err.path.join('.'))
}
// scope.setTag('error.code', err.extensions?.code || err.name)
scope.setExtra('error.message', err.message)
sentry.captureException(err, scope)
}
}
})
})
return mercurius.defaultErrorFormatter(execution, ...args)
}
})

await app.ready()
}

export default { setup }
Expand Down

0 comments on commit 139d673

Please sign in to comment.