From 708f703049778b6b2041cf8d595f835eae0be6af Mon Sep 17 00:00:00 2001 From: Andrew Smith Date: Tue, 25 Mar 2025 07:18:16 +0000 Subject: [PATCH] fix: status code from edge function returned correctly --- supabase_functions/_async/functions_client.py | 7 ++++++- supabase_functions/_sync/functions_client.py | 7 ++++++- supabase_functions/errors.py | 8 ++++---- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/supabase_functions/_async/functions_client.py b/supabase_functions/_async/functions_client.py index 42d8d20..ba12dbb 100644 --- a/supabase_functions/_async/functions_client.py +++ b/supabase_functions/_async/functions_client.py @@ -54,9 +54,14 @@ async def _request( try: response.raise_for_status() except HTTPError as exc: + status_code = None + if hasattr(response, "status_code"): + status_code = response.status_code + raise FunctionsHttpError( response.json().get("error") - or f"An error occurred while requesting your edge function at {exc.request.url!r}." + or f"An error occurred while requesting your edge function at {exc.request.url!r}.", + status_code, ) from exc return response diff --git a/supabase_functions/_sync/functions_client.py b/supabase_functions/_sync/functions_client.py index 99780eb..d8a410f 100644 --- a/supabase_functions/_sync/functions_client.py +++ b/supabase_functions/_sync/functions_client.py @@ -54,9 +54,14 @@ def _request( try: response.raise_for_status() except HTTPError as exc: + status_code = None + if hasattr(response, "status_code"): + status_code = response.status_code + raise FunctionsHttpError( response.json().get("error") - or f"An error occurred while requesting your edge function at {exc.request.url!r}." + or f"An error occurred while requesting your edge function at {exc.request.url!r}.", + status_code, ) from exc return response diff --git a/supabase_functions/errors.py b/supabase_functions/errors.py index ced5f31..0529e34 100644 --- a/supabase_functions/errors.py +++ b/supabase_functions/errors.py @@ -25,20 +25,20 @@ def to_dict(self) -> FunctionsApiErrorDict: class FunctionsHttpError(FunctionsError): - def __init__(self, message: str) -> None: + def __init__(self, message: str, code: int | None = None) -> None: super().__init__( message, "FunctionsHttpError", - 400, + 400 if code is None else code, ) class FunctionsRelayError(FunctionsError): """Base exception for relay errors.""" - def __init__(self, message: str) -> None: + def __init__(self, message: str, code: int | None = None) -> None: super().__init__( message, "FunctionsRelayError", - 400, + 400 if code is None else code, )