Skip to content

Commit 459aa90

Browse files
BridgeARsabrenner
authored andcommitted
refactor: make sure promises as context would be preserved (#6277)
This addresses potential issues similar to one reported about Moleculer, where the promise is used for context in the library.
1 parent 9a05797 commit 459aa90

File tree

2 files changed

+8
-14
lines changed

2 files changed

+8
-14
lines changed

packages/datadog-instrumentations/src/mongodb-core.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,17 +199,15 @@ function instrumentPromise (operation, command, instance, args, server, ns, ops,
199199
return startCh.runStores(ctx, () => {
200200
const promise = command.apply(instance, args)
201201

202-
return promise.then(function (res) {
202+
promise.then(function (res) {
203203
ctx.result = res
204-
return finishCh.runStores(ctx, () => {
205-
return res
206-
})
204+
finishCh.publish(ctx)
207205
}, function (err) {
208206
ctx.error = err
209207
errorCh.publish(ctx)
210208
finishCh.publish(ctx)
211-
212-
throw err
213209
})
210+
211+
return promise
214212
})
215213
}

packages/datadog-instrumentations/src/next.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,16 @@ function instrument (req, res, handler, error) {
162162

163163
// promise should only reject when propagateError is true:
164164
// https://github.com/vercel/next.js/blob/cee656238a/packages/next/server/api-utils/node.ts#L547
165-
return promise.then(
165+
promise.then(
166166
result => finish(ctx, result),
167167
err => finish(ctx, null, err)
168168
)
169+
return promise
169170
} catch (e) {
170171
// this will probably never happen as the handler caller is an async function:
171172
// https://github.com/vercel/next.js/blob/cee656238a/packages/next/server/api-utils/node.ts#L420
172-
return finish(ctx, null, e)
173+
finish(ctx, null, e)
174+
throw e
173175
}
174176
})
175177
}
@@ -198,12 +200,6 @@ function finish (ctx, result, err) {
198200
}
199201

200202
finishChannel.publish(ctx)
201-
202-
if (err) {
203-
throw err
204-
}
205-
206-
return result
207203
}
208204

209205
// also wrapped in dist/server/future/route-handlers/app-route-route-handler.js

0 commit comments

Comments
 (0)