Emit fatal errors as JSON - #8405
Conversation
|
/snapit |
|
🫰✨ Thanks @gonzaloriestra! Your snapshot has been published to npm. Test the snapshot by installing your package globally: pnpm i -g --@shopify:registry=https://registry.npmjs.org @shopify/cli@0.0.0-snapshot-20260827101450Caution After installing, validate the version by running |
2b6397c to
2e72af5
Compare
a987a5b to
2867bf1
Compare
1173d2e to
c40ff3d
Compare
c40ff3d to
ea20e9d
Compare
da3e80a to
c0693c5
Compare
c0693c5 to
2a0f445
Compare
| const {jsonOutputEnabled} = await import('../environment.js') | ||
| if (jsonOutputEnabled()) { | ||
| try { | ||
| const {renderFatalErrorAsJson} = await import('../../../private/node/json-error.js') | ||
| renderFatalErrorAsJson(fatal) | ||
| return Promise.resolve(error) | ||
| // eslint-disable-next-line no-catch-all/no-catch-all | ||
| } catch (serializationError) { | ||
| outputDebug(`Failed to render the error as JSON: ${serializationError}`) | ||
| } | ||
| } | ||
|
|
||
| const {renderFatalError} = await import('../ui.js') |
There was a problem hiding this comment.
The content of this file was moved from ../error.ts. These are the only changes here.
2a0f445 to
66fafe4
Compare
|
Warning This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
This stack of pull requests is managed by Graphite. Learn more about stacking. |
97527e8 to
b308435
Compare
b308435 to
81cf94f
Compare
3d20e1b to
a946666
Compare
a946666 to
28d0cab
Compare
|
This closes the empty-stdout problem, which is what actually blocks scripting against For throw new AbortError('GraphQL operation failed.', JSON.stringify({errors: error.response.errors}, null, 2))
What I would push back on is that it works by accident. Is there room in this design for structured error detail that is not a display string? Two shapes would do it.
I have a working branch for the second one, scoped to Separate and smaller, probably its own issue rather than folded in here. |
dmerand
left a comment
There was a problem hiding this comment.
Mix of human/LLM feedback on this one. Again, off to a great start.
There was a problem hiding this comment.
Building on this comment: The renderer already limits output to known fields, which we should keep. But there is no field for extra structured error data.
Could a command pass selected error data to the renderer in an optional details field? The command would choose what to include. The renderer would continue to ignore other properties on the original error.
This would let callers read the data without parsing display strings such as tryMessage. I think it could fit into the existing error-output path without another error-handling layer.
There was a problem hiding this comment.
Totally agree, I added the optional details field and it's used by store execute now.
| if (jsonOutputEnabled()) { | ||
| try { | ||
| const {renderFatalErrorAsJson} = await import('../../../private/node/json-error.js') | ||
| renderFatalErrorAsJson(fatal) |
There was a problem hiding this comment.
Could we flush stdout on this path before Oclif exits, and cover the normal exit path with a subprocess test? The uncaught-error path flushes stdout, but this path returns to Errors.handle(), which calls process.exit().
In an isolated subprocess check using this PR's error handler, serializer, stdout writer, and Oclif 4.8.3, a 1 MiB error stopped at 65,536 bytes in three runs. JSON.parse failed each time. Adding the PR's flushStdout() before Errors.handle() delivered the full document and kept exit code 1. Unrelated environment/UI dependencies were stubbed; this was not a full CLI invocation.
The existing fixtures miss this case: one sets process.exitCode, which permits normal draining; the large-output test explicitly calls flushStdout() rather than using the normal error path.
There was a problem hiding this comment.
Fixed by flushing in the shared handler. Also added a test to check that a full 1 MiB document survives Errors.handle() with exit code 1.
| renderFatalErrorAsJson(fatal) | ||
| return Promise.resolve(error) | ||
| // eslint-disable-next-line no-catch-all/no-catch-all | ||
| } catch (serializationError) { |
There was a problem hiding this comment.
If JSON serialization fails, could we emit a minimal JSON error instead of falling back to a text banner? When stdout is still writable, callers using --json should still receive a parseable error document. A broken stdout pipe is a separate case.
Could we add a test for this serialization-failure fallback?
There was a problem hiding this comment.
Good idea, I added the fallback.
| body: string | string[][] | ||
| } | ||
|
|
||
| interface JsonErrorBase { |
There was a problem hiding this comment.
Can we infer these error types from the Zod schemas instead of maintaining separate interfaces?
The satisfies ZodType<...> checks do not require the two declarations to match exactly. Optional fields can still differ. Inference would keep the TypeScript type, runtime validation, and help output tied to one declaration, as intended in #8404.
There was a problem hiding this comment.
Done, the schemas are now the single source of truth
28d0cab to
62170a0
Compare
62170a0 to
1984a12
Compare
1984a12 to
796cd1d
Compare
|
@tizmagik nice point! I added the optional details field, and it's used by Regarding the second issue, you are right. The new details field preserves per-error extensions, but response-level extensions.cost.throttleStatus is still dropped. I agree this deserves a separate fix covering both success and failure. I can have a look later. |
796cd1d to
e60fbf7
Compare
Differences in type declarationsWe detected differences in the type declarations generated by Typescript for this branch compared to the baseline ('main' branch). Please, review them to ensure they are backward-compatible. Here are some important things to keep in mind:
New type declarationspackages/cli-kit/dist/private/node/command-event-context.d.tsimport { type CommandEvent, type CommandEventChannelOptions, type CommandEventEmissionOptions, type CommandEventInput } from '../../public/common/command-events.js';
export type CommandEventOutputMode = 'text' | 'json';
interface RunWithCommandEventsOptions extends CommandEventChannelOptions<CommandEvent> {
outputMode?: CommandEventOutputMode;
}
/**
* Runs a command execution with an event channel available to all nested asynchronous work.
*
* @param options - The event sink, clock, and output mode used by the channel.
* @param execute - The command execution to run with the channel.
* @returns The result of the command execution.
*/
export declare function runWithCommandEvents<TResult>(options: RunWithCommandEventsOptions, execute: () => TResult): TResult;
/**
* Emits an event for the current command execution.
*
* Events emitted outside a command execution are ignored.
*
* @param event - The event to emit before its timestamp is added.
* @param options - Presentation details that are not included in the event.
*/
export declare function emitCommandEvent(event: CommandEventInput, options?: CommandEventEmissionOptions): void;
/**
* Returns how command events are presented for the current execution.
*
* @returns The current event output mode, or undefined outside a command event context.
*/
export declare function commandEventOutputMode(): CommandEventOutputMode | undefined;
export {};
packages/cli-kit/dist/private/node/command-event-output.d.tsimport { type CommandEvent } from '../../public/common/command-events.js';
/**
* Writes a command event as JSON without routing it back through the command event context.
*
* @param event - The event to write.
*/
export declare function outputCommandEventAsJson(event: CommandEvent): void;
packages/cli-kit/dist/private/node/json-error.d.tsinterface FatalErrorLike {
type?: number;
message?: unknown;
formattedMessage?: unknown;
tryMessage?: unknown;
nextSteps?: unknown;
customSections?: unknown;
stack?: unknown;
command?: unknown;
args?: unknown;
details?: unknown;
}
/**
* Writes the public JSON representation of a fatal error to stdout.
*
* The allow-list mirrors the meaningful content of the regular fatal-error renderer.
* Arbitrary error properties remain private and are never copied to stdout.
*
* @param error - Fatal error to serialize.
*/
export declare function renderFatalErrorAsJson(error: FatalErrorLike): void;
export {};
packages/cli-kit/dist/public/common/command-events.d.tsimport { z } from 'zod';
/** Schema for a diagnostic emitted while a command executes. */
export declare const commandDiagnosticEventSchema: z.ZodObject<{
type: z.ZodLiteral<"diagnostic">;
timestamp: z.ZodString;
level: z.ZodEnum<["debug", "info", "warning", "error"]>;
message: z.ZodString;
code: z.ZodOptional<z.ZodString>;
}, "strict", z.ZodTypeAny, {
type: "diagnostic";
message: string;
timestamp: string;
level: "info" | "error" | "debug" | "warning";
code?: string | undefined;
}, {
type: "diagnostic";
message: string;
timestamp: string;
level: "info" | "error" | "debug" | "warning";
code?: string | undefined;
}>;
/** Schema for a progress update emitted while a command executes. */
export declare const commandProgressEventSchema: z.ZodObject<{
type: z.ZodLiteral<"progress">;
timestamp: z.ZodString;
status: z.ZodEnum<["started", "updated", "completed"]>;
operation: z.ZodString;
message: z.ZodOptional<z.ZodString>;
current: z.ZodOptional<z.ZodNumber>;
total: z.ZodOptional<z.ZodNumber>;
}, "strict", z.ZodTypeAny, {
type: "progress";
status: "started" | "updated" | "completed";
timestamp: string;
operation: string;
message?: string | undefined;
current?: number | undefined;
total?: number | undefined;
}, {
type: "progress";
status: "started" | "updated" | "completed";
timestamp: string;
operation: string;
message?: string | undefined;
current?: number | undefined;
total?: number | undefined;
}>;
/** Schema for side events emitted while a command executes. */
export declare const commandEventSchema: z.ZodDiscriminatedUnion<"type", [z.ZodObject<{
type: z.ZodLiteral<"diagnostic">;
timestamp: z.ZodString;
level: z.ZodEnum<["debug", "info", "warning", "error"]>;
message: z.ZodString;
code: z.ZodOptional<z.ZodString>;
}, "strict", z.ZodTypeAny, {
type: "diagnostic";
message: string;
timestamp: string;
level: "info" | "error" | "debug" | "warning";
code?: string | undefined;
}, {
type: "diagnostic";
message: string;
timestamp: string;
level: "info" | "error" | "debug" | "warning";
code?: string | undefined;
}>, z.ZodObject<{
type: z.ZodLiteral<"progress">;
timestamp: z.ZodString;
status: z.ZodEnum<["started", "updated", "completed"]>;
operation: z.ZodString;
message: z.ZodOptional<z.ZodString>;
current: z.ZodOptional<z.ZodNumber>;
total: z.ZodOptional<z.ZodNumber>;
}, "strict", z.ZodTypeAny, {
type: "progress";
status: "started" | "updated" | "completed";
timestamp: string;
operation: string;
message?: string | undefined;
current?: number | undefined;
total?: number | undefined;
}, {
type: "progress";
status: "started" | "updated" | "completed";
timestamp: string;
operation: string;
message?: string | undefined;
current?: number | undefined;
total?: number | undefined;
}>]>;
/** A diagnostic emitted while a command executes. */
export type CommandDiagnosticEvent = z.infer<typeof commandDiagnosticEventSchema>;
/** A progress update emitted while a command executes. */
export type CommandProgressEvent = z.infer<typeof commandProgressEventSchema>;
/** A side event emitted while a command executes. */
export type CommandEvent = z.infer<typeof commandEventSchema>;
/** An event before its emission timestamp is added. */
export type CommandEventInput<TEvent extends CommandEvent = CommandEvent> = TEvent extends unknown ? Omit<TEvent, 'timestamp'> : never;
/** Presentation details that are not included in the emitted event. */
export interface CommandEventEmissionOptions {
/** The event is already visible in the command's text UI. */
alreadyRendered?: boolean;
}
/** Receives one timestamped event from a command execution. */
export type CommandEventSink<TEvent extends CommandEvent = CommandEvent> = (event: TEvent, options?: CommandEventEmissionOptions) => void;
/** Emits timestamped side events from one command execution. */
export interface CommandEventChannel<TEvent extends CommandEvent = CommandEvent> {
emit: (event: CommandEventInput<TEvent>, options?: CommandEventEmissionOptions) => void;
}
/** Supplies the current time when an event is emitted. */
export type CommandEventClock = () => Date;
/** Options for a command event channel. */
export interface CommandEventChannelOptions<TEvent extends CommandEvent> {
sink?: CommandEventSink<TEvent>;
clock?: CommandEventClock;
}
/**
* Creates a synchronous, execution-scoped channel for command side events.
* Adapters validate events at their output boundary; the channel preserves domain-specific event fields.
*
* @param options - The event sink and clock used by the channel.
* @returns A channel that adds an ISO timestamp before synchronously delivering each event.
*/
export declare function createCommandEventChannel<TEvent extends CommandEvent = CommandEvent>(options?: CommandEventChannelOptions<TEvent>): CommandEventChannel<TEvent>;
packages/cli-kit/dist/public/node/command-events.d.tsimport { type CommandEvent, type CommandEventChannelOptions, type CommandEventEmissionOptions, type CommandEventInput } from '../common/command-events.js';
import { type CommandEventOutputMode } from '../../private/node/command-event-context.js';
export type { CommandEventOutputMode } from '../../private/node/command-event-context.js';
interface RunWithCommandEventsOptions extends CommandEventChannelOptions<CommandEvent> {
outputMode?: CommandEventOutputMode;
}
/**
* Runs a command execution with an event channel available to all nested asynchronous work.
*
* @param options - The event sink, clock, and output mode used by the channel.
* @param execute - The command execution to run with the channel.
* @returns The result of the command execution.
*/
export declare function runWithCommandEvents<TResult>(options: RunWithCommandEventsOptions, execute: () => TResult): TResult;
/**
* Runs the complete CLI lifecycle with the event presentation selected by its arguments.
*
* @param argv - The command arguments used to determine whether JSON output is enabled.
* @param execute - The command lifecycle to run.
* @returns The result of the command lifecycle.
*/
export declare function runWithCommandEventsForCommand<TResult>(argv: string[], execute: () => TResult): TResult;
/**
* Emits an event for the current command execution.
*
* Events emitted outside a command execution are ignored.
*
* @param event - The event to emit before its timestamp is added.
* @param options - Presentation details that are not included in the event.
*/
export declare function emitCommandEvent(event: CommandEventInput, options?: CommandEventEmissionOptions): void;
/**
* Returns how command events are presented for the current execution.
*
* @returns The current event output mode, or undefined outside a command event context.
*/
export declare function commandEventOutputMode(): CommandEventOutputMode | undefined;
/**
* Renders a command side event to stderr using the existing CLI output behavior.
*
* @param event - The event to render.
*/
export declare function renderCommandEvent(event: CommandEvent): void;
/**
* Renders a command side event as compact JSON to stderr.
*
* @param event - The event to render.
*/
export declare function renderCommandEventAsJson(event: CommandEvent): void;
packages/cli-kit/dist/public/node/json-output-schema.d.tsimport { ZodTypeAny, type z } from 'zod';
interface JsonOutputSchemaDefinition<TSchema extends ZodTypeAny = ZodTypeAny> {
readonly name: string;
readonly schema: TSchema;
readonly definitions: Readonly<Record<string, ZodTypeAny>>;
}
export interface JsonOutputSchema<TSchema extends ZodTypeAny = ZodTypeAny> extends JsonOutputSchemaDefinition<TSchema> {
readonly typescript: string;
validate(value: unknown): z.output<TSchema>;
encode(value: z.input<TSchema>): string;
}
export type InferJsonOutputSchema<TOutputSchema extends JsonOutputSchema> = z.output<TOutputSchema['schema']>;
interface DefineJsonOutputSchemaOptions<TSchema extends ZodTypeAny> {
name: string;
schema: TSchema;
definitions?: Readonly<Record<string, ZodTypeAny>>;
}
/**
* Defines the runtime validator, encoder, and documented TypeScript type for a command's JSON output.
*
* @param options - The root type name, its Zod schema, and any named nested schemas.
* @returns The complete JSON output contract.
*/
export declare function defineJsonOutputSchema<TSchema extends ZodTypeAny>(options: DefineJsonOutputSchemaOptions<TSchema>): JsonOutputSchema<TSchema>;
/**
* Renders the named schemas in a JSON output contract as TypeScript declarations.
*
* @param outputSchema - The root schema and its named nested schemas.
* @returns TypeScript declarations suitable for command help.
*/
export declare function renderJsonOutputSchema(outputSchema: JsonOutputSchemaDefinition): string;
export {};
packages/cli-kit/dist/public/node/error/index.d.tsimport { OutputMessage } from '../output.js';
import { type InlineToken, type TokenItem } from '../../../private/node/ui/components/token-item.js';
import type { AlertCustomSection } from '../ui.js';
export declare enum FatalErrorType {
Abort = 0,
AbortSilent = 1,
Bug = 2
}
export declare class CancelExecution extends Error {
}
/**
* A fatal error represents an error shouldn't be rescued and that causes the execution to terminate.
* There shouldn't be code that catches fatal errors.
*/
export declare abstract class FatalError extends Error {
tryMessage: TokenItem | null;
type: FatalErrorType;
nextSteps?: TokenItem<InlineToken>[];
formattedMessage?: TokenItem;
customSections?: AlertCustomSection[];
/** Selected JSON-serializable data to include in JSON errors. Never attach the raw error or request. */
details?: unknown;
skipOclifErrorHandling: boolean;
/**
* Creates a new FatalError error.
*
* @param message - The error message.
* @param type - The type of fatal error.
* @param tryMessage - The message that recommends next steps to the user.
* You can pass a string a {@link TokenizedString} or a {@link TokenItem}
* if you need to style the message inside the error Banner component.
* @param nextSteps - Message to show as "next steps" with suggestions to solve the issue.
* @param customSections - Custom sections to show in the error banner. To be used if nextSteps is not enough.
*/
constructor(message: TokenItem | OutputMessage, type: FatalErrorType, tryMessage?: TokenItem | OutputMessage | null, nextSteps?: TokenItem<InlineToken>[], customSections?: AlertCustomSection[]);
}
/**
* An abort error is a fatal error that shouldn't be reported as a bug.
* Those usually represent unexpected scenarios that we can't handle and that usually require some action from the developer.
*/
export declare class AbortError extends FatalError {
constructor(message: TokenItem | OutputMessage, tryMessage?: TokenItem | OutputMessage | null, nextSteps?: TokenItem<InlineToken>[], customSections?: AlertCustomSection[]);
}
/**
* An external error is similar to Abort but has extra command and args attributes.
* This is useful to represent errors coming from external commands, usually executed by execa.
*/
export declare class ExternalError extends FatalError {
command: string;
args: string[];
constructor(message: OutputMessage, command: string, args: string[], tryMessage?: TokenItem | OutputMessage | null);
}
export declare class AbortSilentError extends FatalError {
constructor();
}
/**
* A bug error is an error that represents a bug and therefore should be reported.
*/
export declare class BugError extends FatalError {
constructor(message: TokenItem | OutputMessage, tryMessage?: TokenItem | OutputMessage | null);
}
/**
* A function that handles errors that blow up in the CLI.
*
* @param error - Error to be handled.
* @returns A promise that resolves with the error passed.
*/
export declare function handler(error: unknown): Promise<unknown>;
/**
* A function that maps an error to an Abort with the stack trace when coming from the CLI.
*
* @param error - Error to be mapped.
* @returns A promise that resolves with the new error object.
*/
export declare function errorMapper(error: unknown): Promise<unknown>;
/**
* A function that checks if an error should be reported as unexpected.
*
* @param error - Error to be checked.
* @returns A boolean indicating if the error should be reported as unexpected.
*/
export declare function shouldReportErrorAsUnexpected(error: unknown): boolean;
/**
* Stack traces usually have file:// - we strip that and also remove the Windows drive designation.
*
* @param filePath - Path to be cleaned.
* @returns The cleaned path.
*/
export declare function cleanSingleStackTracePath(filePath: string): string;
packages/cli-kit/dist/public/node/error/schema.d.tsimport { zod } from '../schema.js';
export declare const JsonErrorCustomSectionSchema: zod.ZodObject<{
title: zod.ZodOptional<zod.ZodString>;
body: zod.ZodUnion<[zod.ZodString, zod.ZodArray<zod.ZodArray<zod.ZodString, "many">, "many">]>;
}, "strict", zod.ZodTypeAny, {
body: string | string[][];
title?: string | undefined;
}, {
body: string | string[][];
title?: string | undefined;
}>;
export declare const JsonAbortErrorSchema: zod.ZodObject<{
message: zod.ZodString;
tryMessage: zod.ZodOptional<zod.ZodString>;
nextSteps: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
customSections: zod.ZodOptional<zod.ZodArray<zod.ZodObject<{
title: zod.ZodOptional<zod.ZodString>;
body: zod.ZodUnion<[zod.ZodString, zod.ZodArray<zod.ZodArray<zod.ZodString, "many">, "many">]>;
}, "strict", zod.ZodTypeAny, {
body: string | string[][];
title?: string | undefined;
}, {
body: string | string[][];
title?: string | undefined;
}>, "many">>;
details: zod.ZodOptional<zod.ZodUnknown>;
type: zod.ZodLiteral<"abort">;
}, "strict", zod.ZodTypeAny, {
type: "abort";
message: string;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}, {
type: "abort";
message: string;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}>;
export declare const JsonBugErrorSchema: zod.ZodObject<{
stack: zod.ZodOptional<zod.ZodString>;
message: zod.ZodString;
tryMessage: zod.ZodOptional<zod.ZodString>;
nextSteps: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
customSections: zod.ZodOptional<zod.ZodArray<zod.ZodObject<{
title: zod.ZodOptional<zod.ZodString>;
body: zod.ZodUnion<[zod.ZodString, zod.ZodArray<zod.ZodArray<zod.ZodString, "many">, "many">]>;
}, "strict", zod.ZodTypeAny, {
body: string | string[][];
title?: string | undefined;
}, {
body: string | string[][];
title?: string | undefined;
}>, "many">>;
details: zod.ZodOptional<zod.ZodUnknown>;
type: zod.ZodLiteral<"bug">;
}, "strict", zod.ZodTypeAny, {
type: "bug";
message: string;
stack?: string | undefined;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}, {
type: "bug";
message: string;
stack?: string | undefined;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}>;
export declare const JsonExternalErrorSchema: zod.ZodObject<{
command: zod.ZodString;
args: zod.ZodArray<zod.ZodString, "many">;
message: zod.ZodString;
tryMessage: zod.ZodOptional<zod.ZodString>;
nextSteps: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
customSections: zod.ZodOptional<zod.ZodArray<zod.ZodObject<{
title: zod.ZodOptional<zod.ZodString>;
body: zod.ZodUnion<[zod.ZodString, zod.ZodArray<zod.ZodArray<zod.ZodString, "many">, "many">]>;
}, "strict", zod.ZodTypeAny, {
body: string | string[][];
title?: string | undefined;
}, {
body: string | string[][];
title?: string | undefined;
}>, "many">>;
details: zod.ZodOptional<zod.ZodUnknown>;
type: zod.ZodLiteral<"external">;
}, "strict", zod.ZodTypeAny, {
type: "external";
message: string;
command: string;
args: string[];
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}, {
type: "external";
message: string;
command: string;
args: string[];
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}>;
export declare const JsonErrorSchema: zod.ZodUnion<[zod.ZodObject<{
message: zod.ZodString;
tryMessage: zod.ZodOptional<zod.ZodString>;
nextSteps: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
customSections: zod.ZodOptional<zod.ZodArray<zod.ZodObject<{
title: zod.ZodOptional<zod.ZodString>;
body: zod.ZodUnion<[zod.ZodString, zod.ZodArray<zod.ZodArray<zod.ZodString, "many">, "many">]>;
}, "strict", zod.ZodTypeAny, {
body: string | string[][];
title?: string | undefined;
}, {
body: string | string[][];
title?: string | undefined;
}>, "many">>;
details: zod.ZodOptional<zod.ZodUnknown>;
type: zod.ZodLiteral<"abort">;
}, "strict", zod.ZodTypeAny, {
type: "abort";
message: string;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}, {
type: "abort";
message: string;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}>, zod.ZodObject<{
stack: zod.ZodOptional<zod.ZodString>;
message: zod.ZodString;
tryMessage: zod.ZodOptional<zod.ZodString>;
nextSteps: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
customSections: zod.ZodOptional<zod.ZodArray<zod.ZodObject<{
title: zod.ZodOptional<zod.ZodString>;
body: zod.ZodUnion<[zod.ZodString, zod.ZodArray<zod.ZodArray<zod.ZodString, "many">, "many">]>;
}, "strict", zod.ZodTypeAny, {
body: string | string[][];
title?: string | undefined;
}, {
body: string | string[][];
title?: string | undefined;
}>, "many">>;
details: zod.ZodOptional<zod.ZodUnknown>;
type: zod.ZodLiteral<"bug">;
}, "strict", zod.ZodTypeAny, {
type: "bug";
message: string;
stack?: string | undefined;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}, {
type: "bug";
message: string;
stack?: string | undefined;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}>, zod.ZodObject<{
command: zod.ZodString;
args: zod.ZodArray<zod.ZodString, "many">;
message: zod.ZodString;
tryMessage: zod.ZodOptional<zod.ZodString>;
nextSteps: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
customSections: zod.ZodOptional<zod.ZodArray<zod.ZodObject<{
title: zod.ZodOptional<zod.ZodString>;
body: zod.ZodUnion<[zod.ZodString, zod.ZodArray<zod.ZodArray<zod.ZodString, "many">, "many">]>;
}, "strict", zod.ZodTypeAny, {
body: string | string[][];
title?: string | undefined;
}, {
body: string | string[][];
title?: string | undefined;
}>, "many">>;
details: zod.ZodOptional<zod.ZodUnknown>;
type: zod.ZodLiteral<"external">;
}, "strict", zod.ZodTypeAny, {
type: "external";
message: string;
command: string;
args: string[];
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}, {
type: "external";
message: string;
command: string;
args: string[];
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}>]>;
export declare const jsonErrorOutputSchema: import("../json-output-schema.js").JsonOutputSchema<zod.ZodObject<{
error: zod.ZodUnion<[zod.ZodObject<{
message: zod.ZodString;
tryMessage: zod.ZodOptional<zod.ZodString>;
nextSteps: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
customSections: zod.ZodOptional<zod.ZodArray<zod.ZodObject<{
title: zod.ZodOptional<zod.ZodString>;
body: zod.ZodUnion<[zod.ZodString, zod.ZodArray<zod.ZodArray<zod.ZodString, "many">, "many">]>;
}, "strict", zod.ZodTypeAny, {
body: string | string[][];
title?: string | undefined;
}, {
body: string | string[][];
title?: string | undefined;
}>, "many">>;
details: zod.ZodOptional<zod.ZodUnknown>;
type: zod.ZodLiteral<"abort">;
}, "strict", zod.ZodTypeAny, {
type: "abort";
message: string;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}, {
type: "abort";
message: string;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}>, zod.ZodObject<{
stack: zod.ZodOptional<zod.ZodString>;
message: zod.ZodString;
tryMessage: zod.ZodOptional<zod.ZodString>;
nextSteps: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
customSections: zod.ZodOptional<zod.ZodArray<zod.ZodObject<{
title: zod.ZodOptional<zod.ZodString>;
body: zod.ZodUnion<[zod.ZodString, zod.ZodArray<zod.ZodArray<zod.ZodString, "many">, "many">]>;
}, "strict", zod.ZodTypeAny, {
body: string | string[][];
title?: string | undefined;
}, {
body: string | string[][];
title?: string | undefined;
}>, "many">>;
details: zod.ZodOptional<zod.ZodUnknown>;
type: zod.ZodLiteral<"bug">;
}, "strict", zod.ZodTypeAny, {
type: "bug";
message: string;
stack?: string | undefined;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}, {
type: "bug";
message: string;
stack?: string | undefined;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}>, zod.ZodObject<{
command: zod.ZodString;
args: zod.ZodArray<zod.ZodString, "many">;
message: zod.ZodString;
tryMessage: zod.ZodOptional<zod.ZodString>;
nextSteps: zod.ZodOptional<zod.ZodArray<zod.ZodString, "many">>;
customSections: zod.ZodOptional<zod.ZodArray<zod.ZodObject<{
title: zod.ZodOptional<zod.ZodString>;
body: zod.ZodUnion<[zod.ZodString, zod.ZodArray<zod.ZodArray<zod.ZodString, "many">, "many">]>;
}, "strict", zod.ZodTypeAny, {
body: string | string[][];
title?: string | undefined;
}, {
body: string | string[][];
title?: string | undefined;
}>, "many">>;
details: zod.ZodOptional<zod.ZodUnknown>;
type: zod.ZodLiteral<"external">;
}, "strict", zod.ZodTypeAny, {
type: "external";
message: string;
command: string;
args: string[];
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}, {
type: "external";
message: string;
command: string;
args: string[];
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
}>]>;
}, "strict", zod.ZodTypeAny, {
error: {
type: "abort";
message: string;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
} | {
type: "bug";
message: string;
stack?: string | undefined;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
} | {
type: "external";
message: string;
command: string;
args: string[];
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
};
}, {
error: {
type: "abort";
message: string;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
} | {
type: "bug";
message: string;
stack?: string | undefined;
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
} | {
type: "external";
message: string;
command: string;
args: string[];
nextSteps?: string[] | undefined;
customSections?: {
body: string | string[][];
title?: string | undefined;
}[] | undefined;
details?: unknown;
tryMessage?: string | undefined;
};
}>>;
packages/cli-kit/dist/public/node/error/types.d.tsimport type { JsonAbortErrorSchema, JsonBugErrorSchema, JsonErrorCustomSectionSchema, JsonErrorSchema, JsonExternalErrorSchema, jsonErrorOutputSchema } from './schema.js';
import type { z } from 'zod';
export type JsonErrorCustomSection = z.infer<typeof JsonErrorCustomSectionSchema>;
export type JsonAbortError = z.infer<typeof JsonAbortErrorSchema>;
export type JsonBugError = z.infer<typeof JsonBugErrorSchema>;
export type JsonExternalError = z.infer<typeof JsonExternalErrorSchema>;
export type JsonError = z.infer<typeof JsonErrorSchema>;
export type JsonErrorType = JsonError['type'];
export type JsonErrorDocument = z.infer<typeof jsonErrorOutputSchema.schema>;
Existing type declarationspackages/cli-kit/dist/public/node/base-command.d.ts@@ -1,5 +1,6 @@
import { Command } from '@oclif/core';
import { OutputFlags, Input, ParserOutput, FlagInput, OutputArgs } from '@oclif/core/parser';
+import type { JsonOutputSchema } from './json-output-schema.js';
export type ArgOutput = OutputArgs<any>;
export type FlagOutput = OutputFlags<any>;
export interface NonTTYFlagRequirement {
@@ -10,14 +11,19 @@ export interface NonTTYFlagRequirement {
}
declare abstract class BaseCommand extends Command {
static baseFlags: FlagInput<{}>;
+ static descriptionWithMarkdown?: string;
+ static get jsonOutputSchema(): JsonOutputSchema | undefined;
static get requiresSyncAnalytics(): boolean;
static nonTTYFlagRequirements(_flags: FlagOutput): NonTTYFlagRequirement[];
+ static descriptionForHelp(): string | undefined;
+ /** @deprecated Use descriptionForHelp instead. */
static descriptionWithoutMarkdown(): string | undefined;
static analyticsNameOverride(): string | undefined;
static analyticsStopCommand(): string | undefined;
catch(error: Error & {
skipOclifErrorHandling: boolean;
}): Promise<void>;
+ protected _run<T>(): Promise<T>;
protected init(): Promise<unknown>;
protected showNpmFlagWarning(): void;
protected exitWithTimestampWhenEnvVariablePresent(): void;
packages/cli-kit/dist/public/node/environment.d.ts@@ -42,9 +42,10 @@ export declare function getIdentityTokenInformation(): {
* Checks if the JSON output is enabled via flag (--json or -j) or environment variable (SHOPIFY_FLAG_JSON).
*
* @param environment - Process environment variables.
+ * @param argv - Command arguments to inspect for JSON flags.
* @returns True if the JSON output is enabled, false otherwise.
*/
-export declare function jsonOutputEnabled(environment?: NodeJS.ProcessEnv): boolean;
+export declare function jsonOutputEnabled(environment?: NodeJS.ProcessEnv, argv?: string[]): boolean;
/**
* If true, the CLI should not use the network level retry.
*
packages/cli-kit/dist/public/node/error.d.ts@@ -1,87 +1 @@
-import { OutputMessage } from './output.js';
-import { type InlineToken, type TokenItem } from '../../private/node/ui/components/token-item.js';
-import type { AlertCustomSection } from './ui.js';
-export declare enum FatalErrorType {
- Abort = 0,
- AbortSilent = 1,
- Bug = 2
-}
-export declare class CancelExecution extends Error {
-}
-/**
- * A fatal error represents an error shouldn't be rescued and that causes the execution to terminate.
- * There shouldn't be code that catches fatal errors.
- */
-export declare abstract class FatalError extends Error {
- tryMessage: TokenItem | null;
- type: FatalErrorType;
- nextSteps?: TokenItem<InlineToken>[];
- formattedMessage?: TokenItem;
- customSections?: AlertCustomSection[];
- skipOclifErrorHandling: boolean;
- /**
- * Creates a new FatalError error.
- *
- * @param message - The error message.
- * @param type - The type of fatal error.
- * @param tryMessage - The message that recommends next steps to the user.
- * You can pass a string a {@link TokenizedString} or a {@link TokenItem}
- * if you need to style the message inside the error Banner component.
- * @param nextSteps - Message to show as "next steps" with suggestions to solve the issue.
- * @param customSections - Custom sections to show in the error banner. To be used if nextSteps is not enough.
- */
- constructor(message: TokenItem | OutputMessage, type: FatalErrorType, tryMessage?: TokenItem | OutputMessage | null, nextSteps?: TokenItem<InlineToken>[], customSections?: AlertCustomSection[]);
-}
-/**
- * An abort error is a fatal error that shouldn't be reported as a bug.
- * Those usually represent unexpected scenarios that we can't handle and that usually require some action from the developer.
- */
-export declare class AbortError extends FatalError {
- constructor(message: TokenItem | OutputMessage, tryMessage?: TokenItem | OutputMessage | null, nextSteps?: TokenItem<InlineToken>[], customSections?: AlertCustomSection[]);
-}
-/**
- * An external error is similar to Abort but has extra command and args attributes.
- * This is useful to represent errors coming from external commands, usually executed by execa.
- */
-export declare class ExternalError extends FatalError {
- command: string;
- args: string[];
- constructor(message: OutputMessage, command: string, args: string[], tryMessage?: TokenItem | OutputMessage | null);
-}
-export declare class AbortSilentError extends FatalError {
- constructor();
-}
-/**
- * A bug error is an error that represents a bug and therefore should be reported.
- */
-export declare class BugError extends FatalError {
- constructor(message: TokenItem | OutputMessage, tryMessage?: TokenItem | OutputMessage | null);
-}
-/**
- * A function that handles errors that blow up in the CLI.
- *
- * @param error - Error to be handled.
- * @returns A promise that resolves with the error passed.
- */
-export declare function handler(error: unknown): Promise<unknown>;
-/**
- * A function that maps an error to an Abort with the stack trace when coming from the CLI.
- *
- * @param error - Error to be mapped.
- * @returns A promise that resolves with the new error object.
- */
-export declare function errorMapper(error: unknown): Promise<unknown>;
-/**
- * A function that checks if an error should be reported as unexpected.
- *
- * @param error - Error to be checked.
- * @returns A boolean indicating if the error should be reported as unexpected.
- */
-export declare function shouldReportErrorAsUnexpected(error: unknown): boolean;
-/**
- * Stack traces usually have file:// - we strip that and also remove the Windows drive designation.
- *
- * @param filePath - Path to be cleaned.
- * @returns The cleaned path.
- */
-export declare function cleanSingleStackTracePath(filePath: string): string;
\ No newline at end of file
+export * from './error/index.js';
\ No newline at end of file
packages/cli-kit/dist/public/node/output.d.ts@@ -68,6 +68,12 @@ export declare const clearCollectedLogs: () => void;
* @param content - The content to be output to the user.
*/
export declare function outputResult(content: OutputMessage): void;
+/**
+ * Waits for queued stdout writes to reach their destination before the process exits.
+ *
+ * @returns A promise that resolves when stdout has flushed.
+ */
+export declare function flushStdout(): Promise<void>;
/**
* Logs information at the info level.
* Info messages don't get additional formatting.
|

WHY are these changes introduced?
Closes shop/issues-develop#23662
Commands using JSON output need one machine-readable fatal error on stdout.
Based on #8182.
WHAT is this pull request doing?
--jsonafter the--boundary.How to test your changes?
pnpm shopify app info --wrong-flagpnpm shopify app info --wrong-flag --jsonAdd
if (1 === 1) throw new Error('boom')at the beginning of commands/app/info.ts and run:pnpm shopify app infopnpm shopify app info --jsonChecklist