Skip to content

Commit

Permalink
feat(fal_client): better client errors
Browse files Browse the repository at this point in the history
  • Loading branch information
efiop committed Sep 24, 2024
1 parent 6f7e239 commit 2497066
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions projects/fal_client/src/fal_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@
USER_AGENT = "fal-client/0.2.2 (python)"


class FalClientError(Exception):
pass


def _raise_for_status(response: httpx.Response) -> None:
try:
response.raise_for_status()
except httpx.HTTPStatusError as exc:
try:
msg = response.json()["detail"]
except (ValueError, KeyError):
msg = response.text

raise FalClientError(msg) from exc


@dataclass
class Status: ...

Expand Down Expand Up @@ -94,7 +110,7 @@ def status(self, *, with_logs: bool = False) -> Status:
"logs": with_logs,
},
)
response.raise_for_status()
_raise_for_status(response)

return self._parse_status(response.json())

Expand All @@ -119,7 +135,7 @@ def get(self) -> AnyJSON:
continue

response = self.client.get(self.response_url)
response.raise_for_status()
_raise_for_status(response)
return response.json()


Expand All @@ -138,7 +154,7 @@ async def status(self, *, with_logs: bool = False) -> Status:
"logs": with_logs,
},
)
response.raise_for_status()
_raise_for_status(response)

return self._parse_status(response.json())

Expand All @@ -163,7 +179,7 @@ async def get(self) -> AnyJSON:
continue

response = await self.client.get(self.response_url)
response.raise_for_status()
_raise_for_status(response)
return response.json()


Expand Down Expand Up @@ -214,7 +230,7 @@ async def run(
timeout=timeout,
headers=headers,
)
response.raise_for_status()
_raise_for_status(response)
return response.json()

async def submit(
Expand Down Expand Up @@ -242,7 +258,7 @@ async def submit(
json=arguments,
timeout=self.default_timeout,
)
response.raise_for_status()
_raise_for_status(response)

data = response.json()
return AsyncRequestHandle(
Expand Down Expand Up @@ -291,7 +307,7 @@ async def upload(self, data: str | bytes, content_type: str) -> str:
data=data,
headers={"Content-Type": content_type},
)
response.raise_for_status()
_raise_for_status(response)

return response.json()["access_url"]

Expand Down Expand Up @@ -359,7 +375,7 @@ def run(
timeout=timeout,
headers=headers,
)
response.raise_for_status()
_raise_for_status(response)
return response.json()

def submit(
Expand Down Expand Up @@ -388,7 +404,7 @@ def submit(
timeout=self.default_timeout,
headers=headers,
)
response.raise_for_status()
_raise_for_status(response)

data = response.json()
return SyncRequestHandle(
Expand Down Expand Up @@ -433,7 +449,7 @@ def upload(self, data: str | bytes, content_type: str) -> str:
data=data,
headers={"Content-Type": content_type},
)
response.raise_for_status()
_raise_for_status(response)

return response.json()["access_url"]

Expand Down

0 comments on commit 2497066

Please sign in to comment.