diff --git a/python/restate/__init__.py b/python/restate/__init__.py index 3b32d65..84173d5 100644 --- a/python/restate/__init__.py +++ b/python/restate/__init__.py @@ -26,7 +26,7 @@ from .context import Context, ObjectContext, ObjectSharedContext from .context import WorkflowContext, WorkflowSharedContext from .retry_policy import InvocationRetryPolicy -from .client_types import RestateClient, RestateClientSendHandle +from .client_types import RestateClient, HttpError, RestateClientSendHandle # pylint: disable=line-too-long from .context import ( @@ -119,5 +119,6 @@ async def create_client( "getLogger", "RestateClient", "RestateClientSendHandle", + "HttpError", "create_client", ] diff --git a/python/restate/client.py b/python/restate/client.py index d674961..2e8eda5 100644 --- a/python/restate/client.py +++ b/python/restate/client.py @@ -17,7 +17,7 @@ import typing from contextlib import asynccontextmanager -from .client_types import RestateClient, RestateClientSendHandle +from .client_types import RestateClient, RestateClientSendHandle, HttpError from .context import HandlerType from .serde import BytesSerde, JsonSerde, Serde @@ -142,7 +142,8 @@ async def post( if idempotency_key is not None: dict_headers["Idempotency-Key"] = idempotency_key res = await self.client.post(endpoint, headers=dict_headers, content=content) - res.raise_for_status() + if res.status_code >= 400: + raise HttpError(res.status_code, res.reason_phrase, res.text) return res.content @typing.final diff --git a/python/restate/client_types.py b/python/restate/client_types.py index b8d5121..8d1bb23 100644 --- a/python/restate/client_types.py +++ b/python/restate/client_types.py @@ -34,6 +34,18 @@ def __init__(self, invocation_id: str, status_code: int): self.status_code = status_code +class HttpError(Exception): + """ + An error that occurs during an HTTP request. + """ + + def __init__(self, status_code: int, message: str, body: str | None = None): + super().__init__(f"HTTP {status_code}: {message}") + self.status_code = status_code + self.message = message + self.body = body + + class RestateClient(abc.ABC): """ An abstract base class for a Restate client.