─░├žeri─če ge├ž

Referans i├žin hub_sdk/helpers/error_handler.py

Not

Bu dosya https://github.com/ultralytics/hub-sdk/blob/main/hub_sdk/helpers/error_handler .py adresinde mevcuttur. Bir sorun tespit ederseniz, l├╝tfen bir ├çekme ─░ste─či ­čŤá´ŞĆ ile katk─▒da bulunarak d├╝zeltmeye yard─▒mc─▒ olun. Te┼čekk├╝rler ­čÖĆ!



hub_sdk.helpers.error_handler.ErrorHandler

HTTP durum kodlar─▒n─▒ ve hata mesajlar─▒n─▒ y├Ânetmek i├žin bir hata i┼čleyicisini temsil eder.

Nitelikler:

─░sim Tip A├ž─▒klama
status_code int

Hata ile ili┼čkili HTTP durum kodu.

message (str, None)

Ek ayr─▒nt─▒lar sa─člayan iste─če ba─čl─▒ bir hata mesaj─▒. Varsay─▒lan de─čer Yok'tur.

headers (dict, None)

Yan─▒t ba┼čl─▒klar─▒n─▒n ayr─▒nt─▒lar─▒n─▒ sa─člayan iste─če ba─čl─▒ bir s├Âzl├╝k. Varsay─▒lan de─čer Yok'tur.

Kaynak kodu hub_sdk/helpers/error_handler.py
class ErrorHandler:
    """
    Represents an error handler for managing HTTP status codes and error messages.

    Attributes:
        status_code (int): The HTTP status code associated with the error.
        message (str, None): An optional error message providing additional details.
            Defaults to None.
        headers (dict, None): An optional dictionary providing response headers details.
            Defaults to None.
    """

    def __init__(
        self,
        status_code: int,
        message: Optional[str] = None,
        headers: Optional[dict] = None,
    ):
        """
        Initialize the ErrorHandler object with a given status code.

        Args:
            status_code (int): The HTTP status code representing the error.
            message (str, optional): An optional error message providing additional details.
            headers (dict, None): An optional dictionary providing response headers details.
        """
        self.status_code = status_code
        self.message = message
        self.headers = headers

    def handle(self) -> str:
        """
        Handle the error based on the provided status code.

        Returns:
            (str): A message describing the error.
        """
        error_handlers = {
            401: self.handle_unauthorized,
            404: self.handle_not_found,
            500: self.handle_internal_server_error,
            429: self.handle_ratelimit_exceeded,
        }

        handler = error_handlers.get(self.status_code, self.get_default_message)
        return handler()

    @staticmethod
    def handle_unauthorized() -> str:
        """
        Handle an unauthorized error (HTTP 401).

        Returns:
            (str): An error message indicating unauthorized access.
        """
        return "Unauthorized: Please check your credentials."

    def handle_ratelimit_exceeded(self) -> str:
        """
        Handle rate limit exceeded error (HTTP 429).

        Returns:
            (str): An error message indicating rate limit exceeded.
        """
        error_msg = "Rate Limits Exceeded: Please try again later."

        if "X-RateLimit-Reset" in self.headers:
            rate_reset = self.headers.get("X-RateLimit-Reset")

            try:
                reset_time = datetime.datetime.fromtimestamp(int(rate_reset)).strftime("%Y-%m-%d %H:%M:%S")
            except ValueError:
                reset_time = "unknown"

            error_msg = (
                "You have exceeded the rate limits for this request. "
                f"You will be able to make requests again after {reset_time}."
            )
        return error_msg

    @staticmethod
    def handle_not_found() -> str:
        """
        Handle a resource not found error (HTTP 404).

        Returns:
            (str): An error message indicating that the requested resource was not found.
        """
        return "Resource not found."

    @staticmethod
    def handle_internal_server_error() -> str:
        """
        Handle an internal server error (HTTP 500).

        Returns:
            (str): An error message indicating an internal server error.
        """
        return "Internal server error."

    @staticmethod
    def handle_unknown_error() -> str:
        """
        Handle an unknown error.

        Returns:
            (str): An error message indicating that an unknown error occurred.
        """
        return "Unknown error occurred."

    def get_default_message(self) -> str:
        """
        Get the default error message for a given HTTP status code.

        Returns:
            (str): The default error message associated with the provided status code.
                 If no message is found, it falls back to handling an unknown error.
        """
        return http.client.responses.get(self.status_code, self.handle_unknown_error())

__init__(status_code, message=None, headers=None)

ErrorHandler nesnesini belirli bir durum koduyla ba┼člat─▒r.

Parametreler:

─░sim Tip A├ž─▒klama Varsay─▒lan
status_code int

Hatay─▒ temsil eden HTTP durum kodu.

gerekli
message str

Ek ayr─▒nt─▒lar sa─člayan iste─če ba─čl─▒ bir hata mesaj─▒.

None
headers (dict, None)

Yan─▒t ├╝stbilgileri ayr─▒nt─▒lar─▒n─▒ sa─člayan iste─če ba─čl─▒ bir s├Âzl├╝k.

