Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use process.emitWarning to log errors #114

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

const { URL } = require('url')
const { inspect } = require('util')
const specifiers = new Map()
const isWin = process.platform === 'win32'

Expand Down Expand Up @@ -115,6 +116,14 @@ function isBareSpecifier (specifier) {
}
}

function emitWarning (err) {
// Unfortunately, process.emitWarning does not output the full error
// with error.cause like console.warn does so we need to inspect it when
// tracing warnings
const warnMessage = process.execArgv.includes('--trace-warnings') ? inspect(err) : err
process.emitWarning(warnMessage)
}

/**
* Processes a module's exports and builds a set of setter blocks.
*
Expand Down Expand Up @@ -303,7 +312,7 @@ register(${JSON.stringify(realUrl)}, _, set, ${JSON.stringify(specifiers.get(rea
// it would be very tricky to debug
const err = new Error(`'import-in-the-middle' failed to wrap '${realUrl}'`)
err.cause = cause
console.warn(err)
emitWarning(err)

// Revert back to the non-iitm URL
url = realUrl
Expand Down
Loading