Skip to content

Commit

Permalink
fix: throw from catch (#229)
Browse files Browse the repository at this point in the history
* fix: throw error from catch method

* chore: revert catch method signature

* fix: stop checking for SfError instance
  • Loading branch information
mdonnalley authored Feb 21, 2023
1 parent 4629022 commit 6dcb16f
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions src/sfCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,21 @@ export abstract class SfCommand<T> extends Command {

protected async catch(error: Error | SfError | SfCommand.Error): Promise<SfCommand.Error> {
// 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,
...{
Expand All @@ -454,7 +463,8 @@ export abstract class SfCommand<T> extends Command {
} else {
this.logToStderr(this.formatError(sfCommandError));
}
return sfCommandError;

throw sfCommandError;
}

/**
Expand Down Expand Up @@ -526,3 +536,8 @@ export namespace SfCommand {
context?: string;
}
}

function removeEmpty(obj: Record<string, unknown>): Record<string, unknown> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v != null));
}

0 comments on commit 6dcb16f

Please sign in to comment.