Skip to content

Commit 06339b5

Browse files
committed
feat: make sentryInstance optional
1 parent d342d0d commit 06339b5

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

README.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,11 @@ 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-
3832
const sentryMiddleware = sentry({
39-
sentryInstance: Sentry,
33+
config: {
34+
dsn: process.env.SENTRY_DSN,
35+
environment: process.env.NODE_ENV
36+
},
4037
withScope: (scope, error, context) => {
4138
scope.setUser({
4239
id: context.authorization.userId,
@@ -56,11 +53,31 @@ const server = GraphQLServer({
5653
serve.start(() => `Server running on http://localhost:4000`)
5754
```
5855

56+
### Using a Sentry instance
57+
58+
In cases where you want to use your own instance of Sentry to use it in other places in your application you can pass the `sentryInstance`. The `config` property should not be passed as an option.
59+
60+
#### Example usage with a Sentry instance
61+
62+
```ts
63+
Sentry.init({
64+
dsn: process.env.SENTRY_DSN,
65+
})
66+
67+
const sentryMiddleware = sentry({
68+
sentryInstance: Sentry,
69+
withScope: (scope, error, context) => {
70+
scope.setExtra('origin', context.request.headers.origin)
71+
},
72+
})
73+
```
74+
5975
## API & Configuration
6076

6177
```ts
6278
export interface Options<Context> {
63-
sentryInstance: Sentry
79+
sentryInstance?: Sentry
80+
config?: Sentry.NodeOptions
6481
withScope?: ExceptionScope<Context>
6582
captureReturnedErrors?: boolean
6683
forwardErrors?: boolean
@@ -88,11 +105,16 @@ type ExceptionScope<Context> = (
88105

89106
| property | required | description |
90107
| ----------------------- | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
91-
| `sentryInstance` | true | [Sentry's instance](https://docs.sentry.io/error-reporting/configuration/?platform=node) |
108+
| `sentryInstance` | false | [Sentry's instance](https://docs.sentry.io/error-reporting/configuration/?platform=node) |
109+
| `config` | false | [Sentry's config object](https://docs.sentry.io/error-reporting/configuration/?platform=node) |
92110
| `withScope` | false | Function to modify the [Sentry context](https://docs.sentry.io/enriching-error-data/context/?platform=node) to send with the captured error. |
93111
| `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 |
94112
| `forwardErrors` | false | Should middleware forward errors to the client or block them. |
95113

114+
#### Note
115+
116+
If `sentryInstance` is not passed then `config.dsn` is required and vice-versa.
117+
96118
## License
97119

98120
This project is licensed under the [MIT License](LICENSE.md).

src/index.ts

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

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

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

1111
// Options for graphql-middleware-sentry
1212
export interface Options<Context> {
13-
sentryInstance: any
13+
sentryInstance?: any
14+
config?: Sentry.NodeOptions
1415
withScope?: ExceptionScope<Context>
1516
captureReturnedErrors?: boolean
1617
forwardErrors?: boolean
@@ -20,13 +21,22 @@ export class SentryError extends Error {}
2021

2122
export const sentry = <Context>({
2223
sentryInstance = null,
24+
config = {},
2325
withScope,
2426
captureReturnedErrors = false,
2527
forwardErrors = false,
2628
}: Options<Context>): IMiddlewareFunction => {
27-
// Check if Sentry DSN is present
28-
if (!sentryInstance) {
29-
throw new SentryError(`The Sentry instance is missing in the options.`)
29+
// Check if either sentryInstance or config.dsn is present
30+
if (!sentryInstance && !config.dsn) {
31+
throw new SentryError(
32+
`Missing the sentryInstance or the dsn parameter in configuration.`,
33+
)
34+
}
35+
36+
if (!sentryInstance && config.dsn) {
37+
// Init Sentry
38+
sentryInstance = Sentry
39+
Sentry.init(config)
3040
}
3141

3242
// Return middleware resolver

0 commit comments

Comments
 (0)