Skip to content

Commit 17428f7

Browse files
committed
fix: display the error stack in load-addon
1 parent f1d0438 commit 17428f7

File tree

4 files changed

+59
-31
lines changed

4 files changed

+59
-31
lines changed

lib/load-addon.js

Lines changed: 18 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/load-addon.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

script/install.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ function cmakeTs() {
1212
})
1313
}
1414

15+
function devWarn(message) {
16+
if (process.env.NODE_ENV !== "production") {
17+
console.warn(message)
18+
}
19+
}
20+
1521
function main() {
1622
if (process.env.npm_config_build_from_source === "true") {
1723
cmakeTs()
1824
} else {
1925
try {
2026
require("../lib/load-addon.js")
2127
} catch (error) {
22-
console.error(error)
28+
devWarn(error)
2329
cmakeTs()
2430
}
2531
}

src/load-addon.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,60 @@
11
import path from "path"
22
import fs from "fs"
33

4+
function errStr(error: unknown) {
5+
return error instanceof Error
6+
? `${error.name}: ${error.message}\n${error.stack}`
7+
: String(error)
8+
}
9+
10+
function devWarn(message: string) {
11+
if (process.env.NODE_ENV !== "production") {
12+
console.warn(message)
13+
}
14+
}
15+
416
function findAddon(): any | undefined {
17+
let addon: undefined | any = undefined
518
try {
6-
const addonParentDir = path.resolve(path.join(
7-
__dirname,
8-
"..",
9-
"build",
10-
process.platform,
11-
process.arch,
12-
"node",
13-
))
19+
const addonParentDir = path.resolve(
20+
path.join(
21+
__dirname,
22+
"..",
23+
"build",
24+
process.platform,
25+
process.arch,
26+
"node",
27+
),
28+
)
1429
const addOnAbiDirs = fs.readdirSync(addonParentDir).sort((a, b) => {
1530
return Number.parseInt(b, 10) - Number.parseInt(a, 10)
1631
})
1732

1833
// try each available addon ABI
19-
let addon: undefined | any
2034
for (const addOnAbiDir of addOnAbiDirs) {
2135
const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node")
2236
try {
2337
addon = require(addonPath)
2438
break
2539
} catch (err) {
2640
if (fs.existsSync(addonPath)) {
27-
console.error(
28-
`Failed to load addon at ${addonPath}: ${err}\nTrying others...`,
41+
devWarn(
42+
`Failed to load addon at ${addonPath}: ${errStr(err)}\nTrying others...`,
2943
)
3044
} else {
31-
console.error(`No addon.node found in ${addonPath}\nTrying others...`)
45+
devWarn(`No addon.node found in ${addonPath}\nTrying others...`)
3246
}
3347
}
3448
}
35-
36-
if (addon === undefined) {
37-
throw new Error(
38-
`No compatible zeromq.js addon found in ${addonParentDir} folder`,
39-
)
40-
}
41-
42-
return addon
4349
} catch (err) {
44-
throw new Error(`Failed to load zeromq.js addon.node: ${err}`)
50+
throw new Error(`Failed to load zeromq.js addon.node: ${errStr(err)}`)
4551
}
52+
53+
if (addon === undefined) {
54+
throw new Error("No compatible zeromq.js addon found")
55+
}
56+
57+
return addon
4658
}
4759

4860
module.exports = findAddon()

0 commit comments

Comments
 (0)