Skip to content

Wallos: Open-source, self-hostable personal subscription tracker. Visualize your recurring expenses, manage your budget, and save money.

Recover from a refused currency instead of losing the whole refresh to it

thorstenhornung1e603f7272d59
diff --git a/endpoints/cronjobs/updateexchange.php b/endpoints/cronjobs/updateexchange.php
index 9211550..acd59e3 100644
--- a/endpoints/cronjobs/updateexchange.php
+++ b/endpoints/cronjobs/updateexchange.php
@@ -148,7 +148,13 @@ while ($userToUpdateExchange = $usersToUpdateExchange->fetchArray(SQLITE3_ASSOC)
$db->exec('COMMIT');
- echo "Rates updated successfully!<br />";
+ // A currency the provider would not price keeps the rate it had, and
+ // saying which one is the difference between a total somebody can
+ // trust and one they cannot. Only the Frankfurter path fills this.
+ $held = isset($apiData['held']) && is_array($apiData['held']) ? $apiData['held'] : [];
+ echo "Rates updated successfully!" . ($held === []
+ ? ""
+ : " Not priced, so left unchanged: " . htmlspecialchars(implode(', ', $held)) . ".") . "<br />";
}
} else {
// A refresh that stored nothing must not pass for one that
diff --git a/endpoints/currency/update_exchange.php b/endpoints/currency/update_exchange.php
index efa6875..18f1bc2 100644
--- a/endpoints/currency/update_exchange.php
+++ b/endpoints/currency/update_exchange.php
@@ -156,7 +156,13 @@ if ($result) {
$db->exec('COMMIT');
$db->close();
- echo "Rates updated successfully!";
+ // A currency the provider would not price keeps the rate it had, and
+ // saying which one is the difference between a total somebody can
+ // trust and one they cannot. Only the Frankfurter path fills this.
+ $held = isset($apiData['held']) && is_array($apiData['held']) ? $apiData['held'] : [];
+ echo "Rates updated successfully!" . ($held === []
+ ? ""
+ : " Not priced, so left unchanged: " . htmlspecialchars(implode(', ', $held)) . ".") . "";
}
} else {
// A refresh that stored nothing must not pass for one that worked.
diff --git a/includes/frankfurter.php b/includes/frankfurter.php
index 0e63b90..acc2128 100644
--- a/includes/frankfurter.php
+++ b/includes/frankfurter.php
@@ -128,6 +128,49 @@ function frankfurter_detail($decoded)
return trim($decoded['message']);
}
+/**
+ * The codes a 422 named, read out of the provider's own answer.
+ *
+ * Its refusal is {"status":422,"message":"invalid currency: ETH,XYZ"} and it
+ * names every offending code at once, comma separated (measured 2026-09-13 with
+ * one, two and three). That is what makes recovering from it a single retry
+ * rather than a search: ask, and if it refuses, drop exactly what it named and
+ * ask once more.
+ *
+ * Nothing is assumed about the wording beyond the part that matters. If the
+ * message ever stops carrying codes this finds none, nothing is dropped, and
+ * the refusal is reported exactly as it would have been without this - the
+ * recovery can improve the outcome and cannot worsen it.
+ *
+ * @param mixed $decoded json_decode(..., true) of the response body.
+ * @return string[] upper-cased codes, empty when none could be read.
+ */
+function frankfurter_refused_codes($decoded)
+{
+ if (!is_array($decoded) || !isset($decoded['message']) || !is_string($decoded['message'])) {
+ return [];
+ }
+
+ if (preg_match('/invalid currency:\s*(.+)$/i', $decoded['message'], $match) !== 1) {
+ return [];
+ }
+
+ $refused = [];
+
+ foreach (explode(',', $match[1]) as $code) {
+ $code = strtoupper(trim($code));
+
+ // Only something that could have been in the request. A message naming
+ // anything else is not a code list, and acting on it would drop a
+ // currency the provider never objected to.
+ if (preg_match('/^[A-Z]{3}$/', $code) === 1) {
+ $refused[] = $code;
+ }
+ }
+
+ return array_values(array_unique($refused));
+}
+
/**
* The currency codes safe to put in a request, and the ones left behind.
*
@@ -233,8 +276,6 @@ function frankfurter_latest_rates($base, $codes)
];
}
- $apiUrl = 'https://api.frankfurter.dev/v2/rates?base=' . rawurlencode($base)
- . '&quotes=' . rawurlencode(implode(',', $askFor));
$context = stream_context_create([
'http' => [
'method' => 'GET',
@@ -245,13 +286,40 @@ function frankfurter_latest_rates($base, $codes)
]
]);
- $http = frankfurter_http_get($apiUrl, $context);
+ $url = function ($quotes) use ($base) {
+ return 'https://api.frankfurter.dev/v2/rates?base=' . rawurlencode($base)
+ . '&quotes=' . rawurlencode(implode(',', $quotes));
+ };
+
+ $http = frankfurter_http_get($url($askFor), $context);
$decoded = json_decode((string) $http['body'], true);
+
+ // One retry, and only one, because the refusal names every offending code
+ // at once. A single currency the catalogue does not carry otherwise refuses
+ // the request for every other currency in the account, so somebody holding
+ // one gets no rates at all rather than the ones that are fine.
+ //
+ // Only codes the provider itself named are dropped, and they join the list
+ // this function reports, so the answer still says which currencies kept the
+ // rate they had. If nothing can be read from the message, nothing is
+ // dropped and the refusal stands.
+ if (frankfurter_status_code($http['headers']) === 422) {
+ $refused = frankfurter_refused_codes($decoded);
+ $retryWith = array_values(array_diff($askFor, $refused));
+
+ if ($refused !== [] && $retryWith !== []) {
+ $rejected = array_values(array_unique(array_merge($rejected, $refused)));
+ $askFor = $retryWith;
+ $http = frankfurter_http_get($url($askFor), $context);
+ $decoded = json_decode((string) $http['body'], true);
+ }
+ }
+
$rates = frankfurter_rates($decoded);
if ($rates === null) {
return ['message' => frankfurter_failure_message(frankfurter_status_code($http['headers']), $decoded, $base)];
}
- return ['rates' => $rates];
+ return ['rates' => $rates, 'held' => $rejected];
}
diff --git a/tests/cases/currency_provider_frankfurter_test.php b/tests/cases/currency_provider_frankfurter_test.php
index 478859c..d3ebc15 100644
--- a/tests/cases/currency_provider_frankfurter_test.php
+++ b/tests/cases/currency_provider_frankfurter_test.php
@@ -305,8 +305,12 @@ wallos_test('the provider explains which currency it objected to, and that is wh
assert_same('', frankfurter_detail(json_decode('[]', true)),
'and an empty list says nothing, so nothing is invented for it');
+ // Asked with XYZ alone, so the refusal is the whole answer. With another
+ // code beside it the named one is dropped and the request is made again -
+ // that path is its own case below; this one is about the wording surviving
+ // when there is nothing left to ask for.
frankfurter_expect('{"status":422,"message":"invalid currency: XYZ"}', 422);
- $answer = frankfurter_latest_rates('CHF', 'EUR,XYZ');
+ $answer = frankfurter_latest_rates('CHF', 'XYZ');
assert_true(!isset($answer['rates']), 'a 422 is not a set of rates');
assert_contains('invalid currency: XYZ', $answer['message'] ?? '',
@@ -629,3 +633,114 @@ wallos_test('the keyless provider is offered and named', function () {
assert_contains('2 => "Frankfurter"', frankfurter_source('api/fixer/get_fixer.php'),
'the read API can name the provider it may now be asked about');
});
+
+/**
+ * Queues several answers in order, for the paths that ask more than once.
+ *
+ * @param array<int, array{0: string|false, 1: int|null}> $answers body, status
+ */
+function frankfurter_expect_sequence($answers)
+{
+ $GLOBALS['frankfurter_test_urls'] = [];
+ $GLOBALS['frankfurter_test_answers'] = [];
+
+ foreach ($answers as $answer) {
+ list($body, $status) = $answer;
+ $GLOBALS['frankfurter_test_answers'][] = [
+ 'body' => $body,
+ 'headers' => $status === null ? null : ['HTTP/1.1 ' . $status . ' Something'],
+ ];
+ }
+}
+
+wallos_test('a code the provider refuses is dropped, and the rest still refresh', function () {
+ // Measured 2026-09-13: a well-formed code the catalogue does not carry is
+ // not always dropped in silence. quotes=EUR,USD,BTC answers 200 without
+ // BTC, but quotes=EUR,USD,ETH answers 422 and takes EUR and USD with it.
+ // So an account holding one such currency got no rates at all rather than
+ // the ones the provider was perfectly willing to price.
+ frankfurter_expect_sequence([
+ ['{"status":422,"message":"invalid currency: ETH"}', 422],
+ ['[{"date":"2026-09-13","base":"CHF","quote":"EUR","rate":1.0593}]', 200],
+ ]);
+
+ $answer = frankfurter_latest_rates('CHF', 'EUR,ETH');
+
+ assert_same(2, count($GLOBALS['frankfurter_test_urls']),
+ 'the refusal cost exactly one retry, never a search');
+ assert_true(isset($answer['rates']['EUR']), 'the currency it does price came back');
+ assert_same(['ETH'], $answer['held'] ?? [],
+ 'and the answer names the one that kept the rate it had');
+
+ // The retry asks for what is left, and for nothing else.
+ assert_contains('quotes=EUR', $GLOBALS['frankfurter_test_urls'][1], 'the retry asks for EUR');
+ assert_true(strpos($GLOBALS['frankfurter_test_urls'][1], 'ETH') === false,
+ 'and does not ask again for the code that was refused');
+});
+
+wallos_test('every code refused means there is nothing left to retry with', function () {
+ frankfurter_expect_sequence([
+ ['{"status":422,"message":"invalid currency: ETH,XYZ"}', 422],
+ ]);
+
+ $answer = frankfurter_latest_rates('CHF', 'ETH,XYZ');
+
+ assert_same(1, count($GLOBALS['frankfurter_test_urls']), 'nothing was retried');
+ assert_true(!isset($answer['rates']), 'and the refusal is the answer');
+ assert_contains('ETH,XYZ', $answer['message'] ?? '', 'which still names the codes');
+});
+
+wallos_test('a refusal that names nothing is left standing', function () {
+ // The degradation path, and the reason the retry is safe to have: if the
+ // message ever stops carrying codes, nothing is dropped, nothing is asked
+ // again, and the refusal is reported exactly as it would have been.
+ frankfurter_expect_sequence([
+ ['{"status":422,"message":"something else entirely"}', 422],
+ ]);
+
+ $answer = frankfurter_latest_rates('CHF', 'EUR,USD');
+
+ assert_same(1, count($GLOBALS['frankfurter_test_urls']), 'nothing was retried');
+ assert_true(!isset($answer['rates']), 'and the refusal stands');
+});
+
+wallos_test('the refused codes are read out of the provider\'s own message', function () {
+ assert_same(['ETH'], frankfurter_refused_codes(
+ ['status' => 422, 'message' => 'invalid currency: ETH']), 'one code');
+ assert_same(['ETH', 'XYZ', 'QQQ'], frankfurter_refused_codes(
+ ['status' => 422, 'message' => 'invalid currency: ETH,XYZ,QQQ']), 'three, as measured');
+ assert_same(['ETH', 'XYZ'], frankfurter_refused_codes(
+ ['status' => 422, 'message' => 'invalid currency: eth, xyz']), 'case and spacing do not matter');
+
+ foreach ([
+ ['status' => 422, 'message' => 'something else entirely'],
+ ['status' => 422, 'message' => 'invalid currency: '],
+ ['status' => 422],
+ ['message' => 12],
+ [],
+ null,
+ 'not an array',
+ ] as $unusable) {
+ assert_same([], frankfurter_refused_codes($unusable),
+ 'an answer naming no code drops nothing: ' . var_export($unusable, true));
+ }
+
+ // A currency in Wallos is three free-text fields, so a message naming
+ // something that is not a code must not cost somebody a currency.
+ assert_same([], frankfurter_refused_codes(
+ ['message' => 'invalid currency: Lunarium']), 'only three-letter codes are acted on');
+});
+
+wallos_test('both endpoints that report a refresh name what was not priced', function () {
+ foreach ([
+ 'endpoints/currency/update_exchange.php',
+ 'endpoints/cronjobs/updateexchange.php',
+ ] as $path) {
+ $source = frankfurter_source($path);
+
+ assert_contains("\$apiData['held']", $source,
+ $path . ' reads the codes the provider would not price');
+ assert_contains('left unchanged', $source,
+ $path . ' says what happened to them');
+ }
+});