From 5263fe2ee8045e1162492d00ac90de6cb62283e5 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 7 Mar 2019 19:03:40 -0700 Subject: [PATCH] fix: @W-5907580@ respect code if it exist --- src/sfdxError.ts | 8 +++++++- test/unit/sfdxErrorTest.ts | 12 ++++++++++++ yarn.lock | 38 +++----------------------------------- 3 files changed, 22 insertions(+), 36 deletions(-) diff --git a/src/sfdxError.ts b/src/sfdxError.ts index 19f2217f1b..2836cf7248 100644 --- a/src/sfdxError.ts +++ b/src/sfdxError.ts @@ -6,7 +6,7 @@ */ import { NamedError } from '@salesforce/kit'; -import { ensure, isString, JsonMap, Optional } from '@salesforce/ts-types'; +import { ensure, isString, JsonMap, Optional, getString, hasString, ensureString } from '@salesforce/ts-types'; import { Messages, Tokens } from './messages'; /** @@ -205,6 +205,10 @@ export class SfdxError extends NamedError { sfdxError.stack = sfdxError.stack.replace(`${err.name}: ${err.message}`, 'Outer stack:'); sfdxError.stack = `${err.stack}\n${sfdxError.stack}`; } + // Some errors may have a code, so don't look that information + if (hasString(err, 'code')) { + sfdxError.code = ensureString(getString(err, 'code')); + } return sfdxError; } @@ -231,6 +235,8 @@ export class SfdxError extends NamedError { // Additional data helpful for consumers of this error. E.g., API call result public data: any; // tslint:disable-line:no-any + public code?: string; + /** * Create an SfdxError. * @param message The error message. diff --git a/test/unit/sfdxErrorTest.ts b/test/unit/sfdxErrorTest.ts index 24b1716414..cd5be7e6e7 100644 --- a/test/unit/sfdxErrorTest.ts +++ b/test/unit/sfdxErrorTest.ts @@ -103,6 +103,18 @@ describe('SfdxError', () => { .to.contain('Outer stack:\n') .and.contain(myError.stack); }); + + it('should return a wrapped error with a code', () => { + class CodeError extends Error { + public code?: string; + } + const myErrorCode = 'OhMyError'; + const myError = new CodeError('test'); + myError.code = myErrorCode; + const mySfdxError = SfdxError.wrap(myError); + expect(mySfdxError).to.be.an.instanceOf(SfdxError); + expect(mySfdxError.code).to.equal(myErrorCode); + }); }); describe('toObject', () => { diff --git a/yarn.lock b/yarn.lock index a3363c5326..83ccf5affa 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1781,7 +1781,7 @@ debug@^4.0.0, debug@^4.1.0: dependencies: ms "^2.1.1" -debuglog@*, debuglog@^1.0.1: +debuglog@^1.0.1: version "1.0.1" resolved "https://registry.npmjs.org/debuglog/-/debuglog-1.0.1.tgz#aa24ffb9ac3df9a2351837cfb2d279360cd78492" integrity sha1-qiT/uaw9+aI1GDfPstJ5NgzXhJI= @@ -2944,7 +2944,7 @@ import-lazy@^2.1.0: resolved "https://registry.npmjs.org/import-lazy/-/import-lazy-2.1.0.tgz#05698e3d45c88e8d7e9d92cb0584e77f096f3e43" integrity sha1-BWmOPUXIjo1+nZLLBYTnfwlvPkM= -imurmurhash@*, imurmurhash@^0.1.4: +imurmurhash@^0.1.4: version "0.1.4" resolved "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea" integrity sha1-khi5srkoojixPcT7a21XbyMUU+o= @@ -3743,11 +3743,6 @@ lodash-cli@^4: semver "5.3.0" uglify-js "2.7.5" -lodash._baseindexof@*: - version "3.1.0" - resolved "https://registry.npmjs.org/lodash._baseindexof/-/lodash._baseindexof-3.1.0.tgz#fe52b53a1c6761e42618d654e4a25789ed61822c" - integrity sha1-/lK1OhxnYeQmGNZU5KJXie1hgiw= - lodash._baseuniq@~4.6.0: version "4.6.0" resolved "https://registry.npmjs.org/lodash._baseuniq/-/lodash._baseuniq-4.6.0.tgz#0ebb44e456814af7905c6212fa2c9b2d51b841e8" @@ -3756,33 +3751,11 @@ lodash._baseuniq@~4.6.0: lodash._createset "~4.0.0" lodash._root "~3.0.0" -lodash._bindcallback@*: - version "3.0.1" - resolved "https://registry.npmjs.org/lodash._bindcallback/-/lodash._bindcallback-3.0.1.tgz#e531c27644cf8b57a99e17ed95b35c748789392e" - integrity sha1-5THCdkTPi1epnhftlbNcdIeJOS4= - -lodash._cacheindexof@*: - version "3.0.2" - resolved "https://registry.npmjs.org/lodash._cacheindexof/-/lodash._cacheindexof-3.0.2.tgz#3dc69ac82498d2ee5e3ce56091bafd2adc7bde92" - integrity sha1-PcaayCSY0u5ePOVgkbr9Ktx73pI= - -lodash._createcache@*: - version "3.1.2" - resolved "https://registry.npmjs.org/lodash._createcache/-/lodash._createcache-3.1.2.tgz#56d6a064017625e79ebca6b8018e17440bdcf093" - integrity sha1-VtagZAF2JeeevKa4AY4XRAvc8JM= - dependencies: - lodash._getnative "^3.0.0" - lodash._createset@~4.0.0: version "4.0.3" resolved "https://registry.npmjs.org/lodash._createset/-/lodash._createset-4.0.3.tgz#0f4659fbb09d75194fa9e2b88a6644d363c9fe26" integrity sha1-D0ZZ+7CddRlPqeK4imZE02PJ/iY= -lodash._getnative@*, lodash._getnative@^3.0.0: - version "3.9.1" - resolved "https://registry.npmjs.org/lodash._getnative/-/lodash._getnative-3.9.1.tgz#570bc7dede46d61cdcde687d65d3eecbaa3aaff5" - integrity sha1-VwvH3t5G1hzc3mh9ZdPuy6o6r/U= - lodash._reinterpolate@~3.0.0: version "3.0.0" resolved "https://registry.npmjs.org/lodash._reinterpolate/-/lodash._reinterpolate-3.0.0.tgz#0ccf2d89166af03b3663c796538b75ac6e114d9d" @@ -3833,11 +3806,6 @@ lodash.map@^4.5.1: resolved "https://registry.npmjs.org/lodash.map/-/lodash.map-4.6.0.tgz#771ec7839e3473d9c4cde28b19394c3562f4f6d3" integrity sha1-dx7Hg540c9nEzeKLGTlMNWL09tM= -lodash.restparam@*: - version "3.6.1" - resolved "https://registry.npmjs.org/lodash.restparam/-/lodash.restparam-3.6.1.tgz#936a4e309ef330a7645ed4145986c85ae5b20805" - integrity sha1-k2pOMJ7zMKdkXtQUWYbIWuWyCAU= - lodash.set@^4.3.2: version "4.3.2" resolved "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz#d8757b1da807dde24816b0d6a84bea1a76230b23" @@ -5529,7 +5497,7 @@ readable-stream@~1.1.10: isarray "0.0.1" string_decoder "~0.10.x" -readdir-scoped-modules@*, readdir-scoped-modules@^1.0.0: +readdir-scoped-modules@^1.0.0: version "1.0.2" resolved "https://registry.npmjs.org/readdir-scoped-modules/-/readdir-scoped-modules-1.0.2.tgz#9fafa37d286be5d92cbaebdee030dc9b5f406747" integrity sha1-n6+jfShr5dksuuve4DDcm19AZ0c=