From 2855e35313715cb2f0a49e46edff41e6e1f27fc1 Mon Sep 17 00:00:00 2001
From: Michal Zander <mzander@syncad.com>
Date: Tue, 27 Aug 2024 13:21:35 +0000
Subject: [PATCH] Catch null results in json-rpc apis

---
 postgrest/hafah_endpoints.sql | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/postgrest/hafah_endpoints.sql b/postgrest/hafah_endpoints.sql
index 7a7dcff9f..f6d5a0ac1 100644
--- a/postgrest/hafah_endpoints.sql
+++ b/postgrest/hafah_endpoints.sql
@@ -42,7 +42,7 @@ DECLARE
   __params JSON;
   __id JSON;
 
-  __result JSON;
+  __result JSONB;
   __api_type TEXT;
   __method_type TEXT;
   __is_legacy_style BOOLEAN;
@@ -53,7 +53,7 @@ BEGIN
   __params = (__request_data->'params');
   __id = (__request_data->'id');
 
-  SELECT NULL::JSON INTO __result;
+  SELECT NULL::JSONB INTO __result;
 
   IF __jsonrpc != '2.0' OR __jsonrpc IS NULL OR __params IS NULL OR __id IS NULL THEN
     RETURN hafah_backend.raise_exception(-32600, 'Invalid JSON-RPC');
@@ -90,6 +90,11 @@ BEGIN
     END IF;
   END IF;
 
+  IF __result = 'null' THEN
+    RAISE NOTICE 'null in % api', __method;
+    __result := '[]'::JSONB;
+  END IF;
+
   IF __result IS NULL THEN
     RETURN hafah_backend.raise_exception(-32601, 'Method not found', __method, __id);
   ELSEIF __result->'error' IS NULL THEN
-- 
GitLab