μ½˜ν…μΈ λ‘œ κ±΄λ„ˆλ›°κΈ°

μ°Έμ‘° hub_sdk/helpers/error_handler.py

μ°Έκ³ 

이 νŒŒμΌμ€ https://github.com/ultralytics/hub-sdk/blob/main/hub_sdk/helpers/error_handler .pyμ—μ„œ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€. 문제λ₯Ό λ°œκ²¬ν•˜λ©΄ ν’€ λ¦¬ν€˜μŠ€νŠΈ πŸ› οΈ 에 κΈ°μ—¬ν•˜μ—¬ 문제 해결을 λ„μ™€μ£Όμ„Έμš”. κ°μ‚¬ν•©λ‹ˆλ‹€ πŸ™!



hub_sdk.helpers.error_handler.ErrorHandler

HTTP μƒνƒœ μ½”λ“œ 및 였λ₯˜ λ©”μ‹œμ§€λ₯Ό κ΄€λ¦¬ν•˜κΈ° μœ„ν•œ 였λ₯˜ ν•Έλ“€λŸ¬λ₯Ό λ‚˜νƒ€λƒ…λ‹ˆλ‹€.

속성:

이름 μœ ν˜• μ„€λͺ…
status_code int

였λ₯˜μ™€ κ΄€λ ¨λœ HTTP μƒνƒœ μ½”λ“œμž…λ‹ˆλ‹€.

message (str, None)

μΆ”κ°€ μ„ΈλΆ€ 정보λ₯Ό μ œκ³΅ν•˜λŠ” 선택적 였λ₯˜ λ©”μ‹œμ§€μž…λ‹ˆλ‹€. 기본값은 μ—†μŒμž…λ‹ˆλ‹€.

headers (dict, None)

응닡 헀더 μ„ΈλΆ€ 정보λ₯Ό μ œκ³΅ν•˜λŠ” 선택적 λ”•μ…”λ„ˆλ¦¬μž…λ‹ˆλ‹€. 기본값은 μ—†μŒμž…λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ 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 객체λ₯Ό μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€.

λ§€κ°œλ³€μˆ˜:

이름 μœ ν˜• μ„€λͺ… κΈ°λ³Έκ°’
status_code int

였λ₯˜λ₯Ό λ‚˜νƒ€λ‚΄λŠ” HTTP μƒνƒœ μ½”λ“œμž…λ‹ˆλ‹€.

ν•„μˆ˜
message str

μΆ”κ°€ μ„ΈλΆ€ 정보λ₯Ό μ œκ³΅ν•˜λŠ” 선택적 였λ₯˜ λ©”μ‹œμ§€μž…λ‹ˆλ‹€.

None
headers (dict, None)

응닡 헀더 μ„ΈλΆ€ 정보λ₯Ό μ œκ³΅ν•˜λŠ” 선택적 μ‚¬μ „μž…λ‹ˆλ‹€.

None
의 μ†ŒμŠ€ μ½”λ“œ 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()

μ§€μ •λœ HTTP μƒνƒœ μ½”λ“œμ— λŒ€ν•œ κΈ°λ³Έ 였λ₯˜ λ©”μ‹œμ§€λ₯Ό κ°€μ Έμ˜΅λ‹ˆλ‹€.

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
str

제곡된 μƒνƒœ μ½”λ“œμ™€ κ΄€λ ¨λœ κΈ°λ³Έ 였λ₯˜ λ©”μ‹œμ§€μž…λ‹ˆλ‹€. λ©”μ‹œμ§€λ₯Ό 찾을 수 μ—†μœΌλ©΄ μ•Œ 수 μ—†λŠ” 였λ₯˜ 처리둜 λŒμ•„κ°‘λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ 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()

제곡된 μƒνƒœ μ½”λ“œμ— 따라 였λ₯˜λ₯Ό μ²˜λ¦¬ν•©λ‹ˆλ‹€.

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
str

였λ₯˜λ₯Ό μ„€λͺ…ν•˜λŠ” λ©”μ‹œμ§€μž…λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ 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

λ‚΄λΆ€ μ„œλ²„ 였λ₯˜(HTTP 500)λ₯Ό μ²˜λ¦¬ν•©λ‹ˆλ‹€.

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
str

λ‚΄λΆ€ μ„œλ²„ 였λ₯˜λ₯Ό λ‚˜νƒ€λ‚΄λŠ” 였λ₯˜ λ©”μ‹œμ§€μž…λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ 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

λ¦¬μ†ŒμŠ€λ₯Ό 찾을 수 μ—†μŒ 였λ₯˜(HTTP 404)λ₯Ό μ²˜λ¦¬ν•©λ‹ˆλ‹€.

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
str

μš”μ²­λœ λ¦¬μ†ŒμŠ€λ₯Ό 찾을 수 μ—†μŒμ„ λ‚˜νƒ€λ‚΄λŠ” 였λ₯˜ λ©”μ‹œμ§€μž…λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ 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()

처리 속도 μ œν•œ 초과 였λ₯˜(HTTP 429).

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
str

μš”κΈˆ ν•œλ„ 초과λ₯Ό λ‚˜νƒ€λ‚΄λŠ” 였λ₯˜ λ©”μ‹œμ§€μž…λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ 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

무단 였λ₯˜ 처리(HTTP 401).

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
str

무단 μ•‘μ„ΈμŠ€λ₯Ό λ‚˜νƒ€λ‚΄λŠ” 였λ₯˜ λ©”μ‹œμ§€μž…λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ 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

μ•Œ 수 μ—†λŠ” 였λ₯˜λ₯Ό μ²˜λ¦¬ν•©λ‹ˆλ‹€.

λ°˜ν™˜ν•©λ‹ˆλ‹€:

μœ ν˜• μ„€λͺ…
str

μ•Œ 수 μ—†λŠ” 였λ₯˜κ°€ λ°œμƒν–ˆμŒμ„ λ‚˜νƒ€λ‚΄λŠ” 였λ₯˜ λ©”μ‹œμ§€μž…λ‹ˆλ‹€.

의 μ†ŒμŠ€ μ½”λ“œ 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."