Skip to content

Commit a3778ac

Browse files
authored
fix: Use process.emitWarning to log wrapping errors (#114)
1 parent 543effd commit a3778ac

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

hook.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
44

55
const { URL } = require('url')
6+
const { inspect } = require('util')
67
const specifiers = new Map()
78
const isWin = process.platform === 'win32'
89

@@ -115,6 +116,14 @@ function isBareSpecifier (specifier) {
115116
}
116117
}
117118

119+
function emitWarning (err) {
120+
// Unfortunately, process.emitWarning does not output the full error
121+
// with error.cause like console.warn does so we need to inspect it when
122+
// tracing warnings
123+
const warnMessage = process.execArgv.includes('--trace-warnings') ? inspect(err) : err
124+
process.emitWarning(warnMessage)
125+
}
126+
118127
/**
119128
* Processes a module's exports and builds a set of setter blocks.
120129
*
@@ -303,7 +312,7 @@ register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(rea
303312
// it would be very tricky to debug
304313
const err = new Error(`'import-in-the-middle' failed to wrap '${realUrl}'`)
305314
err.cause = cause
306-
console.warn(err)
315+
emitWarning(err)
307316

308317
// Revert back to the non-iitm URL
309318
url = realUrl

0 commit comments

Comments
 (0)