python-youtube icon indicating copy to clipboard operation
python-youtube copied to clipboard

Feature request: Add error "reason" to PyYouTubeException class attributes

Open Dyl-M opened this issue 7 months ago • 0 comments

The issue

Even if the error message and error code are already accessible, I'd like to have an (easy) access to the reason(s) for the error, which could be more explicit than the two elements I've mentioned.

From what I understand, since the error handler in PyYouTubeException is defined in this way :

class PyYouTubeException(Exception):
    """
    This is a return demo:
    {'error': {'errors': [{'domain': 'youtube.parameter',
    'reason': 'missingRequiredParameter',
    'message': 'No filter selected. Expected one of: forUsername, managedByMe, categoryId, mine, mySubscribers, id, idParam',
    'locationType': 'parameter',
    'location': ''}],
    'code': 400,
    'message': 'No filter selected. Expected one of: forUsername, managedByMe, categoryId, mine, mySubscribers, id, idParam'}}
    """

    def __init__(self, response: Optional[Union[ErrorMessage, Response]]):
        self.status_code: Optional[int] = None
        self.error_type: Optional[str] = None
        self.message: Optional[str] = None
        self.response: Optional[Union[ErrorMessage, Response]] = response
        self.error_handler()

The “reason” seems to be accessible via the response attribute, which is how most attributes are retrieved when handling the error.

def error_handler(self):
        """
        Error has two big type(but not the error type.): This module's error, Api return error.
        So This will change two error to one format
        """
        if isinstance(self.response, ErrorMessage):
            self.status_code = self.response.status_code
            self.message = self.response.message
            self.error_type = "PyYouTubeException"
        elif isinstance(self.response, Response):
            res_data = self.response.json()
            if "error" in res_data:
                error = res_data["error"]
                if isinstance(error, dict):
                    self.status_code = res_data["error"]["code"]
                    self.message = res_data["error"]["message"]
                else:
                    self.status_code = self.response.status_code
                    self.message = error
                self.error_type = "YouTubeException"

My point

I'm facing this clarity problem with a current project where I'm logging errors when calling the API. The status_code, the error_type or the message don't give enough information about the type of problem I'm experiencing.

If we can dig into the error details using “response” to retrieve the attributes we already have, I don't see why we can't do it for the reason for the error, which may be more explicit than the error code.

Even though I've got an idea of how to access this information, it would be a real benefit in terms of error understanding if we could access this information directly through PyYouTubeException attributes.

Dyl-M avatar Mar 16 '25 12:03 Dyl-M