-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
66 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/** | ||
* Mutate: enrich error with details | ||
* @param {Error} error | ||
* @param {...any} arguments | ||
* @return {void} | ||
*/ | ||
module.exports = function betterror(error, details) { | ||
if (details && typeof details === 'object') { | ||
error.details = Object.assign( | ||
error.details || {}, | ||
details | ||
); | ||
} | ||
return error; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const betterror = require('.'); | ||
|
||
describe('betterror', () => { | ||
it('Should return the original error', () => { | ||
const error = new Error('Something must have gone terribly wrong'); | ||
expect(betterror(error)).to.equal(error); | ||
}); | ||
it('Should not a "details" field when not applicable', () => { | ||
const error = betterror(new Error('Something must have gone terribly wrong')); | ||
expect(error).to.not.have.keys(['details']); | ||
}); | ||
it('Should add a "details" field when applicable', () => { | ||
const error = betterror(new Error('Something must have gone terribly wrong'), { a: 1 }); | ||
expect(error).to.have.keys(['details']); | ||
}); | ||
it('Should add "details" to error', () => { | ||
const error = betterror(new Error('Something must have gone terribly wrong'), { a: 1 }); | ||
expect(error.details).to.deep.equal({ a: 1 }); | ||
}); | ||
it('Should assign details if there is are some already', () => { | ||
const err = new Error('Something must have gone terribly wrong'); | ||
err.details = { a: 1 }; | ||
const error = betterror(err, { b: 2 }); | ||
expect(error.details).to.deep.equal({ a: 1, b: 2 }); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters