Skip to content

Commit

Permalink
fix: Use process.emitWarning to log wrapping errors (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
timfish authored Jun 29, 2024
1 parent 543effd commit a3778ac
Showing 1 changed file with 10 additions and 1 deletion.
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

0 comments on commit a3778ac

Please sign in to comment.