Skip to content

Commit d342d0d

Browse files
committed
feat: pass sentry instance
Closes #41
1 parent c17cdf7 commit d342d0d

File tree

3 files changed

+69
-149
lines changed

3 files changed

+69
-149
lines changed

README.md

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ const resolvers = {
2929
}
3030
}
3131

32+
Sentry.init({
33+
dsn: process.env.SENTRY_DSN,
34+
environment: process.env.NODE_ENV,
35+
release: process.env.npm_package_version
36+
})
37+
3238
const sentryMiddleware = sentry({
33-
config: {
34-
dsn: process.env.SENTRY_DSN,
35-
environment: process.env.NODE_ENV,
36-
release: process.env.npm_package_version
37-
},
39+
sentryInstance: Sentry,
3840
withScope: (scope, error, context) => {
3941
scope.setUser({
4042
id: context.authorization.userId,
@@ -58,7 +60,7 @@ serve.start(() => `Server running on http://localhost:4000`)
5860

5961
```ts
6062
export interface Options<Context> {
61-
config: Sentry.NodeOptions
63+
sentryInstance: Sentry
6264
withScope?: ExceptionScope<Context>
6365
captureReturnedErrors?: boolean
6466
forwardErrors?: boolean
@@ -77,7 +79,7 @@ The `withScope` option is a function that is called with the current Sentry scop
7779
```ts
7880
type ExceptionScope<Context> = (
7981
scope: Sentry.Scope,
80-
error: any,
82+
error: Error,
8183
context: Context,
8284
) => void
8385
```
@@ -86,7 +88,7 @@ type ExceptionScope<Context> = (
8688

8789
| property | required | description |
8890
| ----------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
89-
| `config` | true | [Sentry's config object](https://docs.sentry.io/error-reporting/configuration/?platform=node) |
91+
| `sentryInstance` | true | [Sentry's instance](https://docs.sentry.io/error-reporting/configuration/?platform=node) |
9092
| `withScope` | false | Function to modify the [Sentry context](https://docs.sentry.io/enriching-error-data/context/?platform=node) to send with the captured error. |
9193
| `captureReturnedErrors` | false | Capture errors returned from other middlewares, e.g., `graphql-shield` [returns errors](https://github.com/maticzav/graphql-shield#custom-errors) from rules and resolvers |
9294
| `forwardErrors` | false | Should middleware forward errors to the client or block them. |

src/index.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import * as Sentry from '@sentry/node'
1+
import { Scope } from '@sentry/core'
22

33
import { IMiddlewareFunction } from 'graphql-middleware/dist/types'
44

55
export type ExceptionScope<Context> = (
6-
scope: Sentry.Scope,
7-
error: any,
6+
scope: Scope,
7+
error: Error,
88
context: Context,
99
) => void
1010

1111
// Options for graphql-middleware-sentry
1212
export interface Options<Context> {
13-
config: Sentry.NodeOptions
13+
sentryInstance: any
1414
withScope?: ExceptionScope<Context>
1515
captureReturnedErrors?: boolean
1616
forwardErrors?: boolean
@@ -19,45 +19,43 @@ export interface Options<Context> {
1919
export class SentryError extends Error {}
2020

2121
export const sentry = <Context>({
22-
config = {},
22+
sentryInstance = null,
2323
withScope,
2424
captureReturnedErrors = false,
2525
forwardErrors = false,
2626
}: Options<Context>): IMiddlewareFunction => {
2727
// Check if Sentry DSN is present
28-
if (!config.dsn) {
29-
throw new SentryError(`Missing dsn parameter in configuration.`)
28+
if (!sentryInstance) {
29+
throw new SentryError(`The Sentry instance is missing in the options.`)
3030
}
3131

32-
// Init Sentry
33-
Sentry.init(config)
34-
3532
// Return middleware resolver
36-
return async function(resolve, parent, args, ctx, info) {
33+
return async (resolve, parent, args, ctx, info) => {
3734
try {
3835
const res = await resolve(parent, args, ctx, info)
3936
if (captureReturnedErrors && res instanceof Error) {
40-
captureException(res, ctx, withScope)
37+
captureException(sentryInstance, res, ctx, withScope)
4138
}
4239
return res
43-
} catch (err) {
44-
captureException(err, ctx, withScope)
40+
} catch (error) {
41+
captureException(sentryInstance, error, ctx, withScope)
4542

4643
// Forward error
4744
if (forwardErrors) {
48-
throw err
45+
throw error
4946
}
5047
}
5148
}
5249
}
5350

5451
function captureException<Context>(
55-
err,
52+
sentryInstance,
53+
error: Error,
5654
ctx: Context,
5755
withScope: ExceptionScope<Context>,
5856
) {
59-
Sentry.withScope(scope => {
60-
withScope(scope, err, ctx)
61-
Sentry.captureException(err)
57+
sentryInstance.withScope(scope => {
58+
withScope(scope, error, ctx)
59+
sentryInstance.captureException(error)
6260
})
6361
}

0 commit comments

Comments
 (0)