Skip to content

Commit 9db8d9a

Browse files
committed
refactor(cli): remove LoggableFernCliError in favor of CliError
Replace all LoggableFernCliError throw sites with CliError.internalError() and remove the dedicated catch branches since CliError is already handled upstream. Deletes the now-unused LoggableFernCliError class. Made-with: Cursor
1 parent 7234b71 commit 9db8d9a

14 files changed

Lines changed: 98 additions & 136 deletions

File tree

packages/cli/cli-v2/src/context/adapter/TaskContextAdapter.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createLogger, LOG_LEVELS, Logger, LogLevel } from "@fern-api/logger";
22
import {
3-
type CliErrorCode,
3+
type CliError,
44
type CreateInteractiveTaskParams,
55
type Finishable,
66
type InteractiveTaskContext,
@@ -55,12 +55,12 @@ export class TaskContextAdapter implements TaskContext {
5555
await run();
5656
}
5757

58-
public failAndThrow(message?: string, error?: unknown, options?: { code?: CliErrorCode }): never {
58+
public failAndThrow(message?: string, error?: unknown, options?: { code?: CliError.Code }): never {
5959
this.failWithoutThrowing(message, error, options);
6060
throw new TaskAbortSignal();
6161
}
6262

63-
public failWithoutThrowing(message?: string, error?: unknown, options?: { code?: CliErrorCode }): void {
63+
public failWithoutThrowing(message?: string, error?: unknown, options?: { code?: CliError.Code }): void {
6464
this.result = TaskResult.Failure;
6565
if (error instanceof TaskAbortSignal) {
6666
return;
@@ -73,8 +73,8 @@ export class TaskContextAdapter implements TaskContext {
7373
reportError(this.context, error, { ...options, message });
7474
}
7575

76-
public captureException(error: unknown, code?: CliErrorCode): void {
77-
const errorCode = resolveErrorCode(error, code) ?? "INTERNAL_ERROR";
76+
public captureException(error: unknown, code?: CliError.Code): void {
77+
const errorCode = resolveErrorCode(error, code);
7878
this.context.telemetry.captureException(error, { errorCode });
7979
}
8080

packages/cli/cli-v2/src/context/withContext.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
import { LogLevel } from "@fern-api/logger";
2-
import {
3-
CliError,
4-
type CliErrorCode,
5-
resolveErrorCode,
6-
shouldReportToSentry,
7-
TaskAbortSignal
8-
} from "@fern-api/task-context";
2+
import { CliError, resolveErrorCode, shouldReportToSentry, TaskAbortSignal } from "@fern-api/task-context";
93

104
import chalk from "chalk";
115
import { KeyringUnavailableError } from "../auth/errors/KeyringUnavailableError.js";
@@ -117,7 +111,7 @@ function handleError(context: Context, error: unknown): void {
117111
export function reportError(
118112
context: Context,
119113
error: unknown,
120-
options?: { message?: string; code?: CliErrorCode }
114+
options?: { message?: string; code?: CliError.Code }
121115
): void {
122116
if (error instanceof TaskAbortSignal) {
123117
return;

packages/cli/cli-v2/src/sdk/generator/GeneratorPipeline.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ export class GeneratorPipeline {
123123
throw new CliError({
124124
message:
125125
`Custom image configurations are only supported with local generation (--local). ` +
126-
`Target "${args.target.name}" uses a custom image registry.`
126+
`Target "${args.target.name}" uses a custom image registry.`,
127+
code: CliError.Code.ConfigError
127128
});
128129
}
129130
return await this.runRemoteGeneration(args);

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { Project } from "@fern-api/project-loader";
55
import { isVersionAhead } from "@fern-api/semver-utils";
66
import {
77
CliError,
8-
type CliErrorCode,
98
Finishable,
109
PosthogEvent,
1110
resolveErrorCode,
@@ -120,12 +119,12 @@ export class CliContext {
120119
);
121120
}
122121

123-
public failAndThrow(message?: string, error?: unknown, options?: { code?: CliErrorCode }): never {
122+
public failAndThrow(message?: string, error?: unknown, options?: { code?: CliError.Code }): never {
124123
this.failWithoutThrowing(message, error, options);
125124
throw new TaskAbortSignal();
126125
}
127126

128-
public failWithoutThrowing(message?: string, error?: unknown, options?: { code?: CliErrorCode }): void {
127+
public failWithoutThrowing(message?: string, error?: unknown, options?: { code?: CliError.Code }): void {
129128
this.didSucceed = false;
130129
if (error instanceof TaskAbortSignal) {
131130
// We already tracked the true error, so we can just return.
@@ -286,7 +285,7 @@ export class CliContext {
286285
}
287286
}
288287

289-
public async captureException(error: unknown, code?: CliErrorCode): Promise<void> {
288+
public async captureException(error: unknown, code?: CliError.Code): Promise<void> {
290289
await this.sentryClient.captureException(error, code);
291290
}
292291

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { addPrefixToString } from "@fern-api/core-utils";
33
import { createLogger, LogLevel } from "@fern-api/logger";
44
import {
55
CliError,
6-
type CliErrorCode,
76
CreateInteractiveTaskParams,
87
Finishable,
98
InteractiveTaskContext,
@@ -29,7 +28,7 @@ export declare namespace TaskContextImpl {
2928
onResult?: (result: TaskResult) => void;
3029
shouldBufferLogs: boolean;
3130
instrumentPostHogEvent: (event: PosthogEvent) => void;
32-
captureException?: (error: unknown, code?: CliErrorCode) => void;
31+
captureException?: (error: unknown, code?: CliError.Code) => void;
3332
}
3433
}
3534

@@ -43,7 +42,7 @@ export class TaskContextImpl implements Startable<TaskContext>, Finishable, Task
4342
protected status: "notStarted" | "running" | "finished" = "notStarted";
4443
private onResult: ((result: TaskResult) => void) | undefined;
4544
private instrumentPostHogEventImpl: (event: PosthogEvent) => void;
46-
private captureExceptionImpl?: (error: unknown, code?: CliErrorCode) => void;
45+
private captureExceptionImpl?: (error: unknown, code?: CliError.Code) => void;
4746
public constructor({
4847
logImmediately,
4948
logPrefix,
@@ -83,13 +82,13 @@ export class TaskContextImpl implements Startable<TaskContext>, Finishable, Task
8382

8483
public takeOverTerminal: (run: () => void | Promise<void>) => Promise<void>;
8584

86-
public failAndThrow(message?: string, error?: unknown, options?: { code?: CliErrorCode }): never {
85+
public failAndThrow(message?: string, error?: unknown, options?: { code?: CliError.Code }): never {
8786
this.failWithoutThrowing(message, error, options);
8887
this.finish();
8988
throw new TaskAbortSignal();
9089
}
9190

92-
public failWithoutThrowing(message?: string, error?: unknown, options?: { code?: CliErrorCode }): void {
91+
public failWithoutThrowing(message?: string, error?: unknown, options?: { code?: CliError.Code }): void {
9392
this.result = TaskResult.Failure;
9493

9594
if (error instanceof TaskAbortSignal) {
@@ -116,7 +115,7 @@ export class TaskContextImpl implements Startable<TaskContext>, Finishable, Task
116115
}
117116
}
118117

119-
public captureException(error: unknown, code?: CliErrorCode): void {
118+
public captureException(error: unknown, code?: CliError.Code): void {
120119
this.captureExceptionImpl?.(error, code);
121120
}
122121

packages/cli/cli/src/cli.ts

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import {
3333
import { LOG_LEVELS, LogLevel } from "@fern-api/logger";
3434
import { askToLogin, login, logout } from "@fern-api/login";
3535
import { protocGenFern } from "@fern-api/protoc-gen-fern";
36-
import { CliError, LoggableFernCliError, TaskAbortSignal } from "@fern-api/task-context";
3736
import getPort from "get-port";
3837
import { Argv } from "yargs";
3938
import { hideBin } from "yargs/helpers";
@@ -88,8 +87,6 @@ import { RUNTIME } from "./runtime.js";
8887

8988
void runCli();
9089

91-
const USE_NODE_18_OR_ABOVE_MESSAGE = "The Fern CLI requires Node 18+ or above.";
92-
9390
async function runCli() {
9491
getOrCreateFernRunId();
9592

@@ -134,25 +131,7 @@ async function runCli() {
134131
});
135132
}
136133
} catch (error) {
137-
cliContext.instrumentPostHogEvent({
138-
command: process.argv.join(" "),
139-
properties: {
140-
failed: true,
141-
error
142-
}
143-
});
144-
if (error instanceof CliError) {
145-
cliContext.failWithoutThrowing(error.message, error);
146-
} else if ((error as Error)?.message?.includes("globalThis")) {
147-
cliContext.logger.error(USE_NODE_18_OR_ABOVE_MESSAGE);
148-
cliContext.failWithoutThrowing(undefined, error, { code: "ENVIRONMENT_ERROR" });
149-
} else if (error instanceof TaskAbortSignal) {
150-
cliContext.failWithoutThrowing();
151-
} else if (error instanceof LoggableFernCliError) {
152-
cliContext.logger.error(`Failed. ${error.log}`);
153-
} else {
154-
cliContext.failWithoutThrowing("Failed.", error, { code: "INTERNAL_ERROR" });
155-
}
134+
cliContext.failWithoutThrowing(undefined, error);
156135
}
157136

158137
await exit();

packages/cli/generation/ir-generator/src/extended-properties/addExtendedPropertiesToIr.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
ObjectTypeDeclaration,
77
TypeDeclaration
88
} from "@fern-api/ir-sdk";
9-
import { LoggableFernCliError } from "@fern-api/task-context";
9+
import { CliError } from "@fern-api/task-context";
1010

1111
import { getTypeDeclaration } from "../utils/getTypeDeclaration.js";
1212

@@ -96,9 +96,7 @@ function getObjectTypeDeclarationFromTypeId(typeId: string, ir: TypesAndServices
9696
}
9797
}
9898

99-
throw new LoggableFernCliError(
100-
`Unexpected error: ${typeId} is extended but has shape ${typeDeclaration.shape.type}`
101-
);
99+
throw CliError.internalError(`Unexpected error: ${typeId} is extended but has shape ${typeDeclaration.shape.type}`);
102100
}
103101

104102
function getAllPropertiesForObject({
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { TypeDeclaration } from "@fern-api/ir-sdk";
2-
import { LoggableFernCliError } from "@fern-api/task-context";
2+
import { CliError } from "@fern-api/task-context";
33

44
export function getTypeDeclaration(typeId: string, types: Record<string, TypeDeclaration>): TypeDeclaration {
55
const maybeTypeDeclaration = types[typeId];
66
if (maybeTypeDeclaration == null) {
7-
throw new LoggableFernCliError(`Illegal Error: Failed to load type declaration for type ${typeId}`);
7+
throw CliError.internalError(`Failed to load type declaration for type ${typeId}`);
88
}
99
return maybeTypeDeclaration;
1010
}

packages/cli/generation/local-generation/local-workspace-runner/src/GenerationRunner.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { generatorsYml, SNIPPET_JSON_FILENAME } from "@fern-api/configuration";
44
import { AbsoluteFilePath, join, RelativeFilePath } from "@fern-api/fs-utils";
55
import { generateIntermediateRepresentation } from "@fern-api/ir-generator";
66
import { IntermediateRepresentation } from "@fern-api/ir-sdk";
7-
import { LoggableFernCliError, TaskAbortSignal, TaskContext } from "@fern-api/task-context";
7+
import { TaskAbortSignal, TaskContext } from "@fern-api/task-context";
88
import { FernGeneratorExec } from "@fern-fern/generator-exec-sdk";
99
import chalk from "chalk";
1010
import { generateDynamicSnippetTests } from "./dynamic-snippets/generateDynamicSnippetTests.js";
@@ -101,8 +101,6 @@ export class GenerationRunner {
101101
} catch (error) {
102102
if (error instanceof TaskAbortSignal) {
103103
// already logged by failAndThrow, nothing to do
104-
} else if (error instanceof LoggableFernCliError) {
105-
interactiveTaskContext.failWithoutThrowing(`Generation failed: ${error.log}`, error);
106104
} else {
107105
interactiveTaskContext.failWithoutThrowing(
108106
`Generation failed: ${error instanceof Error ? error.message : "Unknown error"}`,

0 commit comments

Comments
 (0)