Skip to content

Commit bab9819

Browse files
author
ArtemZ
committed
Better error handling
1 parent 66538a7 commit bab9819

File tree

4 files changed

+75
-45
lines changed

4 files changed

+75
-45
lines changed

cli.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env node
22

33
const { consoleTerminal, everdevInit, everdevDone, cli } = require("./dist")
4+
const { rewriteKnownErrors } = require("./dist/rewriteKnownErrors")
45

56
;(async () => {
67
try {
@@ -14,6 +15,7 @@ const { consoleTerminal, everdevInit, everdevDone, cli } = require("./dist")
1415
err.code = code
1516
err.data = data
1617
}
18+
err = rewriteKnownErrors(err)
1719
console.error(`${err}`)
1820
process.exit(1)
1921
}

src/core/component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
executableName,
66
formatTable,
77
loadBinaryVersions,
8-
nullTerminal,
98
run,
109
StringTerminal,
1110
writeJsonFile,
@@ -136,7 +135,8 @@ export class Component {
136135
if (fs.existsSync(this.path())) {
137136
const isDeprecatedVersion =
138137
!!_downloadedVersion.match(/^0.21.0$|^0.1.21$/)
139-
const compilerOut = await this.run(nullTerminal, process.cwd(), [
138+
const stringTerminal = new StringTerminal()
139+
const compilerOut = await this.run(stringTerminal, process.cwd(), [
140140
"--version",
141141
])
142142
return isDeprecatedVersion

src/core/utils.ts

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -197,53 +197,61 @@ export async function downloadFromBinaries(
197197
version?: string
198198
},
199199
) {
200-
src = src.replace("{p}", os.platform())
201-
const srcExt = path.extname(src).toLowerCase()
202-
const srcUrl = `https://binaries.tonlabs.io/${src}`
203-
terminal.write(`Downloading from ${srcUrl}`)
204-
const dstDir = path.dirname(dstPath)
205-
if (!fs.existsSync(dstDir)) {
206-
fs.mkdirSync(dstDir, { recursive: true })
207-
}
208-
if (srcExt === ".zip") {
209-
await downloadAndUnzip(dstDir, srcUrl, terminal)
210-
} else if (srcExt === ".gz") {
211-
await downloadAndGunzip(dstPath, srcUrl, terminal)
212-
if (path.extname(dstPath) === ".tar") {
213-
await run(
214-
"tar",
215-
["xvf", dstPath],
216-
{ cwd: path.dirname(dstPath) },
217-
terminal,
218-
)
219-
fs.unlink(dstPath, () => {})
200+
try {
201+
console.log("TEST BRANCH")
202+
src = src.replace("{p}", os.platform())
203+
const srcExt = path.extname(src).toLowerCase()
204+
const srcUrl = `https://binaries.tonlabs.io/${src}`
205+
terminal.write(`Downloading from ${srcUrl}`)
206+
const dstDir = path.dirname(dstPath)
207+
if (!fs.existsSync(dstDir)) {
208+
fs.mkdirSync(dstDir, { recursive: true })
220209
}
221-
} else {
222-
throw Error(`Unexpected binary file extension: ${srcExt}`)
223-
}
224-
if (options?.executable && os.platform() !== "win32") {
225-
if (options?.adjustedPath) {
226-
const dir = path.dirname(options.adjustedPath)
227-
fs.readdirSync(dir)
228-
.map(filename => path.resolve(dir, filename))
229-
.filter(filename => !fs.lstatSync(filename).isDirectory())
230-
.forEach(filename => fs.chmodSync(filename, 0o755))
210+
if (srcExt === ".zip") {
211+
await downloadAndUnzip(dstDir, srcUrl, terminal)
212+
} else if (srcExt === ".gz") {
213+
await downloadAndGunzip(dstPath, srcUrl, terminal)
214+
if (path.extname(dstPath) === ".tar") {
215+
await run(
216+
"tar",
217+
["xvf", dstPath],
218+
{ cwd: path.dirname(dstPath) },
219+
terminal,
220+
)
221+
fs.unlink(dstPath, () => {})
222+
}
231223
} else {
232-
fs.chmodSync(dstPath, 0o755)
224+
throw Error(`Unexpected binary file extension: ${srcExt}`)
233225
}
234-
// Without pause on Fedora 32 Linux always leads to an error: spawn ETXTBSY
235-
await new Promise(resolve => setTimeout(resolve, 100))
236-
}
237-
if (options?.globally) {
238-
if (!options.version) {
239-
throw Error("Version required to install package")
226+
if (options?.executable && os.platform() !== "win32") {
227+
if (options?.adjustedPath) {
228+
const dir = path.dirname(options.adjustedPath)
229+
fs.readdirSync(dir)
230+
.map(filename => path.resolve(dir, filename))
231+
.filter(filename => !fs.lstatSync(filename).isDirectory())
232+
.forEach(filename => fs.chmodSync(filename, 0o755))
233+
} else {
234+
fs.chmodSync(dstPath, 0o755)
235+
}
236+
// Without pause on Fedora 32 Linux always leads to an error: spawn ETXTBSY
237+
await new Promise(resolve => setTimeout(resolve, 100))
240238
}
241-
await installGlobally(dstPath, options.version, terminal).catch(err => {
242-
fs.unlink(dstPath, () => {})
243-
throw err
244-
})
239+
if (options?.globally) {
240+
if (!options.version) {
241+
throw Error("Version required to install package")
242+
}
243+
await installGlobally(dstPath, options.version, terminal).catch(
244+
err => {
245+
fs.unlink(dstPath, () => {})
246+
throw err
247+
},
248+
)
249+
}
250+
terminal.write("\n")
251+
} catch (err) {
252+
console.log(err)
253+
throw err
245254
}
246-
terminal.write("\n")
247255
}
248256

249257
export function run(
@@ -289,7 +297,13 @@ export function run(
289297
if (code === 0) {
290298
resolve(output.join(""))
291299
} else {
292-
reject(`${name} failed`)
300+
reject(
301+
Error(
302+
terminal instanceof StringTerminal
303+
? terminal.stderr
304+
: `${name} failed`,
305+
),
306+
)
293307
}
294308
})
295309
} catch (error) {

src/rewriteKnownErrors.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export function rewriteKnownErrors(err: unknown) {
2+
let presumableError = ""
3+
4+
if (err instanceof Error) {
5+
if (/dyld: Symbol not found/.test(err.message)) {
6+
presumableError = "Wrong Mac OS version"
7+
}
8+
9+
if (presumableError !== "") {
10+
err.message = `${presumableError} (presumably)\nOriginal error:${err.message}`
11+
}
12+
}
13+
return err
14+
}

0 commit comments

Comments
 (0)