1
+ // @ts -check
1
2
const { get } = require ( 'http' ) ;
2
3
const nock = require ( 'nock' ) ;
3
4
const nodeSDK = require ( '@sentry/node' ) ;
@@ -132,14 +133,16 @@ function ensureWrappedGet(importedGet, url) {
132
133
// As of Next 12.1, creating a `NextServer` instance (which we do immediately upon starting this test runner) loads
133
134
// `_app`, which has the effect of initializing the SDK. So, unless something's gone wrong, we should always be able
134
135
// to find the integration
135
- let httpIntegration ;
136
- try {
137
- httpIntegration = nodeSDK . getCurrentHub ( ) . getClient ( ) . getIntegration ( nodeSDK . Integrations . Http ) ;
138
- } catch ( err ) {
136
+ const hub = nodeSDK . getCurrentHub ( ) ;
137
+ const client = hub . getClient ( ) ;
138
+
139
+ if ( ! client ) {
139
140
console . warn ( `Warning: Sentry SDK not set up at \`NextServer\` initialization. Request URL: ${ url } ` ) ;
140
141
return importedGet ;
141
142
}
142
143
144
+ const httpIntegration = client . getIntegration ( nodeSDK . Integrations . Http ) ;
145
+
143
146
// This rewraps `http.get` and `http.request`, which, at this point, look like `nockWrapper(sentryWrapper(get))` and
144
147
// `nockWrapper(sentryWrapper(request))`. By the time we're done with this function, they'll look like
145
148
// `sentryWrapper(nockWrapper(sentryWrapper(get)))` and `sentryWrapper(nockWrapper(sentryWrapper(request)))`,
@@ -160,7 +163,12 @@ function ensureWrappedGet(importedGet, url) {
160
163
//
161
164
// TODO: add in a "don't do this twice" check (in `fill`, maybe moved from `wrap`), so that we don't wrap the outer
162
165
// wrapper with a third wrapper
163
- httpIntegration . setupOnce ( ) ;
166
+ if ( httpIntegration ) {
167
+ httpIntegration . setupOnce (
168
+ ( ) => undefined ,
169
+ ( ) => hub ,
170
+ ) ;
171
+ }
164
172
165
173
// now that we've rewrapped it, grab the correct version of `get` for use in our tests
166
174
const httpModule = require ( 'http' ) ;
0 commit comments