From 24450037619c39c361647af4415665499dc518a2 Mon Sep 17 00:00:00 2001 From: Thomas Date: Tue, 19 Mar 2019 12:06:07 -0600 Subject: [PATCH] feat: support wrapping a string --- src/sfdxError.ts | 5 ++++- test/unit/sfdxErrorTest.ts | 6 ++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/sfdxError.ts b/src/sfdxError.ts index 3e83258cc7..dd29e31ec5 100644 --- a/src/sfdxError.ts +++ b/src/sfdxError.ts @@ -199,7 +199,10 @@ export class SfdxError extends NamedError { * Convert an Error to an SfdxError. * @param err The error to convert. */ - public static wrap(err: Error): SfdxError { + public static wrap(err: Error | string): SfdxError { + if (isString(err)) { + return new SfdxError(err); + } const sfdxError = new SfdxError(err.message, err.name); if (sfdxError.stack) { sfdxError.stack = sfdxError.stack.replace(`${err.name}: ${err.message}`, 'Outer stack:'); diff --git a/test/unit/sfdxErrorTest.ts b/test/unit/sfdxErrorTest.ts index cd5be7e6e7..52c5dac85c 100644 --- a/test/unit/sfdxErrorTest.ts +++ b/test/unit/sfdxErrorTest.ts @@ -115,6 +115,12 @@ describe('SfdxError', () => { expect(mySfdxError).to.be.an.instanceOf(SfdxError); expect(mySfdxError.code).to.equal(myErrorCode); }); + + it('should return a new error with just a string', () => { + const mySfdxError = SfdxError.wrap('test'); + expect(mySfdxError).to.be.an.instanceOf(SfdxError); + expect(mySfdxError.message).to.equal('test'); + }); }); describe('toObject', () => {