Skip to content

Commit

Permalink
Do not throw exception when json parsing fails for unstable APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
sjanel committed Mar 27, 2024
1 parent 1a54c18 commit baa5524
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
7 changes: 6 additions & 1 deletion src/api/common/src/fiatconverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,12 @@ std::optional<double> FiatConverter::queryCurrencyRateSource1(Market mk) {

std::optional<double> FiatConverter::queryCurrencyRateSource2(Market mk) {
const auto dataStr = _curlHandle2.query("", CurlOptions(HttpRequestType::kGet));
const json jsonData = json::parse(dataStr);
static constexpr bool kAllowExceptions = false;
const json jsonData = json::parse(dataStr, nullptr, kAllowExceptions);
if (jsonData.is_discarded()) {
log::error("Invalid response received from fiat currency converter service's second source");
return {};
}
const auto baseIt = jsonData.find("base");
const auto ratesIt = jsonData.find("rates");
if (baseIt == jsonData.end() || ratesIt == jsonData.end()) {
Expand Down
5 changes: 3 additions & 2 deletions src/api/common/src/withdrawalfees-crawler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ WithdrawalFeesCrawler::WithdrawalInfoMaps WithdrawalFeesCrawler::WithdrawalFeesF
WithdrawalInfoMaps ret;

if (!withdrawalFeesCsv.empty()) {
const json jsonData = json::parse(withdrawalFeesCsv);
static constexpr bool kAllowExceptions = false;
const json jsonData = json::parse(withdrawalFeesCsv, nullptr, kAllowExceptions);
const auto exchangesIt = jsonData.find("exchange");
if (exchangesIt == jsonData.end()) {
if (jsonData.is_discarded() || exchangesIt == jsonData.end()) {
log::error("no exchange data found in source 1 - either site information unavailable or code to be updated");
return ret;
}
Expand Down
1 change: 1 addition & 0 deletions src/objects/include/monetaryamount.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class MonetaryAmount {

/// Constructs a new MonetaryAmount from a string representing the amount only and a currency code.
/// Precision is calculated automatically.
/// If 'amountStr' is empty, the amount will be set to 0.
MonetaryAmount(std::string_view amountStr, CurrencyCode currencyCode);

/// Constructs a new MonetaryAmount from another MonetaryAmount and a new CurrencyCode.
Expand Down
1 change: 1 addition & 0 deletions src/objects/test/monetaryamount_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ TEST(MonetaryAmountTest, StringConstructor) {
EXPECT_EQ(MonetaryAmount("05AUD"), MonetaryAmount(5, "AUD"));
EXPECT_EQ(MonetaryAmount("746REPV2"), MonetaryAmount("746", "REPV2"));

EXPECT_EQ(MonetaryAmount(""), MonetaryAmount());
EXPECT_EQ(MonetaryAmount("", "EUR"), MonetaryAmount(0, "EUR"));

EXPECT_THROW(MonetaryAmount("usdt"), invalid_argument);
Expand Down
23 changes: 10 additions & 13 deletions src/tech/include/cachedresult.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,14 @@ class CachedResultT : public CachedResultBase<typename ClockT::duration> {
}
}

CachedResultT(const CachedResultT &) = delete;
CachedResultT &operator=(const CachedResultT &) = delete;

CachedResultT(CachedResultT &&) noexcept = default;
CachedResultT &operator=(CachedResultT &&) noexcept = default;

~CachedResultT() = default;

/// Sets given value associated to the key built with given parameters,
/// if given timestamp is more recent than the one associated to the value already present at this key (if any)
template <class ResultTypeT, class... Args>
void set(ResultTypeT &&val, TimePoint t, Args &&...funcArgs) {
auto [it, inserted] =
_cachedResultsMap.try_emplace(TKey(std::forward<Args &&>(funcArgs)...), std::forward<ResultTypeT>(val), t);
if (!inserted && it->second.second < t) {
it->second = TValue(std::forward<ResultTypeT>(val), t);
void set(ResultTypeT &&val, TimePoint timePoint, Args &&...funcArgs) {
auto [it, inserted] = _cachedResultsMap.try_emplace(TKey(std::forward<Args &&>(funcArgs)...),
std::forward<ResultTypeT>(val), timePoint);
if (!inserted && it->second.second < timePoint) {
it->second = TValue(std::forward<ResultTypeT>(val), timePoint);
}
}

Expand All @@ -78,19 +70,24 @@ class CachedResultT : public CachedResultBase<typename ClockT::duration> {
template <class... Args>
const ResultType &get(Args &&...funcArgs) {
TKey key(std::forward<Args &&>(funcArgs)...);

auto nowTime = ClockT::now();

auto flattenTuple = [this](auto &&...values) { return _func(std::forward<decltype(values) &&>(values)...); };

if (this->_state == State::kForceUniqueRefresh) {
_cachedResultsMap.clear();
this->_state = State::kForceCache;
}

auto it = _cachedResultsMap.find(key);
if (it == _cachedResultsMap.end()) {
TValue val(std::apply(flattenTuple, key), nowTime);
it = _cachedResultsMap.insert_or_assign(std::move(key), std::move(val)).first;
} else if (this->_state != State::kForceCache && this->_refreshPeriod < nowTime - it->second.second) {
it->second = TValue(std::apply(flattenTuple, std::move(key)), nowTime);
}

return it->second.first;
}

Expand Down

0 comments on commit baa5524

Please sign in to comment.