Skip to content
This repository was archived by the owner on Sep 8, 2025. It is now read-only.

Commit 72b09a0

Browse files
committed
chore(format): fix formatting of code
1 parent 0bbb2f0 commit 72b09a0

File tree

5 files changed

+50
-23
lines changed

5 files changed

+50
-23
lines changed

postgrest/_async/request_builder.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from json import JSONDecodeError
43
from typing import Any, Generic, Optional, TypeVar, Union
54

65
from httpx import Headers, QueryParams

postgrest/_sync/request_builder.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import annotations
22

3-
from json import JSONDecodeError
43
from typing import Any, Generic, Optional, TypeVar, Union
54

65
from httpx import Headers, QueryParams
@@ -19,7 +18,7 @@
1918
pre_update,
2019
pre_upsert,
2120
)
22-
from ..exceptions import APIError, generate_default_error_message
21+
from ..exceptions import APIError, APIErrorFromJSON, generate_default_error_message
2322
from ..types import ReturnMethod
2423
from ..utils import SyncClient, get_origin_and_cast
2524

@@ -75,10 +74,9 @@ def execute(self) -> APIResponse[_ReturnT]:
7574
return body
7675
return APIResponse[_ReturnT].from_http_request_response(r)
7776
else:
78-
raise APIError(r.json())
77+
json_obj = APIErrorFromJSON.model_validate_json(r.content)
78+
raise APIError(dict(json_obj))
7979
except ValidationError as e:
80-
raise APIError(r.json()) from e
81-
except JSONDecodeError:
8280
raise APIError(generate_default_error_message(r))
8381

8482

@@ -124,10 +122,9 @@ def execute(self) -> SingleAPIResponse[_ReturnT]:
124122
): # Response.ok from JS (https://developer.mozilla.org/en-US/docs/Web/API/Response/ok)
125123
return SingleAPIResponse[_ReturnT].from_http_request_response(r)
126124
else:
127-
raise APIError(r.json())
125+
json_obj = APIErrorFromJSON.model_validate_json(r.content)
126+
raise APIError(dict(json_obj))
128127
except ValidationError as e:
129-
raise APIError(r.json()) from e
130-
except JSONDecodeError:
131128
raise APIError(generate_default_error_message(r))
132129

133130

