Skip to content

Commit

Permalink
feat: catch method supports base and SfError
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Jun 3, 2022
1 parent 950c6e1 commit 2b9a038
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"/messages"
],
"dependencies": {
"@oclif/core": "^1.7.0",
"@oclif/core": "^1.9.0",
"@salesforce/core": "^3.15.5",
"@salesforce/kit": "^1.5.41",
"@salesforce/ts-types": "^1.5.20",
Expand Down
27 changes: 17 additions & 10 deletions src/sfCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Lifecycle,
Mode,
EnvironmentVariable,
SfError,
} from '@salesforce/core';
import { AnyJson } from '@salesforce/ts-types';
import * as chalk from 'chalk';
Expand Down Expand Up @@ -238,12 +239,9 @@ export abstract class SfCommand<T> extends Command {
/**
* Wrap the command error into the standardized JSON structure.
*/
protected toErrorJson(error: Error): SfCommand.Error {
protected toErrorJson(error: SfCommand.Error): SfCommand.Error {
return {
status: process.exitCode ?? 1,
stack: error.stack,
name: error.name,
message: error.message,
...error,
warnings: this.warnings,
};
}
Expand All @@ -259,15 +257,23 @@ export abstract class SfCommand<T> extends Command {
}
}

protected async catch(error: SfCommand.Error): Promise<SfCommand.Error> {
process.exitCode = process.exitCode ?? error.exitCode ?? 1;
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;
process.exitCode ??= codeFromError;
const sfCommandError = {
...error,
status: process.exitCode,
stack: error.stack,
};

if (this.jsonEnabled()) {
CliUx.ux.styledJSON(this.toErrorJson(error));
CliUx.ux.styledJSON(this.toErrorJson(sfCommandError));
} else {
// eslint-disable-next-line no-console
console.error(this.formatError(error));
console.error(this.formatError(sfCommandError));
}
return error;
return sfCommandError;
}

/**
Expand Down Expand Up @@ -334,5 +340,6 @@ export namespace SfCommand {
actions?: string[];
code?: unknown;
exitCode?: number;
data?: unknown;
}
}
27 changes: 20 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -492,10 +492,10 @@
widest-line "^3.1.0"
wrap-ansi "^7.0.0"

"@oclif/core@^1.7.0":
version "1.7.0"
resolved "https://registry.npmjs.org/@oclif/core/-/core-1.7.0.tgz#3b763b53eafa9afbb13cf7cdfeac0f8ddd8ab1af"
integrity sha512-I4q4qgtnNG7ef4sBDrJhwADdi7RExQV7LnflnXaWZZDiUoqF9AdOjC4jjx40MK0rRrCehCUtPNZBcr3WmxuS4Q==
"@oclif/core@^1.9.0":
version "1.9.0"
resolved "https://registry.yarnpkg.com/@oclif/core/-/core-1.9.0.tgz#bb2a7820a9176f28921f449c0f577d39c15e74d0"
integrity sha512-duvlaRQf4JM+mKuwwos1DNa/Q9x6tnF3khV5RU0fy5hhETF7THlTmxioKlIvKMyQDVpySqtZXZ0OKHeCi2EWuQ==
dependencies:
"@oclif/linewrap" "^1.0.0"
"@oclif/screen" "^3.0.2"
Expand All @@ -505,7 +505,7 @@
chalk "^4.1.2"
clean-stack "^3.0.1"
cli-progress "^3.10.0"
debug "^4.3.3"
debug "^4.3.4"
ejs "^3.1.6"
fs-extra "^9.1.0"
get-package-type "^0.1.0"
Expand All @@ -514,11 +514,10 @@
indent-string "^4.0.0"
is-wsl "^2.2.0"
js-yaml "^3.14.1"
lodash "^4.17.21"
natural-orderby "^2.0.3"
object-treeify "^1.1.33"
password-prompt "^1.1.2"
semver "^7.3.5"
semver "^7.3.7"
string-width "^4.2.3"
strip-ansi "^6.0.1"
supports-color "^8.1.1"
Expand Down Expand Up @@ -1942,6 +1941,13 @@ debug@^3.2.7:
dependencies:
ms "^2.1.1"

debug@^4.3.4:
version "4.3.4"
resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865"
integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ==
dependencies:
ms "2.1.2"

decamelize-keys@^1.1.0:
version "1.1.0"
resolved "https://registry.npmjs.org/decamelize-keys/-/decamelize-keys-1.1.0.tgz#d171a87933252807eb3cb61dc1c1445d078df2d9"
Expand Down Expand Up @@ -5019,6 +5025,13 @@ semver@^6.0.0, semver@^6.1.2, semver@^6.3.0:
resolved "https://registry.npmjs.org/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==

semver@^7.3.7:
version "7.3.7"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.7.tgz#12c5b649afdbf9049707796e22a4028814ce523f"
integrity sha512-QlYTucUYOews+WeEujDoEGziz4K6c47V/Bd+LjSSYcA94p+DmINdf7ncaUinThfvZyu13lN9OY1XDxt8C0Tw0g==
dependencies:
lru-cache "^6.0.0"

sentence-case@^3.0.4:
version "3.0.4"
resolved "https://registry.yarnpkg.com/sentence-case/-/sentence-case-3.0.4.tgz#3645a7b8c117c787fde8702056225bb62a45131f"
Expand Down

0 comments on commit 2b9a038

Please sign in to comment.