Skip to content

Commit 82915fb

Browse files
committed
feat(cli): correct error messages in cli v2
1 parent 40f0f06 commit 82915fb

18 files changed

Lines changed: 76 additions & 58 deletions

File tree

packages/cli/cli-v2/src/commands/api/check/command.ts

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

33
import chalk from "chalk";
44
import type { Argv } from "yargs";
@@ -45,7 +45,7 @@ export class CheckCommand {
4545
const response = this.buildJsonResponse({ apiCheckResult: result, hasErrors });
4646
context.stdout.info(JSON.stringify(response, null, 2));
4747
if (hasErrors) {
48-
throw new TaskAbortSignal();
48+
throw CliError.validationError();
4949
}
5050
return;
5151
}
@@ -58,7 +58,7 @@ export class CheckCommand {
5858
}
5959

6060
if (hasErrors) {
61-
throw new TaskAbortSignal();
61+
throw CliError.validationError();
6262
}
6363

6464
if (result.warningCount > 0) {

packages/cli/cli-v2/src/commands/auth/login/command.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { verifyAndDecodeJwt } from "@fern-api/auth";
22
import { LogLevel } from "@fern-api/logger";
33
import { type Auth0TokenResponse, getTokenFromAuth0 } from "@fern-api/login";
4-
import { TaskAbortSignal } from "@fern-api/task-context";
4+
import { CliError } from "@fern-api/task-context";
55
import chalk from "chalk";
66
import type { Argv } from "yargs";
77
import { TaskContextAdapter } from "../../../context/adapter/TaskContextAdapter.js";
@@ -41,13 +41,13 @@ export class LoginCommand {
4141
const payload = await verifyAndDecodeJwt(idToken);
4242
if (payload == null) {
4343
context.stdout.error(`${Icons.error} Internal error; could not verify ID token`);
44-
throw new TaskAbortSignal();
44+
throw CliError.internalError();
4545
}
4646

4747
const email = payload.email;
4848
if (email == null) {
4949
context.stdout.error(`${Icons.error} Internal error; ID token does not contain email claim`);
50-
throw new TaskAbortSignal();
50+
throw CliError.internalError();
5151
}
5252

5353
const { isNew, totalAccounts } = await context.tokenService.login(email, accessToken);

packages/cli/cli-v2/src/commands/auth/logout/command.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TaskAbortSignal } from "@fern-api/task-context";
1+
import { CliError } from "@fern-api/task-context";
22
import chalk from "chalk";
33
import inquirer from "inquirer";
44
import type { Argv } from "yargs";
@@ -64,7 +64,7 @@ export class LogoutCommand {
6464

6565
if (!context.isTTY) {
6666
context.stdout.error(`${Icons.error} Use --force to skip confirmation in non-interactive mode`);
67-
throw new TaskAbortSignal();
67+
throw new CliError({ code: CliError.Code.ValidationError });
6868
}
6969

7070
const { confirmed } = await inquirer.prompt<{ confirmed: boolean }>([
@@ -90,7 +90,9 @@ export class LogoutCommand {
9090

9191
if (!removed) {
9292
context.stdout.error(`${Icons.error} Account not found: ${user}`);
93-
throw new TaskAbortSignal();
93+
throw new CliError({
94+
code: CliError.Code.EnvironmentError
95+
});
9496
}
9597

9698
context.stdout.info(`${Icons.success} Logged out of ${chalk.bold(user)}`);
@@ -105,7 +107,9 @@ export class LogoutCommand {
105107
context.stdout.error(
106108
`${Icons.error} Multiple accounts found. Use --user or --all in non-interactive mode.`
107109
);
108-
throw new TaskAbortSignal();
110+
throw new CliError({
111+
code: CliError.Code.EnvironmentError
112+
});
109113
}
110114

111115
const choices = accounts.map((account) => ({

packages/cli/cli-v2/src/commands/auth/switch/command.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TaskAbortSignal } from "@fern-api/task-context";
1+
import { CliError } from "@fern-api/task-context";
22
import chalk from "chalk";
33
import inquirer from "inquirer";
44
import type { Argv } from "yargs";
@@ -20,14 +20,16 @@ export class SwitchCommand {
2020
context.stdout.warn(`${chalk.yellow("⚠")} You are not logged in to Fern.`);
2121
context.stdout.info("");
2222
context.stdout.info(chalk.dim(" To log in, run: fern auth login"));
23-
throw new TaskAbortSignal();
23+
throw new CliError({
24+
code: CliError.Code.ConfigError
25+
});
2426
}
2527

2628
if (accounts.length === 1) {
2729
const account = accounts[0];
2830
if (account == null) {
2931
context.stdout.error(`${Icons.error} Internal error; no accounts found`);
30-
throw new TaskAbortSignal();
32+
throw CliError.internalError();
3133
}
3234

3335
context.stdout.warn(
@@ -59,7 +61,9 @@ export class SwitchCommand {
5961
): Promise<void> {
6062
if (!context.isTTY) {
6163
context.stdout.error(`${Icons.error} Use --user to specify account in non-interactive mode`);
62-
throw new TaskAbortSignal();
64+
throw new CliError({
65+
code: CliError.Code.EnvironmentError
66+
});
6367
}
6468

6569
const choices = accounts.map((account) => ({
@@ -84,7 +88,9 @@ export class SwitchCommand {
8488
const success = await context.tokenService.switchAccount(user);
8589
if (!success) {
8690
context.stdout.error(`${Icons.error} Account not found: ${user}`);
87-
throw new TaskAbortSignal();
91+
throw new CliError({
92+
code: CliError.Code.EnvironmentError
93+
});
8894
}
8995
context.stdout.info(`${Icons.success} Switched to ${chalk.bold(user)}`);
9096
}

packages/cli/cli-v2/src/commands/auth/token/command.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createOrganizationIfDoesNotExist } from "@fern-api/auth";
22
import { createVenusService } from "@fern-api/core";
3-
import { TaskAbortSignal } from "@fern-api/task-context";
3+
import { CliError } from "@fern-api/task-context";
44
import type { Argv } from "yargs";
55
import { TaskContextAdapter } from "../../../context/adapter/TaskContextAdapter.js";
66
import type { Context } from "../../../context/Context.js";
@@ -41,24 +41,32 @@ export class TokenCommand {
4141
response.error._visit({
4242
organizationNotFoundError: () => {
4343
process.stderr.write(`${Icons.error} Organization "${orgId}" was not found.\n`);
44-
throw new TaskAbortSignal();
44+
throw new CliError({
45+
code: CliError.Code.ConfigError
46+
});
4547
},
4648
unauthorizedError: () => {
4749
process.stderr.write(`${Icons.error} You do not have access to organization "${orgId}".\n`);
48-
throw new TaskAbortSignal();
50+
throw new CliError({
51+
code: CliError.Code.AuthError
52+
});
4953
},
5054
missingOrgPermissionsError: () => {
5155
process.stderr.write(
5256
`${Icons.error} You do not have the required permissions in organization "${orgId}".\n`
5357
);
54-
throw new TaskAbortSignal();
58+
throw new CliError({
59+
code: CliError.Code.AuthError
60+
});
5561
},
5662
_other: () => {
5763
process.stderr.write(
5864
`${Icons.error} Failed to generate token.\n` +
5965
`\n Please contact support@buildwithfern.com for assistance.\n`
6066
);
61-
throw new TaskAbortSignal();
67+
throw new CliError({
68+
code: CliError.Code.InternalError
69+
});
6270
}
6371
});
6472
}
@@ -75,7 +83,7 @@ export class TokenCommand {
7583
`${Icons.error} No organization specified.\n` +
7684
`\n Run fern init or specify an organization with --org, then run this command again.\n`
7785
);
78-
throw new TaskAbortSignal();
86+
throw new CliError({ code: CliError.Code.ConfigError });
7987
}
8088
}
8189
}

packages/cli/cli-v2/src/commands/auth/whoami/command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { TaskAbortSignal } from "@fern-api/task-context";
1+
import { CliError } from "@fern-api/task-context";
22
import chalk from "chalk";
33
import type { Argv } from "yargs";
44
import type { Context } from "../../../context/Context.js";
@@ -18,7 +18,7 @@ export class WhoamiCommand {
1818
context.stdout.warn(`${chalk.yellow("⚠")} You are not logged in to Fern.`);
1919
context.stdout.info("");
2020
context.stdout.info(chalk.dim(" To log in, run: fern auth login"));
21-
throw new TaskAbortSignal();
21+
throw new CliError({ code: CliError.Code.AuthError });
2222
}
2323

2424
if (args.json) {

packages/cli/cli-v2/src/commands/check/command.ts

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

33
import chalk from "chalk";
44
import type { Argv } from "yargs";
@@ -46,14 +46,14 @@ export class CheckCommand {
4646
const response = this.buildJsonResponse({ apiCheckResult, sdkCheckResult, docsCheckResult, hasErrors });
4747
context.stdout.info(JSON.stringify(response, null, 2));
4848
if (hasErrors) {
49-
throw new TaskAbortSignal();
49+
throw new CliError({ code: CliError.Code.ValidationError });
5050
}
5151
return;
5252
}
5353

5454
// Fail if there are errors, or if strict mode and there are warnings.
5555
if (hasErrors) {
56-
throw new TaskAbortSignal();
56+
throw new CliError({ code: CliError.Code.ValidationError });
5757
}
5858

5959
if (totalWarnings > 0) {

packages/cli/cli-v2/src/commands/docs/check/command.ts

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

33
import chalk from "chalk";
44
import type { Argv } from "yargs";
@@ -40,7 +40,7 @@ export class CheckCommand {
4040
const response = this.buildJsonResponse({ result, hasErrors });
4141
context.stdout.info(JSON.stringify(response, null, 2));
4242
if (hasErrors) {
43-
throw new TaskAbortSignal();
43+
throw new CliError({ code: CliError.Code.ValidationError });
4444
}
4545
return;
4646
}
@@ -53,7 +53,7 @@ export class CheckCommand {
5353
}
5454

5555
if (hasErrors) {
56-
throw new TaskAbortSignal();
56+
throw new CliError({ code: CliError.Code.ValidationError });
5757
}
5858

5959
if (result.warningCount > 0) {

packages/cli/cli-v2/src/commands/docs/publish/command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { FernToken } from "@fern-api/auth";
22
import { extractErrorMessage } from "@fern-api/core-utils";
33
import { filterOssWorkspaces } from "@fern-api/docs-resolver";
4-
import { CliError, TaskAbortSignal } from "@fern-api/task-context";
4+
import { CliError } from "@fern-api/task-context";
55

66
import chalk from "chalk";
77
import inquirer from "inquirer";
@@ -161,7 +161,7 @@ export class PublishCommand {
161161
});
162162

163163
if (summary.failedCount > 0) {
164-
throw new TaskAbortSignal();
164+
throw new CliError({ code: CliError.Code.ContainerError });
165165
}
166166
}
167167

packages/cli/cli-v2/src/commands/org/create/command.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createOrganizationIfDoesNotExist, getOrganizationNameValidationError } from "@fern-api/auth";
2-
import { CliError, TaskAbortSignal } from "@fern-api/task-context";
2+
import { CliError } from "@fern-api/task-context";
33

44
import type { Argv } from "yargs";
55
import { TaskContextAdapter } from "../../../context/adapter/TaskContextAdapter.js";
@@ -23,7 +23,7 @@ export class CreateCommand {
2323
context.stderr.error(
2424
`${Icons.error} Organization tokens cannot create organizations. Unset the FERN_TOKEN environment variable and run 'fern auth login' to create an organization.`
2525
);
26-
throw new TaskAbortSignal();
26+
throw new CliError({ code: CliError.Code.AuthError });
2727
}
2828

2929
const validationError = getOrganizationNameValidationError(args.name);

0 commit comments

Comments
 (0)