Skip to content

Commit 20fb1d2

Browse files
committed
Better error handling
1 parent d5311e5 commit 20fb1d2

File tree

2 files changed

+57
-32
lines changed

2 files changed

+57
-32
lines changed

packages/cli/src/command-helpers/local-node.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,23 @@ export async function downloadGraphNodeRelease(
5252
onProgress?: (downloaded: number, total: number | null) => void,
5353
): Promise<string> {
5454
const fileName = getPlatformBinaryName();
55-
return downloadGithubRelease(
56-
'incrypto32',
57-
'graph-node',
58-
release,
59-
outputDir,
60-
fileName,
61-
onProgress,
62-
);
55+
56+
try {
57+
return await downloadGithubRelease(
58+
'incrypto32',
59+
'graph-node',
60+
release,
61+
outputDir,
62+
fileName,
63+
onProgress,
64+
);
65+
} catch (e) {
66+
if (e === 404) {
67+
throw `Graph Node release ${release} does not exist, please check the release page for the correct release tag`;
68+
}
69+
70+
throw `Failed to download: ${release}`;
71+
}
6372
}
6473

6574
async function downloadGithubRelease(
@@ -89,7 +98,7 @@ export async function download(
8998
): Promise<string> {
9099
const res = await fetch(url);
91100
if (!res.ok || !res.body) {
92-
throw new Error(`Failed to download: ${res.statusText}`);
101+
throw res.status;
93102
}
94103

95104
const totalLength = Number(res.headers.get('content-length')) || null;

packages/cli/src/commands/node.ts

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import * as os from 'node:os';
44
import * as path from 'node:path';
55
import { print } from 'gluegun';
66
import ProgressBar from 'progress';
7-
import { Command, Flags } from '@oclif/core';
7+
import { Args, Command, Flags } from '@oclif/core';
88
import {
99
downloadGraphNodeRelease,
1010
extractGz,
@@ -16,43 +16,51 @@ import {
1616
export default class NodeCommand extends Command {
1717
static description = 'Manage Graph node related operations';
1818

19-
static flags = {
19+
static override flags = {
2020
help: Flags.help({
2121
char: 'h',
2222
}),
23+
tag: Flags.string({
24+
summary: 'Tag of the Graph Node release to install.',
25+
}),
26+
'download-dir': Flags.string({
27+
summary: 'Directory to download the Graph Node release to.',
28+
default: os.tmpdir(),
29+
}),
2330
};
2431

25-
static args = {};
32+
static override args = {
33+
install: Args.boolean({
34+
description: 'Install the Graph Node',
35+
}),
36+
};
2637

2738
static examples = ['$ graph node install'];
2839

2940
static strict = false;
3041

3142
async run() {
32-
const { argv } = await this.parse(NodeCommand);
33-
34-
if (argv.length > 0) {
35-
const subcommand = argv[0];
36-
37-
if (subcommand === 'install') {
38-
await installGraphNode();
39-
}
43+
const { flags, args } = await this.parse(NodeCommand);
4044

41-
// If no valid subcommand is provided, show help
42-
await this.config.runCommand('help', ['node']);
45+
if (args.install) {
46+
await installGraphNode(flags.tag);
47+
return;
4348
}
49+
50+
// If no valid subcommand is provided, show help
51+
await this.config.runCommand('help', ['node']);
4452
}
4553
}
4654

47-
async function installGraphNode() {
48-
const latestRelease = await getLatestGraphNodeRelease();
55+
async function installGraphNode(tag?: string) {
56+
const latestRelease = tag || (await getLatestGraphNodeRelease());
4957
const tmpBase = os.tmpdir();
5058
const tmpDir = await fs.promises.mkdtemp(path.join(tmpBase, 'graph-node-'));
5159
let progressBar: ProgressBar | undefined;
52-
const downloadPath = await downloadGraphNodeRelease(
53-
latestRelease,
54-
tmpDir,
55-
(downloaded, total) => {
60+
61+
let downloadPath: string;
62+
try {
63+
downloadPath = await downloadGraphNodeRelease(latestRelease, tmpDir, (downloaded, total) => {
5664
if (!total) return;
5765

5866
progressBar ||= new ProgressBar(`Downloading ${latestRelease} [:bar] :percent`, {
@@ -63,18 +71,21 @@ async function installGraphNode() {
6371
});
6472

6573
progressBar.tick(downloaded - (progressBar.curr || 0));
66-
},
67-
);
74+
});
75+
} catch (e) {
76+
print.error(e);
77+
throw e;
78+
}
6879

6980
let extractedPath: string;
7081

82+
print.info(`Extracting ${downloadPath}`);
7183
if (downloadPath.endsWith('.gz')) {
7284
extractedPath = await extractGz(downloadPath);
73-
print.info(`Extracted ${extractedPath}`);
7485
} else if (downloadPath.endsWith('.zip')) {
7586
extractedPath = await extractZipAndGetExe(downloadPath, tmpDir);
76-
print.info(`Extracted ${extractedPath}`);
7787
} else {
88+
print.error(`Unsupported file type: ${downloadPath}`);
7889
throw new Error(`Unsupported file type: ${downloadPath}`);
7990
}
8091

@@ -85,6 +96,11 @@ async function installGraphNode() {
8596
await chmod(movedPath, 0o755);
8697
}
8798

99+
print.info(`Installed Graph Node ${latestRelease}`);
100+
print.info(
101+
`Please add the following to your PATH: ${path.dirname(movedPath)} if it's not already there or if you're using a custom download directory`,
102+
);
103+
88104
// Delete the temporary directory
89105
await fs.promises.rm(tmpDir, { recursive: true, force: true });
90106
}

0 commit comments

Comments
 (0)