@@ -290,7 +287,7 @@ def select(
290287
*columns: The names of the columns to fetch.
291288
count: The method to use to get the count of rows returned.
292289
Returns:
293-
:class:`SyncSelectRequestBuilder`
290+
:class:`AsyncSelectRequestBuilder`
294291
"""
295292
method, params, headers, json = pre_select(*columns, count=count, head=head)
296293
return SyncSelectRequestBuilder[_ReturnT](
@@ -317,7 +314,7 @@ def insert(
317314
Otherwise, use the default value for the column.
318315
Only applies for bulk inserts.
319316
Returns:
320-
:class:`SyncQueryRequestBuilder`
317+
:class:`AsyncQueryRequestBuilder`
321318
"""
322319
method, params, headers, json = pre_insert(
323320
json,
@@ -353,7 +350,7 @@ def upsert(
353350
not when merging with existing rows under `ignoreDuplicates: false`.
354351
This also only applies when doing bulk upserts.
355352
Returns:
356-
:class:`SyncQueryRequestBuilder`
353+
:class:`AsyncQueryRequestBuilder`
357354
"""
358355
method, params, headers, json = pre_upsert(
359356
json,
@@ -381,7 +378,7 @@ def update(
381378
count: The method to use to get the count of rows returned.
382379
returning: Either 'minimal' or 'representation'
383380
Returns:
384-
:class:`SyncFilterRequestBuilder`
381+
:class:`AsyncFilterRequestBuilder`
385382
"""
386383
method, params, headers, json = pre_update(
387384
json,
@@ -404,7 +401,7 @@ def delete(
404401
count: The method to use to get the count of rows returned.
405402
returning: Either 'minimal' or 'representation'
406403
Returns:
407-
:class:`SyncFilterRequestBuilder`
404+
:class:`AsyncFilterRequestBuilder`
408405
"""
409406
method, params, headers, json = pre_delete(
410407
count=count,

postgrest/exceptions.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import Dict, Optional
2+
23
from pydantic import BaseModel
34

45

@@ -7,6 +8,7 @@ class APIErrorFromJSON(BaseModel):
78
A pydantic object to validate an error info object
89
from a json string.
910
"""
11+
1012
message: Optional[str]
1113
"""The error message."""
1214
code: Optional[str]
@@ -16,6 +18,7 @@ class APIErrorFromJSON(BaseModel):
1618
details: Optional[str]
1719
"""The error details."""
1820

21+
1922
class APIError(Exception):
2023
"""
2124
Base exception for all API errors.

tests/_async/test_client.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest.mock import patch
22

33
import pytest
4-
from httpx import BasicAuth, Headers, Response, Request
4+
from httpx import BasicAuth, Headers, Request, Response
55

66
from postgrest import AsyncPostgrestClient
77
from postgrest.exceptions import APIError
@@ -107,6 +107,7 @@ async def test_response_status_code_outside_ok(postgrest_client: AsyncPostgrestC
107107
)
108108
assert exc_response["errors"][0].get("code") == 400
109109

110+
110111
@pytest.mark.asyncio
111112
async def test_response_maybe_single(postgrest_client: AsyncPostgrestClient):
112113
with patch(
@@ -127,20 +128,21 @@ async def test_response_maybe_single(postgrest_client: AsyncPostgrestClient):
127128
assert isinstance(exc_response.get("message"), str)
128129
assert "code" in exc_response and int(exc_response["code"]) == 204
129130

131+
130132
# https://github.com/supabase/postgrest-py/issues/595
131133
@pytest.mark.asyncio
132-
async def test_response_client_invalid_response_but_valid_json(postgrest_client: AsyncPostgrestClient):
134+
async def test_response_client_invalid_response_but_valid_json(
135+
postgrest_client: AsyncPostgrestClient,
136+
):
133137
with patch(
134138
"httpx._client.AsyncClient.request",
135139
return_value=Response(
136140
status_code=502,
137-
text='"gateway error: Error: Network connection lost."', # quotes makes this text a valid non-dict JSON object
138-
request=Request(method="GET", url="http://example.com")
139-
)
141+
text='"gateway error: Error: Network connection lost."', # quotes makes this text a valid non-dict JSON object
142+
request=Request(method="GET", url="http://example.com"),
143+
),
140144
):
141-
client = (
142-
postgrest_client.from_("test").select("a", "b").eq("c", "d").single()
143-
)
145+
client = postgrest_client.from_("test").select("a", "b").eq("c", "d").single()
144146
assert "Accept" in client.headers
145147
assert client.headers.get("Accept") == "application/vnd.pgrst.object+json"
146148
with pytest.raises(APIError) as exc_info:

tests/_sync/test_client.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from unittest.mock import patch
22

33
import pytest
4-
from httpx import BasicAuth, Headers
4+
from httpx import BasicAuth, Headers, Request, Response
55

66
from postgrest import SyncPostgrestClient
77
from postgrest.exceptions import APIError
@@ -123,3 +123,29 @@ def test_response_maybe_single(postgrest_client: SyncPostgrestClient):
123123
exc_response = exc_info.value.json()
124124
assert isinstance(exc_response.get("message"), str)
125125
assert "code" in exc_response and int(exc_response["code"]) == 204
126+
127+
128+
# https://github.com/supabase/postgrest-py/issues/595
129+
130+
131+
def test_response_client_invalid_response_but_valid_json(
132+
postgrest_client: SyncPostgrestClient,
133+
):
134+
with patch(
135+
"httpx._client.SyncClient.request",
136+
return_value=Response(
137+
status_code=502,
138+
text='"gateway error: Error: Network connection lost."', # quotes makes this text a valid non-dict JSON object
139+
request=Request(method="GET", url="http://example.com"),
140+
),
141+
):
142+
client = postgrest_client.from_("test").select("a", "b").eq("c", "d").single()
143+
assert "Accept" in client.headers
144+
assert client.headers.get("Accept") == "application/vnd.pgrst.object+json"
145+
with pytest.raises(APIError) as exc_info:
146+
client.execute()
147+
assert isinstance(exc_info, pytest.ExceptionInfo)
148+
exc_response = exc_info.value.json()
149+
assert isinstance(exc_response.get("message"), str)
150+
assert exc_response.get("message") == "JSON could not be generated"
151+
assert "code" in exc_response and int(exc_response["code"]) == 502

0 commit comments

Comments
 (0)