Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Dyl-M opened this issue Mar 16, 2025 · 0 comments
Open

Comments

@Dyl-M
Copy link
Contributor

Dyl-M commented Mar 16, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant