Commit 790663e2 authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix(api): an RPC that returns a number no longer crashes the request

award_coins answers the new balance; json_decode gave an int and
SupabaseClient::request(), typed to return an array, threw a TypeError.
Every achievement that paid coins answered 500 after the coins were
already granted (seen in production on achievements.php). Scalars now
come back as ['result' => value]; error bodies that are not objects no
longer index into a scalar.
Co-Authored-By: 's avatarClaude Opus 5.5 <noreply@anthropic.com>
parent fee8349c
...@@ -109,8 +109,13 @@ class SupabaseClient { ...@@ -109,8 +109,13 @@ class SupabaseClient {
$decoded = json_decode($response, true); $decoded = json_decode($response, true);
if ($httpCode >= 400) { if ($httpCode >= 400) {
return ['error' => $decoded['message'] ?? $decoded['error'] ?? 'Request failed', 'code' => $httpCode]; $msg = is_array($decoded) ? ($decoded['message'] ?? $decoded['error'] ?? null) : null;
return ['error' => $msg ?? 'Request failed', 'code' => $httpCode];
} }
// An RPC that returns a scalar (award_coins answers the new balance)
// decodes to an int or a string; the return type is an array, so a bare
// value threw a TypeError and the whole request answered 500.
if ($decoded !== null && !is_array($decoded)) return ['result' => $decoded];
return $decoded ?? []; return $decoded ?? [];
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment