Skip to content

Commit

Permalink
feat: support wrapping a string
Browse files Browse the repository at this point in the history
  • Loading branch information
amphro committed Mar 19, 2019
1 parent 8a6fc47 commit 2445003
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/sfdxError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:');
Expand Down
6 changes: 6 additions & 0 deletions test/unit/sfdxErrorTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit 2445003

Please sign in to comment.