Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
refactor(browser-sessions, agent-sponsor): replace error code checks with utility function for handling PostgREST no rows error
Erik Engervall02efaa627a28
diff --git a/apps/api/src/lib/browser-sessions.ts b/apps/api/src/lib/browser-sessions.tsindex 2b7b9da36..1c6d8ea3f 100644--- a/apps/api/src/lib/browser-sessions.ts+++ b/apps/api/src/lib/browser-sessions.ts@@ -1,5 +1,5 @@-import { supabase_service } from "../services/supabase";-import { getValue, setValue, deleteKey } from "../services/redis";+import { deleteKey, getValue, setValue } from "../services/redis";+import { isPostgrestNoRowsError, supabase_service } from "../services/supabase";import { logger as _logger } from "./logger";const logger = _logger.child({ module: "browser-sessions" });@@ -71,8 +71,7 @@ export async function getBrowserSession(.single();if (error) {- // PGRST116 = no rows found- if (error.code === "PGRST116") return null;+ if (isPostgrestNoRowsError(error)) return null;logger.error("Failed to get browser session", { error, id });throw new Error(`Failed to get browser session: ${error.message}`);}@@ -125,7 +124,7 @@ export async function getBrowserSessionByBrowserId(.single();if (error) {- if (error.code === "PGRST116") return null;+ if (isPostgrestNoRowsError(error)) return null;logger.error("Failed to get browser session by browser_id", {error,browserId,diff --git a/apps/api/src/services/agent-sponsor.ts b/apps/api/src/services/agent-sponsor.tsindex 96f447675..8480b4843 100644--- a/apps/api/src/services/agent-sponsor.ts+++ b/apps/api/src/services/agent-sponsor.ts@@ -1,6 +1,10 @@import { logger } from "../lib/logger";import { deleteKey, getValue, setValue } from "./redis";-import { supabase_rr_service, supabase_service } from "./supabase";+import {+ isPostgrestNoRowsError,+ supabase_rr_service,+ supabase_service,+} from "./supabase";type AgentSponsorStatus = {status: "pending" | "verified" | "blocked";@@ -44,13 +48,24 @@ export async function getAgentSponsorStatus({.eq("api_key_id", apiKeyId).single();- if (error || !data) {- // Cache the miss to avoid repeated queries- await setValue(- cacheKey,- JSON.stringify(CACHE_MISS_SENTINEL),- AGENT_SPONSOR_CACHE_TTL,- );+ if (error) {+ // Only cache "no sponsor" when it's a confirmed no-rows result.+ // Do not cache on other errors (e.g. connection/timeout) so we retry on next request.+ if (isPostgrestNoRowsError(error)) {+ await setValue(+ cacheKey,+ JSON.stringify(CACHE_MISS_SENTINEL),+ AGENT_SPONSOR_CACHE_TTL,+ );+ } else {+ logger.error("Failed to look up agent sponsor status", {+ apiKeyId,+ error,+ });+ }+ return null;+ }+ if (!data) {return null;}diff --git a/apps/api/src/services/supabase.ts b/apps/api/src/services/supabase.tsindex 71dc4991d..d6a21229f 100644--- a/apps/api/src/services/supabase.ts+++ b/apps/api/src/services/supabase.ts@@ -1,9 +1,18 @@import { createClient, SupabaseClient } from "@supabase/supabase-js";+import { configDotenv } from "dotenv";import { config } from "../config";import { logger } from "../lib/logger";-import { configDotenv } from "dotenv";configDotenv();+/** PostgREST code when .single() returns 0 or >1 rows. Use to distinguish "no row" from real DB errors. */+export const POSTGREST_NO_ROWS_CODE = "PGRST116";++export function isPostgrestNoRowsError(+ error: { code?: string } | null | undefined,+): boolean {+ return error?.code === POSTGREST_NO_ROWS_CODE;+}+// SupabaseService class initializes the Supabase client conditionally based on environment variables.class SupabaseService {private client: SupabaseClient | null = null;