Skip to content

Commit eaf55a4

Browse files
committed
chore(cli): wrap third-party errors (get-port, latest-version, inquirer, stream-json) in CliError
Made-with: Cursor
1 parent 1708d17 commit eaf55a4

39 files changed

Lines changed: 105 additions & 66 deletions

packages/cli/cli/src/cli-context/CliContext.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,15 @@ export class CliContext {
443443
* @returns Promise<string> representing the user's input
444444
*/
445445
public async getInput(config: { message: string; default?: string }): Promise<string> {
446-
return await input({ message: config.message, default: config.default });
446+
try {
447+
return await input({ message: config.message, default: config.default });
448+
} catch (error) {
449+
if ((error as Error)?.name === "ExitPromptError") {
450+
this.logger.info("\nCancelled by user.");
451+
throw new TaskAbortSignal();
452+
}
453+
throw error;
454+
}
447455
}
448456
}
449457

packages/cli/cli/src/cli-context/StdoutRedirector.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CliError} from "@fern-api/task-context";
1+
import { CliError } from "@fern-api/task-context";
22

33
/**
44
* Redirects process.stdout to process.stderr.

packages/cli/cli/src/cli-context/upgrade-utils/getFernUpgradeMessage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { CliError} from "@fern-api/task-context";
1+
import { CliError } from "@fern-api/task-context";
22
import { FernRegistryClient } from "@fern-fern/generators-sdk";
33
import boxen from "boxen";
44
import chalk from "chalk";

packages/cli/cli/src/cli-context/upgrade-utils/getLatestVersionOfCli.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CliError } from "@fern-api/task-context";
12
import latestVersion from "latest-version";
23

34
import { CliEnvironment } from "../CliEnvironment.js";
@@ -14,7 +15,14 @@ export async function getLatestVersionOfCli({
1415
if (cliEnvironment.packageName !== "fern-api" || cliEnvironment.packageVersion === "0.0.0") {
1516
return cliEnvironment.packageVersion;
1617
}
17-
return latestVersion(cliEnvironment.packageName, {
18-
version: includePreReleases ? "prerelease" : "latest"
19-
});
18+
try {
19+
return await latestVersion(cliEnvironment.packageName, {
20+
version: includePreReleases ? "prerelease" : "latest"
21+
});
22+
} catch (error) {
23+
throw new CliError({
24+
message: `Failed to resolve latest CLI version: ${error instanceof Error ? error.message : String(error)}`,
25+
code: CliError.Code.NetworkError
26+
});
27+
}
2028
}

packages/cli/cli/src/cli.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
import { LOG_LEVELS, LogLevel } from "@fern-api/logger";
3535
import { askToLogin, login, logout } from "@fern-api/login";
3636
import { protocGenFern } from "@fern-api/protoc-gen-fern";
37+
import { CliError } from "@fern-api/task-context";
3738
import getPort from "get-port";
3839
import { Argv } from "yargs";
3940
import { hideBin } from "yargs/helpers";
@@ -85,7 +86,6 @@ import { FERN_CWD_ENV_VAR } from "./cwd.js";
8586
import { rerunFernCliAtVersion } from "./rerunFernCliAtVersion.js";
8687
import { resolveGroupGithubConfig } from "./resolveGroupGithubConfig.js";
8788
import { RUNTIME } from "./runtime.js";
88-
import { CliError } from "@fern-api/task-context";
8989

9090
void runCli();
9191

@@ -375,7 +375,9 @@ function addInitCommand(cli: Argv<GlobalCliOptions>, cliContext: CliContext) {
375375
const result = await loadOpenAPIFromUrl({ url: argv.openapi, logger: cliContext.logger });
376376

377377
if (result.status === LoadOpenAPIStatus.Failure) {
378-
cliContext.failAndThrow(result.errorMessage, undefined, { code: CliError.Code.NetworkError });
378+
cliContext.failAndThrow(result.errorMessage, undefined, {
379+
code: CliError.Code.NetworkError
380+
});
379381
}
380382

381383
const tmpFilepath = result.filePath;
@@ -3113,7 +3115,10 @@ function parseOwnerRepo(githubRepo: string): { owner: string; repo: string } {
31133115
const owner = parts[parts.length - 2];
31143116
const repo = parts[parts.length - 1];
31153117
if (owner == null || repo == null) {
3116-
throw new CliError({ message: `Could not parse owner/repo from: ${githubRepo}`, code: CliError.Code.ParseError });
3118+
throw new CliError({
3119+
message: `Could not parse owner/repo from: ${githubRepo}`,
3120+
code: CliError.Code.ParseError
3121+
});
31173122
}
31183123
return { owner, repo };
31193124
}

packages/cli/cli/src/cliV2.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import {
33
GENERATORS_CONFIGURATION_FILENAME,
44
INCORRECT_DOCKER_ORG
55
} from "@fern-api/configuration-loader";
6+
import { CliError } from "@fern-api/task-context";
67
import { FernRegistry } from "@fern-fern/generators-sdk";
78
import { writeFile } from "fs/promises";
89
import { Argv } from "yargs";
9-
1010
import { CliContext } from "./cli-context/CliContext.js";
1111
import { getGeneratorUpgradeMessage } from "./cli-context/upgrade-utils/getFernUpgradeMessage.js";
1212
import { getProjectGeneratorUpgrades } from "./cli-context/upgrade-utils/getGeneratorVersions.js";
@@ -15,7 +15,6 @@ import { GenerationModeFilter, getGeneratorList } from "./commands/generator-lis
1515
import { getGeneratorMetadata } from "./commands/generator-metadata/getGeneratorMetadata.js";
1616
import { getOrganization } from "./commands/organization/getOrganization.js";
1717
import { upgradeGenerator } from "./commands/upgrade/upgradeGenerator.js";
18-
import { CliError } from "@fern-api/task-context";
1918

2019
/**
2120
* Corrects the incorrect "fern-api/" Docker org prefix to "fernapi/" and logs a warning.

packages/cli/cli/src/commands/diff/diff.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { diffSemverOrThrow } from "@fern-api/core-utils";
22
import { AbsoluteFilePath, cwd, doesPathExist, resolve, streamObjectFromFile } from "@fern-api/fs-utils";
33
import { IntermediateRepresentation, serialization } from "@fern-api/ir-sdk";
44
import { IntermediateRepresentationChangeDetector } from "@fern-api/ir-utils";
5-
import { TaskAbortSignal } from "@fern-api/task-context";
5+
import { CliError, TaskAbortSignal } from "@fern-api/task-context";
66
import semver from "semver";
77

88
import { CliContext } from "../../cli-context/CliContext.js";
@@ -56,7 +56,9 @@ export async function diff({
5656

5757
const nextVersion = semver.inc(fromVersion, bump);
5858
if (!nextVersion) {
59-
context.failWithoutThrowing(`Invalid current version: ${fromVersion}`, undefined, { code: CliError.Code.VersionError });
59+
context.failWithoutThrowing(`Invalid current version: ${fromVersion}`, undefined, {
60+
code: CliError.Code.VersionError
61+
});
6062
throw new TaskAbortSignal();
6163
}
6264
return { bump, nextVersion, errors };
@@ -73,7 +75,9 @@ async function readIr({
7375
}): Promise<IntermediateRepresentation> {
7476
const absoluteFilepath = AbsoluteFilePath.of(resolve(cwd(), filepath));
7577
if (!(await doesPathExist(absoluteFilepath, "file"))) {
76-
context.failWithoutThrowing(`File not found: ${absoluteFilepath}`, undefined, { code: CliError.Code.ConfigError });
78+
context.failWithoutThrowing(`File not found: ${absoluteFilepath}`, undefined, {
79+
code: CliError.Code.ConfigError
80+
});
7781
throw new TaskAbortSignal();
7882
}
7983
let ir: unknown;

packages/cli/cli/src/commands/docs-diff/docsDiff.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { FernToken } from "@fern-api/auth";
22
import { AbsoluteFilePath, cwd, doesPathExist, join, RelativeFilePath } from "@fern-api/fs-utils";
33
import { askToLogin } from "@fern-api/login";
44
import { Project } from "@fern-api/project-loader";
5-
import { CliError} from "@fern-api/task-context";
5+
import { CliError } from "@fern-api/task-context";
66
import { mkdir, readFile, writeFile } from "fs/promises";
77
import { PNG } from "pngjs";
88
import { Browser, launch } from "puppeteer";

packages/cli/cli/src/commands/docs-md-generate/generateLibraryDocs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,9 @@ async function downloadIr(
419419
const ir = irWrapper.ir;
420420

421421
if (ir == null) {
422-
return context.failAndThrow(`IR is empty for library '${libraryName}'`, undefined, { code: CliError.Code.InternalError });
422+
return context.failAndThrow(`IR is empty for library '${libraryName}'`, undefined, {
423+
code: CliError.Code.InternalError
424+
});
423425
}
424426

425427
if (language === "CPP") {

packages/cli/cli/src/commands/docs-preview/deleteDocsPreview.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { FernToken } from "@fern-api/auth";
22
import { createFdrService } from "@fern-api/core";
33
import { askToLogin } from "@fern-api/login";
4+
import { CliError } from "@fern-api/task-context";
45
import chalk from "chalk";
5-
66
import { CliContext } from "../../cli-context/CliContext.js";
7-
import { CliError } from "@fern-api/task-context";
87

98
/**
109
* Preview URLs follow the pattern: {org}-preview-{hash}.docs.buildwithfern.com

0 commit comments

Comments
 (0)