Skip to content

Commit

Permalink
Start defining custom errors in one place (#2122)
Browse files Browse the repository at this point in the history
* Moved InferenceTimeoutError

* Moved text generation errors

---------

Co-authored-by: Lucain <lucainp@gmail.com>
  • Loading branch information
Y4suyuki and Wauplin authored Mar 18, 2024
1 parent 965f708 commit 7da60b8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 33 deletions.
38 changes: 38 additions & 0 deletions src/huggingface_hub/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Contains all custom errors."""

from requests import HTTPError


# INFERENCE CLIENT ERRORS


class InferenceTimeoutError(HTTPError, TimeoutError):
"""Error raised when a model is unavailable or the request times out."""


# TEXT GENERATION ERRORS


class TextGenerationError(HTTPError):
"""Generic error raised if text-generation went wrong."""


# Text Generation Inference Errors
class ValidationError(TextGenerationError):
"""Server-side validation error."""


class GenerationError(TextGenerationError):
pass


class OverloadedError(TextGenerationError):
pass


class IncompleteGenerationError(TextGenerationError):
pass


class UnknownError(TextGenerationError):
pass
2 changes: 1 addition & 1 deletion src/huggingface_hub/inference/_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@
from requests.structures import CaseInsensitiveDict

from huggingface_hub.constants import ALL_INFERENCE_API_FRAMEWORKS, INFERENCE_ENDPOINT, MAIN_INFERENCE_API_FRAMEWORKS
from huggingface_hub.errors import InferenceTimeoutError
from huggingface_hub.inference._common import (
TASKS_EXPECTING_IMAGES,
ContentT,
InferenceTimeoutError,
ModelStatus,
_b64_encode,
_b64_to_image,
Expand Down
6 changes: 0 additions & 6 deletions src/huggingface_hub/inference/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
overload,
)

from requests import HTTPError

from ..constants import ENDPOINT
from ..utils import (
build_hf_headers,
Expand Down Expand Up @@ -99,10 +97,6 @@ class ModelStatus:
framework: str


class InferenceTimeoutError(HTTPError, TimeoutError):
"""Error raised when a model is unavailable or the request times out."""


## IMPORT UTILS


Expand Down
2 changes: 1 addition & 1 deletion src/huggingface_hub/inference/_generated/_async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
from requests.structures import CaseInsensitiveDict

from huggingface_hub.constants import ALL_INFERENCE_API_FRAMEWORKS, INFERENCE_ENDPOINT, MAIN_INFERENCE_API_FRAMEWORKS
from huggingface_hub.errors import InferenceTimeoutError
from huggingface_hub.inference._common import (
TASKS_EXPECTING_IMAGES,
ContentT,
InferenceTimeoutError,
ModelStatus,
_async_stream_text_generation_response,
_b64_encode,
Expand Down
34 changes: 9 additions & 25 deletions src/huggingface_hub/inference/_text_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@

from requests import HTTPError

from huggingface_hub.errors import (
GenerationError,
IncompleteGenerationError,
OverloadedError,
TextGenerationError,
UnknownError,
ValidationError,
)

from ..utils import is_pydantic_available


Expand Down Expand Up @@ -487,31 +496,6 @@ def __post_init__(self):
# ----------------------


class TextGenerationError(HTTPError):
"""Generic error raised if text-generation went wrong."""


# Text Generation Inference Errors
class ValidationError(TextGenerationError):
"""Server-side validation error."""


class GenerationError(TextGenerationError):
pass


class OverloadedError(TextGenerationError):
pass


class IncompleteGenerationError(TextGenerationError):
pass


class UnknownError(TextGenerationError):
pass


def raise_text_generation_error(http_error: HTTPError) -> NoReturn:
"""
Try to parse text-generation-inference error message and raise HTTPError in any case.
Expand Down

0 comments on commit 7da60b8

Please sign in to comment.