Skip to content

Commit

Permalink
fix(forex): better error message when exchange rate is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider committed Nov 19, 2016
1 parent ae73372 commit c85acb5
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/money.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ Money.prototype.to = function to(currency) {
.forexService(self.currency, currency)
.then(rate => {
try {
if (rate === undefined) {
return Promise.reject(new Error(`Undefined exchange rate for ${self.currency} to ${currency}`));
}
return new Money(self.amount.times(rate), currency);
} catch (e) {
return Promise.reject(e);
Expand Down
2 changes: 1 addition & 1 deletion test/money.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ describe('Money', () => {
let nzd = new Money('100 NZD');
Money.forexService = () => Promise.resolve(undefined);
return nzd.to('CNY')
.should.be.rejected();
.should.be.rejectedWith({ message: "Undefined exchange rate for NZD to CNY" });
});

it('should reject an exchange rate that is not a number', () => {
Expand Down

0 comments on commit c85acb5

Please sign in to comment.