From 2b9a038713c77e1230e21f5cc6f8ae2a6aaab5be Mon Sep 17 00:00:00 2001 From: mshanemc Date: Fri, 3 Jun 2022 16:21:37 -0500 Subject: [PATCH] feat: catch method supports base and SfError --- package.json | 2 +- src/sfCommand.ts | 27 +++++++++++++++++---------- yarn.lock | 27 ++++++++++++++++++++------- 3 files changed, 38 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index 523a8daf..ee734283 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/sfCommand.ts b/src/sfCommand.ts index ae013193..85adc4c3 100644 --- a/src/sfCommand.ts +++ b/src/sfCommand.ts @@ -14,6 +14,7 @@ import { Lifecycle, Mode, EnvironmentVariable, + SfError, } from '@salesforce/core'; import { AnyJson } from '@salesforce/ts-types'; import * as chalk from 'chalk'; @@ -238,12 +239,9 @@ export abstract class SfCommand 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, }; } @@ -259,15 +257,23 @@ export abstract class SfCommand extends Command { } } - protected async catch(error: SfCommand.Error): Promise { - process.exitCode = process.exitCode ?? error.exitCode ?? 1; + 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; + 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; } /** @@ -334,5 +340,6 @@ export namespace SfCommand { actions?: string[]; code?: unknown; exitCode?: number; + data?: unknown; } } diff --git a/yarn.lock b/yarn.lock index beea65c5..424a20e0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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" @@ -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"