Skip to content

Commit 026c7c5

Browse files
committed
feat: migrate to @sentry/node
- Refactored `sentry()` params to use destructuring with defaults, so the normalization is no longer necessary - Added `extras` to use Sentry’s `scope.setExtra()` to enrich the error data - Added `captureReturnedErrors` option to capture errors from other middlewares that return errors instead of throwing, e.g., `graphql-shield` https://github.com/maticzav/graphql-shield#custom-errors Closes #29
1 parent 9cf52ba commit 026c7c5

File tree

1 file changed

+35
-22
lines changed

1 file changed

+35
-22
lines changed

src/index.ts

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
import { config as raven, captureException, ConstructorOptions } from 'raven'
1+
import * as Sentry from '@sentry/node'
2+
import * as _ from 'lodash'
3+
24
import { IMiddlewareFunction } from 'graphql-middleware/dist/types'
35

6+
interface Extra {
7+
name: string
8+
path: string
9+
}
10+
411
// Options for graphql-middleware-sentry
512
export interface Options {
613
dsn: string
7-
config?: ConstructorOptions
14+
config?: Sentry.NodeOptions
15+
extras?: Extra[]
16+
captureReturnedErrors?: boolean
817
forwardErrors?: boolean
918
}
1019

@@ -14,41 +23,45 @@ export class SentryError extends Error {
1423
}
1524
}
1625

17-
function normalizeOptions(options: Options): Options {
26+
export const sentry = ({
27+
dsn,
28+
config = {},
29+
extras = [],
30+
captureReturnedErrors = false,
31+
forwardErrors = false,
32+
}: Options): IMiddlewareFunction => {
1833
// Check if Sentry DSN is present
19-
if (!options.dsn) {
34+
if (!dsn) {
2035
throw new SentryError(`Missing dsn parameter in configuration.`)
2136
}
2237

23-
return {
24-
dsn: options.dsn,
25-
config: options.config !== undefined ? options.config : {},
26-
forwardErrors:
27-
options.forwardErrors !== undefined ? options.forwardErrors : false,
28-
}
29-
}
30-
31-
export const sentry = (_options: Options): IMiddlewareFunction => {
32-
const options = normalizeOptions(_options)
33-
34-
// Configure and install Raven
35-
raven(options.dsn, options.config).install()
38+
// Init Sentry
39+
Sentry.init({ dsn, ...config })
3640

3741
// Return middleware resolver
3842
return async function(resolve, parent, args, ctx, info) {
3943
try {
4044
const res = await resolve(parent, args, ctx, info)
45+
if (captureReturnedErrors && res instanceof Error) {
46+
captureException(res, ctx, extras)
47+
}
4148
return res
4249
} catch (err) {
43-
// Capture exception
44-
captureException(err, {
45-
req: ctx.request,
46-
})
50+
captureException(err, ctx, extras)
4751

4852
// Forward error
49-
if (options.forwardErrors) {
53+
if (forwardErrors) {
5054
throw err
5155
}
5256
}
5357
}
5458
}
59+
60+
function captureException(err, ctx, extras: Extra[]) {
61+
Sentry.withScope(scope => {
62+
extras.forEach(extra => {
63+
scope.setExtra(extra.name, _.get(ctx, extra.path))
64+
})
65+
Sentry.captureException(err)
66+
})
67+
}

0 commit comments

Comments
 (0)