sdk-codegen icon indicating copy to clipboard operation
sdk-codegen copied to clipboard

Python: Make Looker endpoint error response more accessible

Open ob1-dev opened this issue 4 years ago • 3 comments

I would like to be able to inspect the Looker API response data to better understand the type of error I have got when performing an action. Knowing that information I can then go on to perform a different set of actions after the error.

For example the create_user can have a variety of responses like 'Bad Request', 'Validation Error' etc. but in order to get that information my exception handling has to do the following:

try:
    """
    do something in Looker SDK
    """
except looker_error.SDKError as exc:
    error_data = json.loads(exc.args[0])
    """
    parse error_data to take different actions depending on response
    """

My suggestion would be to have a new error class that inherits from the existing SDKError, which takes the response as the first argument making it accessible as a property.

class APIError(SDKError):
    """API error class
    """
    def __init__(self, sdk_response, *args, **kwargs):
        super(APIError).__init__(*args, **kwargs)
        self._sdk_response = json.loads(sdk_response)
    
    @property
    def sdk_response(self):
        return self._sdk_response

Then updating the APIMethods._return call raise this error instead.

        encoding = response.encoding
        if not response.ok:
            raise error.APIError(response.value.decode(encoding=encoding))
        ret: TReturn
        if structure is None:

This implementation would mean that all existing handling of SDKErrors would continue to work, and those who want to take a richer set of handling would be able to do so! 🎆

ob1-dev avatar Jun 29 '21 21:06 ob1-dev

In other news, I don't seem to be able to label or link this issue 😢

ob1-dev avatar Jun 29 '21 21:06 ob1-dev

Thanks for this! We have a todo to improve error handling/reporting/info for all our SDKs so you're definitely on the right track with this issue. We'll be taking a look at your PR soon and will provide feedback.

jkaster avatar Jun 29 '21 23:06 jkaster

In other news, I don't seem to be able to label or link this issue

Good to know. That may be a GitHub "feature."

jkaster avatar Jun 29 '21 23:06 jkaster