Skip to content

Commit 7077a38

Browse files
authored
fix: custom error status code from edge function returned correctly (#196)
1 parent f935dd1 commit 7077a38

File tree

3 files changed

+16
-6
lines changed

3 files changed

+16
-6
lines changed

supabase_functions/_async/functions_client.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ async def _request(
5454
try:
5555
response.raise_for_status()
5656
except HTTPError as exc:
57+
status_code = None
58+
if hasattr(response, "status_code"):
59+
status_code = response.status_code
60+
5761
raise FunctionsHttpError(
5862
response.json().get("error")
59-
or f"An error occurred while requesting your edge function at {exc.request.url!r}."
63+
or f"An error occurred while requesting your edge function at {exc.request.url!r}.",
64+
status_code,
6065
) from exc
6166

6267
return response

supabase_functions/_sync/functions_client.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,14 @@ def _request(
5454
try:
5555
response.raise_for_status()
5656
except HTTPError as exc:
57+
status_code = None
58+
if hasattr(response, "status_code"):
59+
status_code = response.status_code
60+
5761
raise FunctionsHttpError(
5862
response.json().get("error")
59-
or f"An error occurred while requesting your edge function at {exc.request.url!r}."
63+
or f"An error occurred while requesting your edge function at {exc.request.url!r}.",
64+
status_code,
6065
) from exc
6166

6267
return response

supabase_functions/errors.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ def to_dict(self) -> FunctionsApiErrorDict:
2525

2626

2727
class FunctionsHttpError(FunctionsError):
28-
def __init__(self, message: str) -> None:
28+
def __init__(self, message: str, code: int | None = None) -> None:
2929
super().__init__(
3030
message,
3131
"FunctionsHttpError",
32-
400,
32+
400 if code is None else code,
3333
)
3434

3535

3636
class FunctionsRelayError(FunctionsError):
3737
"""Base exception for relay errors."""
3838

39-
def __init__(self, message: str) -> None:
39+
def __init__(self, message: str, code: int | None = None) -> None:
4040
super().__init__(
4141
message,
4242
"FunctionsRelayError",
43-
400,
43+
400 if code is None else code,
4444
)

0 commit comments

Comments
 (0)