Skip to content

Commit 910a82d

Browse files
committed
fix: better error messages in case failure to load
1 parent fc1d0cf commit 910a82d

File tree

4 files changed

+61
-43
lines changed

4 files changed

+61
-43
lines changed

lib/load-addon.js

Lines changed: 23 additions & 14 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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function main() {
1919
try {
2020
require("../lib/load-addon.js")
2121
} catch (error) {
22-
console.error("Failed to load ZMQ addon:", error)
22+
console.error(error)
2323
cmakeTs()
2424
}
2525
}

src/load-addon.ts

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,44 @@
11
import path from "path"
22
import fs from "fs"
33

4-
const addonParentDir = path.join(
5-
__dirname,
6-
"..",
7-
"build",
8-
process.platform,
9-
process.arch,
10-
"node",
11-
)
12-
const addOnAbiDirs = fs.readdirSync(addonParentDir).sort((a, b) => {
13-
return Number.parseInt(b, 10) - Number.parseInt(a, 10)
14-
})
15-
16-
let addon: undefined | any
17-
// try each available addon ABI
18-
for (const addOnAbiDir of addOnAbiDirs) {
19-
const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node")
4+
function findAddon(): any | undefined {
205
try {
21-
addon = require(addonPath)
22-
} catch (err) {
23-
console.error(
24-
`Failed to load addon at ${addonPath}: ${err}\nTrying others...`,
6+
const addonParentDir = path.join(
7+
__dirname,
8+
"..",
9+
"build",
10+
process.platform,
11+
process.arch,
12+
"node",
2513
)
26-
}
27-
}
14+
const addOnAbiDirs = fs.readdirSync(addonParentDir).sort((a, b) => {
15+
return Number.parseInt(b, 10) - Number.parseInt(a, 10)
16+
})
2817

29-
if (addon === undefined) {
30-
throw new Error(
31-
`No compatible addon found in ${addonParentDir} folder. Please build addon with 'npm run build'`,
32-
)
18+
// try each available addon ABI
19+
let addon: undefined | any
20+
for (const addOnAbiDir of addOnAbiDirs) {
21+
const addonPath = path.join(addonParentDir, addOnAbiDir, "addon.node")
22+
try {
23+
addon = require(addonPath)
24+
break
25+
} catch (err) {
26+
console.error(
27+
`Failed to load addon at ${addonPath}: ${err}\nTrying others...`,
28+
)
29+
}
30+
}
31+
32+
if (addon === undefined) {
33+
throw new Error(
34+
`No compatible zeromq.js addon found in ${addonParentDir} folder`,
35+
)
36+
}
37+
38+
return addon
39+
} catch (err) {
40+
throw new Error(`Failed to load zeromq.js addon.node: ${err}`)
41+
}
3342
}
3443

35-
module.exports = addon
44+
module.exports = findAddon()

0 commit comments

Comments
 (0)