diff --git a/src/sfCommand.ts b/src/sfCommand.ts index c07a2b1da..9d3d48713 100644 --- a/src/sfCommand.ts +++ b/src/sfCommand.ts @@ -432,12 +432,21 @@ export abstract class SfCommand extends Command { protected async catch(error: Error | SfError | SfCommand.Error): Promise { // transform an unknown error into one that conforms to the interface - const codeFromError = error instanceof SfError ? error.exitCode : 1; + + // @ts-expect-error because exitCode is not on Error + const codeFromError = (error.exitCode as number) ?? 1; process.exitCode ??= codeFromError; - const sfErrorProperties = - error instanceof SfError - ? { data: error.data, actions: error.actions, code: codeFromError, context: error.context } - : {}; + + const sfErrorProperties = removeEmpty({ + // @ts-expect-error because data is not on Error + data: (error.data as unknown) ?? null, + // @ts-expect-error because actions is not on Error + actions: (error.actions as string[]) ?? null, + code: codeFromError, + // @ts-expect-error because context is not on Error + context: (error.context as string) ?? null, + }); + const sfCommandError: SfCommand.Error = { ...sfErrorProperties, ...{ @@ -454,7 +463,8 @@ export abstract class SfCommand extends Command { } else { this.logToStderr(this.formatError(sfCommandError)); } - return sfCommandError; + + throw sfCommandError; } /** @@ -526,3 +536,8 @@ export namespace SfCommand { context?: string; } } + +function removeEmpty(obj: Record): Record { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v != null)); +}