None
Kaynak kodu hub_sdk/helpers/error_handler.py
def __init__(
    self,
    status_code: int,
    message: Optional[str] = None,
    headers: Optional[dict] = None,
):
    """
    Initialize the ErrorHandler object with a given status code.

    Args:
        status_code (int): The HTTP status code representing the error.
        message (str, optional): An optional error message providing additional details.
        headers (dict, None): An optional dictionary providing response headers details.
    """
    self.status_code = status_code
    self.message = message
    self.headers = headers

get_default_message()

Belirli bir HTTP durum kodu i├žin varsay─▒lan hata mesaj─▒n─▒ al─▒n.

─░ade:

Tip A├ž─▒klama
str

Sa─članan durum koduyla ili┼čkili varsay─▒lan hata iletisi. Hi├žbir mesaj bulunamazsa, bilinmeyen bir hatan─▒n i┼členmesine geri d├Ân├╝l├╝r.

Kaynak kodu hub_sdk/helpers/error_handler.py
def get_default_message(self) -> str:
    """
    Get the default error message for a given HTTP status code.

    Returns:
        (str): The default error message associated with the provided status code.
             If no message is found, it falls back to handling an unknown error.
    """
    return http.client.responses.get(self.status_code, self.handle_unknown_error())

handle()

Verilen durum koduna g├Âre hatay─▒ i┼čleyin.

─░ade:

Tip A├ž─▒klama
str

Hatay─▒ a├ž─▒klayan bir mesaj.

Kaynak kodu hub_sdk/helpers/error_handler.py
def handle(self) -> str:
    """
    Handle the error based on the provided status code.

    Returns:
        (str): A message describing the error.
    """
    error_handlers = {
        401: self.handle_unauthorized,
        404: self.handle_not_found,
        500: self.handle_internal_server_error,
        429: self.handle_ratelimit_exceeded,
    }

    handler = error_handlers.get(self.status_code, self.get_default_message)
    return handler()

handle_internal_server_error() staticmethod

Bir dahili sunucu hatas─▒n─▒ (HTTP 500) i┼čleyin.

─░ade:

Tip A├ž─▒klama
str

Dahili sunucu hatas─▒n─▒ belirten bir hata mesaj─▒.

Kaynak kodu hub_sdk/helpers/error_handler.py
@staticmethod
def handle_internal_server_error() -> str:
    """
    Handle an internal server error (HTTP 500).

    Returns:
        (str): An error message indicating an internal server error.
    """
    return "Internal server error."

handle_not_found() staticmethod

Kaynak bulunamad─▒ hatas─▒n─▒ i┼čleyin (HTTP 404).

─░ade:

Tip A├ž─▒klama
str

─░stenen kayna─č─▒n bulunamad─▒─č─▒n─▒ belirten bir hata mesaj─▒.

Kaynak kodu hub_sdk/helpers/error_handler.py
@staticmethod
def handle_not_found() -> str:
    """
    Handle a resource not found error (HTTP 404).

    Returns:
        (str): An error message indicating that the requested resource was not found.
    """
    return "Resource not found."

handle_ratelimit_exceeded()

H─▒z s─▒n─▒r─▒ a┼č─▒ld─▒ hatas─▒n─▒ i┼čleyin (HTTP 429).

─░ade:

Tip A├ž─▒klama
str

H─▒z s─▒n─▒r─▒n─▒n a┼č─▒ld─▒─č─▒n─▒ belirten bir hata mesaj─▒.

Kaynak kodu hub_sdk/helpers/error_handler.py
def handle_ratelimit_exceeded(self) -> str:
    """
    Handle rate limit exceeded error (HTTP 429).

    Returns:
        (str): An error message indicating rate limit exceeded.
    """
    error_msg = "Rate Limits Exceeded: Please try again later."

    if "X-RateLimit-Reset" in self.headers:
        rate_reset = self.headers.get("X-RateLimit-Reset")

        try:
            reset_time = datetime.datetime.fromtimestamp(int(rate_reset)).strftime("%Y-%m-%d %H:%M:%S")
        except ValueError:
            reset_time = "unknown"

        error_msg = (
            "You have exceeded the rate limits for this request. "
            f"You will be able to make requests again after {reset_time}."
        )
    return error_msg

handle_unauthorized() staticmethod

Yetkisiz bir hatay─▒ i┼čleyin (HTTP 401).

─░ade:

Tip A├ž─▒klama
str

Yetkisiz eri┼čimi g├Âsteren bir hata mesaj─▒.

Kaynak kodu hub_sdk/helpers/error_handler.py
@staticmethod
def handle_unauthorized() -> str:
    """
    Handle an unauthorized error (HTTP 401).

    Returns:
        (str): An error message indicating unauthorized access.
    """
    return "Unauthorized: Please check your credentials."

handle_unknown_error() staticmethod

Bilinmeyen bir hatay─▒ i┼čleyin.

─░ade:

Tip A├ž─▒klama
str

Bilinmeyen bir hata olu┼čtu─čunu belirten bir hata mesaj─▒.

Kaynak kodu hub_sdk/helpers/error_handler.py
@staticmethod
def handle_unknown_error() -> str:
    """
    Handle an unknown error.

    Returns:
        (str): An error message indicating that an unknown error occurred.
    """
    return "Unknown error occurred."