Skip to content

Commit 9ef2314

Browse files
committed
Raise a named error to allow clients to handle failed jobs
1 parent eed2943 commit 9ef2314

File tree

3 files changed

+46
-10
lines changed

3 files changed

+46
-10
lines changed

src/gradient/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
PermissionDeniedError,
3636
UnprocessableEntityError,
3737
APIResponseValidationError,
38+
IndexingJobError,
3839
)
3940
from ._base_client import DefaultHttpxClient, DefaultAioHttpClient, DefaultAsyncHttpxClient
4041
from ._utils._logs import setup_logging as _setup_logging
@@ -65,6 +66,7 @@
6566
"UnprocessableEntityError",
6667
"RateLimitError",
6768
"InternalServerError",
69+
"IndexingJobError",
6870
"Timeout",
6971
"RequestOptions",
7072
"Client",

src/gradient/_exceptions.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"UnprocessableEntityError",
1616
"RateLimitError",
1717
"InternalServerError",
18+
"IndexingJobError",
1819
]
1920

2021

@@ -106,3 +107,15 @@ class RateLimitError(APIStatusError):
106107

107108
class InternalServerError(APIStatusError):
108109
pass
110+
111+
112+
class IndexingJobError(GradientError):
113+
"""Raised when an indexing job fails, encounters an error, or is cancelled."""
114+
115+
uuid: str
116+
phase: str
117+
118+
def __init__(self, message: str, *, uuid: str, phase: str) -> None:
119+
super().__init__(message)
120+
self.uuid = uuid
121+
self.phase = phase

src/gradient/resources/knowledge_bases/indexing_jobs.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import httpx
88

99
from ..._types import Body, Omit, Query, Headers, NotGiven, SequenceNotStr, omit, not_given
10+
from ..._exceptions import IndexingJobError
1011
from ..._utils import maybe_transform, async_maybe_transform
1112
from ..._compat import cached_property
1213
from ..._resource import SyncAPIResource, AsyncAPIResource
@@ -301,7 +302,7 @@ def wait_for_completion(
301302
302303
Raises:
303304
TimeoutError: If the job doesn't complete within the specified timeout.
304-
RuntimeError: If the job fails, errors, or is cancelled.
305+
IndexingJobError: If the job fails, errors, or is cancelled.
305306
"""
306307
if not uuid:
307308
raise ValueError(f"Expected a non-empty value for `uuid` but received {uuid!r}")
@@ -327,17 +328,27 @@ def wait_for_completion(
327328

328329
# Failure states
329330
if phase == "BATCH_JOB_PHASE_FAILED":
330-
raise RuntimeError(
331+
raise IndexingJobError(
331332
f"Indexing job {uuid} failed. "
332333
f"Total items indexed: {response.job.total_items_indexed}, "
333-
f"Total items failed: {response.job.total_items_failed}"
334+
f"Total items failed: {response.job.total_items_failed}",
335+
uuid=uuid,
336+
phase=phase,
334337
)
335338

336339
if phase == "BATCH_JOB_PHASE_ERROR":
337-
raise RuntimeError(f"Indexing job {uuid} encountered an error")
340+
raise IndexingJobError(
341+
f"Indexing job {uuid} encountered an error",
342+
uuid=uuid,
343+
phase=phase,
344+
)
338345

339346
if phase == "BATCH_JOB_PHASE_CANCELLED":
340-
raise RuntimeError(f"Indexing job {uuid} was cancelled")
347+
raise IndexingJobError(
348+
f"Indexing job {uuid} was cancelled",
349+
uuid=uuid,
350+
phase=phase,
351+
)
341352

342353
# Still in progress (UNKNOWN, PENDING, or RUNNING)
343354
# Check timeout
@@ -623,7 +634,7 @@ async def wait_for_completion(
623634
624635
Raises:
625636
TimeoutError: If the job doesn't complete within the specified timeout.
626-
RuntimeError: If the job fails, errors, or is cancelled.
637+
IndexingJobError: If the job fails, errors, or is cancelled.
627638
"""
628639
if not uuid:
629640
raise ValueError(f"Expected a non-empty value for `uuid` but received {uuid!r}")
@@ -649,17 +660,27 @@ async def wait_for_completion(
649660

650661
# Failure states
651662
if phase == "BATCH_JOB_PHASE_FAILED":
652-
raise RuntimeError(
663+
raise IndexingJobError(
653664
f"Indexing job {uuid} failed. "
654665
f"Total items indexed: {response.job.total_items_indexed}, "
655-
f"Total items failed: {response.job.total_items_failed}"
666+
f"Total items failed: {response.job.total_items_failed}",
667+
uuid=uuid,
668+
phase=phase,
656669
)
657670

658671
if phase == "BATCH_JOB_PHASE_ERROR":
659-
raise RuntimeError(f"Indexing job {uuid} encountered an error")
672+
raise IndexingJobError(
673+
f"Indexing job {uuid} encountered an error",
674+
uuid=uuid,
675+
phase=phase,
676+
)
660677

661678
if phase == "BATCH_JOB_PHASE_CANCELLED":
662-
raise RuntimeError(f"Indexing job {uuid} was cancelled")
679+
raise IndexingJobError(
680+
f"Indexing job {uuid} was cancelled",
681+
uuid=uuid,
682+
phase=phase,
683+
)
663684

664685
# Still in progress (UNKNOWN, PENDING, or RUNNING)
665686
# Check timeout

0 commit comments

Comments
 (0)