์ฝ˜ํ…์ธ ๋กœ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

์ฐธ์กฐ 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)

์ถ”๊ฐ€ ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์„ ํƒ์  ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์ž…๋‹ˆ๋‹ค. ๊ธฐ๋ณธ๊ฐ’์€ ์—†์Œ์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ 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.
    """

    def __init__(self, status_code: int, message: Optional[str] = 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.
        """
        self.status_code = status_code
        self.message = message

    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,
        }

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

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

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

    def handle_not_found(self) -> 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."

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

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

    def handle_unknown_error(self) -> 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)

์ง€์ •๋œ ์ƒํƒœ ์ฝ”๋“œ๋กœ ErrorHandler ๊ฐ์ฒด๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

๋งค๊ฐœ๋ณ€์ˆ˜:

์ด๋ฆ„ ์œ ํ˜• ์„ค๋ช… ๊ธฐ๋ณธ๊ฐ’
status_code int

์˜ค๋ฅ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” HTTP ์ƒํƒœ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.

ํ•„์ˆ˜
message str

์ถ”๊ฐ€ ์„ธ๋ถ€ ์ •๋ณด๋ฅผ ์ œ๊ณตํ•˜๋Š” ์„ ํƒ์  ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์ž…๋‹ˆ๋‹ค.

None
์˜ ์†Œ์Šค ์ฝ”๋“œ hub_sdk/helpers/error_handler.py
def __init__(self, status_code: int, message: Optional[str] = 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.
    """
    self.status_code = status_code
    self.message = message

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,
    }

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

handle_internal_server_error()

๋‚ด๋ถ€ ์„œ๋ฒ„ ์˜ค๋ฅ˜(HTTP 500)๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
str

๋‚ด๋ถ€ ์„œ๋ฒ„ ์˜ค๋ฅ˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ hub_sdk/helpers/error_handler.py
def handle_internal_server_error(self) -> 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()

๋ฆฌ์†Œ์Šค๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ ์˜ค๋ฅ˜(HTTP 404)๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
str

์š”์ฒญ๋œ ๋ฆฌ์†Œ์Šค๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ hub_sdk/helpers/error_handler.py
def handle_not_found(self) -> 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_unauthorized()

์Šน์ธ๋˜์ง€ ์•Š์€ ์˜ค๋ฅ˜(HTTP 401)๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
str

๋ฌด๋‹จ ์•ก์„ธ์Šค๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ hub_sdk/helpers/error_handler.py
def handle_unauthorized(self) -> str:
    """
    Handle an unauthorized error (HTTP 401).

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

handle_unknown_error()

์•Œ ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜๋ฅผ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.

๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค:

์œ ํ˜• ์„ค๋ช…
str

์•Œ ์ˆ˜ ์—†๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์˜ค๋ฅ˜ ๋ฉ”์‹œ์ง€์ž…๋‹ˆ๋‹ค.

์˜ ์†Œ์Šค ์ฝ”๋“œ hub_sdk/helpers/error_handler.py
def handle_unknown_error(self) -> str:
    """
    Handle an unknown error.

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