From 6dcb16f5cc662a07fb2217ffc5401c7ff7a14d61 Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Tue, 21 Feb 2023 08:08:25 -0800 Subject: [PATCH] fix: throw from catch (#229) * fix: throw error from catch method * chore: revert catch method signature * fix: stop checking for SfError instance --- src/sfCommand.ts | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/sfCommand.ts b/src/sfCommand.ts index c07a2b1d..9d3d4871 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)); +}