kipcole9 / money

Elixir implementation of Money with Currency
https://hex.pm/packages/ex_money
Other
571 stars 47 forks source link

FunctionClauseError in `Exhanges.Retriever` #170

Closed PJUllrich closed 2 months ago

PJUllrich commented 2 months ago

Hey @kipcole9, I started getting exceptions from the Money.ExchangeRates.Retriever module yesterday. I think it can't parse the response from OpenExchangeRates anymore. Here's the error log:

[ExchangeRates.Retriever] Terminate called with unhandled {:function_clause, [{:json, :decode, [~c"{\n  \"disclaimer\": \"Usage subject to terms: https://openexchangerates.org/terms\",\n  \"license\": \"https://openexchangerates.org/license\",\n  \"timestamp\": 1723536000,\n  \"base\": \"USD\",\n  \"rates\": {\n    \"AED\": 3.67299,\n    \"AFN\": 70.563637,\n    \"ALL\": 91.418942,\n    \"AMD\": 388.107863,\n    \"ANG\": 1.801655,\n    \"AOA\": 879.690333,\n    \"ARS\": 938.776719,\n    \"AUD\": 1.513515,\n    \"AWG\": 1.8,\n    \"AZN\": 1.7,\n    \"BAM\": 1.789395,\n    \"BBD\": 2,\n    \"BDT\": 117.524816,\n    \"BGN\": 1.788545,\n    \"BHD\": 0.37687,\n    \"BIF\": 2882.029399,\n    \"BMD\": 1,\n    \"BND\": 1.324227,\n    \"BOB\": 6.911419,\n    \"BRL\": 5.493285,\n    \"BSD\": 1,\n    \"BTC\": 0.000016881385,\n    \"BTN\": 83.936166,\n    \"BWP\": 13.50973,\n    \"BYN\": 3.271561,\n    \"BZD\": 2.015105,\n    \"CAD\": 1.372773,\n    \"CDF\": 2844.122435,\n    \"CHF\": 0.867046,\n    \"CLF\": 0.03385,\n    \"CLP\": 934.29972,\n    \"CNH\": 7.173046,\n    \"CNY\": 7.1726,\n    \"COP\": 4070.020633,\n    \"CRC\": 529.580081,\n    \"CUC\": 1,\n    \"CUP\": 25.75,\n    \"CVE\": 100.883344,\n    \"CZK\": 23.021001,\n    \"DJF\": 178.016275,\n    \"DKK\": 6.827497,\n    \"DOP\": 59.753846,\n    \"DZD\": 134.483994,\n    \"EGP\": 49.355212,\n    \"ERN\": 15,\n    \"ETB\": 104.408466,\n    \"EUR\": 0.914896,\n    \"FJD\": 2.23975,\n    \"FKP\": 0.780721,\n    \"GBP\": 0.780721,\n    \"GEL\": 2.68,\n    \"GGP\": 0.780721,\n    \"GHS\": 15.570062,\n    \"GIP\": 0.780721,\n    \"GMD\": 69,\n    \"GNF\": 8625.750479,\n    \"GTQ\": 7.751917,\n    \"GYD\": 209.228322,\n    \"HKD\": 7.789179,\n    \"HNL\": 24.834892,\n    \"HRK\": 6.893347,\n    \"HTG\": 131.754833,\n    \"HUF\": 359.538303,\n    \"IDR\": 15824.181302,\n    \"ILS\": 3.772168,\n    \"IMP\": 0.780721,\n    \"INR\": 83.962042,\n    \"IQD\": 1309.612695,\n    \"IRR\": 42105,\n    \"ISK\": 138.24,\n    \"JEP\": 0.780721,\n    \"JMD\": 157.127911,\n    \"JOD\": 0.7088,\n    \"JPY\": 147.8487,\n    \"KES\": 129,\n    \"KGS\": 85.4,\n    \"KHR\": 4097.803194,\n    \"KMF\": 450.849981,\n    \"KPW\": 900,\n    \"KRW\": 1370.413441,\n    \"KWD\": 0.306267,\n    \"KYD\": 0.833124,\n    \"KZT\": 479.663947,\n    \"LAK\": 22146.188021,\n    \"LBP\": 89503.601764,\n    \"LKR\": 299.095416,\n    \"LRD\": 195.334835,\n    \"LSL\": 18.224695,\n    \"LYD\": 4.803337,\n    \"MAD\": 9.817154,\n    \"MDL\": 17.590241,\n    \"MGA\": 4566.364462,\n    \"MKD\": 56.299175,\n    \"MMK\": 2098,\n    \"MNT\": 3398,\n    \"MOP\": 8.026295,\n    \"MRU\": 39.725601,\n    \"MUR\": 46.369997,\n    \"MVR\": 15.4,\n    \"MWK\": 1733.497159,\n    \"MXN\": 18.973864,\n    \"MYR\": 4.448,\n    \"MZN\": 63.899991,\n    \"NAD\": 18.224695,\n    \"NGN\": 1590,\n    \"NIO\": 36.797298,\n    \"NOK\": 10.773936,\n    \"NPR\": 134.371727,\n    \"NZD\": 1.654869,\n    \"OMR\": 0.384898,\n    \"PAB\": 1,\n    \"PEN\": 3.728288,\n    \"PGK\": 3.882909,\n    \"PHP\": 56.956503,\n    \"PKR\": 278.513399,\n    \"PLN\": 3.929201,\n    \"PYG\": 7564.511343,\n    \"QAR\": 3.656833,\n    \"RON\": 4.5535,\n    \"RSD\": 107.064,\n    \"RUB\": 93.249619,\n    \"RWF\": 1323.611013,\n    \"SAR\": 3.75434,\n    \"SBD\": 8.489576,\n    \"SCR\": 13.620311,\n    \"SDG\": 601.5,\n    \"SEK\": 10.513385,\n    \"SGD\": 1.323305,\n    \"SHP\": 0.780721,\n    \"SLL\": 20969.5,\n    \"SOS\": 571.27365,\n    \"SRD\": 28.821,\n    \"SSP\": 130.26,\n    \"STD\": 22281.8,\n    \"STN\": 22.765639,\n    \"SVC\": 8.747524,\n    \"SYP\": 2512.53,\n    \"SZL\": 18.21883,\n    \"THB\": 35.16575,\n    \"TJS\": 10.596663,\n    \"TMT\": 3.51,\n    \"TND\": 3.083272,\n    \"TOP\": 2.37102,\n    \"TRY\": 33.56969,\n    \"TTD\": 6.79692,\n    \"TWD\": 32.409834,\n    \"TZS\": 2709.148196,\n    \"UAH\": 41.289852,\n    \"UGX\": 3729.178433,\n    \"USD\": 1,\n    \"UYU\": 40.231517,\n    \"UZS\": 12638.888617,\n    \"VES\": 36.584798,\n    \"VND\": 25128.78542,\n    \"VUV\": 118.722,\n    \"WST\": 2.8,\n    \"XAF\": 600.132207,\n    \"XAG\": 0.03598857,\n    \"XAU\": 0.0004058,\n    \"XCD\": 2.70255,\n    \"XDR\": 0.749412,\n    \"XOF\": 600.132207,\n    \"XPD\": 0.0011079,\n    \"XPF\": 109.176092,\n    \"XPT\": 0.00106987,\n    \"YER\": 250.399984,\n    \"ZAR\": 18.2024,\n    \"ZMW\": 26.166634,\n    \"ZWL\": 322\n  }\n}", :ok, %{null: nil}], [file: ~c"json.erl", line: 681]}, {Cldr.Json, :decode!, 1, [file: ~c"lib/cldr/utils/json.ex", line: 34]}, {Money.ExchangeRates.OpenExchangeRates, :decode_rates, 1, [file: ~c"lib/money/exchange_rates/open_exchange_rates.ex", line: 53]}, {Money.ExchangeRates.Retriever, :process_response, 3, [file: ~c"lib/money/exchange_rates/exchange_rates_retriever.ex", line: 205]}, {Money.ExchangeRates.Retriever, :retrieve_latest_rates, 1, [file: ~c"lib/money/exchange_rates/exchange_rates_retriever.ex", line: 357]}, {Money.ExchangeRates.Retriever, :handle_call, 3, [file: ~c"lib/money/exchange_rates/exchange_rates_retriever.ex", line: 297]}, {:gen_server, :try_handle_call, 4, [file: ~c"gen_server.erl", line: 2209]}, {:gen_server, :handle_msg, 6, [file: ~c"gen_server.erl", line: 2238]}]}
kipcole9 commented 2 months ago

