From c76476456f1acef18c0b2441d36de3f55ae3650b Mon Sep 17 00:00:00 2001 From: Illimar Tambek Date: Mon, 14 Dec 2015 21:40:05 +0800 Subject: [PATCH 1/2] Throw actual Error instance and improve error message --- money.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/money.js b/money.js index 359df37..265d445 100644 --- a/money.js +++ b/money.js @@ -75,7 +75,19 @@ rates[fx.base] = 1; // Throw an error if either rate isn't in the rates array - if ( !rates[to] || !rates[from] ) throw "fx error"; + if ( !rates[to] || !rates[from] ) { + var msg = 'Cannot convert ' + from + ' to ' + to + ': '; + + if ( !rates[to] && !rates[from] ) { + msg += 'exhange rates for both currencies are missing'; + } else if ( !rates[to] ) { + msg += 'exhange rate for ' + to + ' is missing'; + } else if ( !rates[from] ) { + msg += 'exhange rate for ' + from + ' is missing'; + } + + throw new Error( msg ); + } // If `from` currency === fx.base, return the basic exchange rate for the `to` currency if ( from === fx.base ) { From 406de6c6a8ba8facf7f71c374d47e42d9ad86160 Mon Sep 17 00:00:00 2001 From: Illimar Tambek Date: Tue, 5 Jan 2016 20:00:26 +0700 Subject: [PATCH 2/2] Fix typo --- money.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/money.js b/money.js index 265d445..88aff08 100644 --- a/money.js +++ b/money.js @@ -79,11 +79,11 @@ var msg = 'Cannot convert ' + from + ' to ' + to + ': '; if ( !rates[to] && !rates[from] ) { - msg += 'exhange rates for both currencies are missing'; + msg += 'exchange rates for both currencies are missing'; } else if ( !rates[to] ) { - msg += 'exhange rate for ' + to + ' is missing'; + msg += 'exchange rate for ' + to + ' is missing'; } else if ( !rates[from] ) { - msg += 'exhange rate for ' + from + ' is missing'; + msg += 'exchange rate for ' + from + ' is missing'; } throw new Error( msg );