python-youtube
python-youtube copied to clipboard
Feature request: Add error "reason" to PyYouTubeException class attributes
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.
- I recently made a change to display the
messageattribute in logs instead oferror_typeand the result is not really satisfying; ending up with the message "The operation was aborted.", not giving much information about what actually went wrong. - I don't particularly want to have to display the entire
responseattribute either, as I don't want to overload the logs with unhelpful information.
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.