Oh, that’s not good! Thanks for the issue. I will do my best to resolve in the next 24 hours.

PJUllrich commented 2 months ago

I think the error lies in either ex_cldr, ex_cldr_currencies, or ex_cldr_numbers because it occurred after I upgraded to their latest versions. Once I downgraded back to these versions, it started working again:

Package Broken Version Working Version
ex_cldr 2.40.0 2.39.2
ex_cldr_currencies 2.16.2 2.16.1
ex_cldr_numbers 2.33.2 2.33.1

Thank you and I hope it helps! ❤️

PJUllrich commented 2 months ago

It seems that the ExchangeRatesRetriever uses Cldr for fetching the rates. This might be where the error occurs: https://github.com/kipcole9/money/blob/main/lib/money/exchange_rates/exchange_rates_retriever.ex#L200

kipcole9 commented 2 months ago

@PJUllrich, the bug was related to the Cldr.Json.decode!/1 JSON shim that is part of cldr_utils. This module is only used when running on OTP 27+ since it wraps the new :json module.

The shim was only written to support binary arguments, whereas the exchange rate data is a chartist. I've now fixed that.

The resolution is to mix deps.update cldr_utils ex_cldr_numbers and you should be good to go. Please let me know if that resolves the issue for you?

PJUllrich commented 2 months ago

@kipcole9 yes, that worked :) thank you!