typeform-python-sdk icon indicating copy to clipboard operation
typeform-python-sdk copied to clipboard

More descriptive error messages

Open paparaka opened this issue 3 years ago • 0 comments

Description:

When providing an incorect request body, the error messge is not descriptive enough.

Example:

from typeform import Typeform

r = Typeform("XXXXXXXX").forms.create(data={
    "title": "Incorrect form payload",
    "type": "quiz",
    "theme": https://api.typeform.com/themes/GKFi5U"
})

Raises the following exception:

...
File ~/Projects/coef/typeform-python-sdk/typeform/client.py:41, in Client.__validator(self, result)
     39 if type(body) is dict and body.get('code', None) is not None:
     40     print(body)
---> 41     raise Exception(body.get('description'))
     42 elif result.status_code >= 400:
     43     raise Exception(result.reason)

Exception: The payload is invalid.

If you are to intercept the HTTP response, you will receive a much more verbose error message:

{
    "code": "VALIDATION_ERROR",
    "description": "The payload is invalid.",
    "details": [
        {
            "code": "WRONG_TYPE",
            "description": "should be object,null",
            "field": "/theme",
            "in": "body"
        }
    ]
}

Motivation for or Use Case:

I'd like to handle errors better and understand where the issue is

Related Issues - has a similar issue been reported before?:

#16 Invalid payload error when creating a new Typeform

Environment Configuration:

Latest revision at: ccfe1ec

Suggest a Fix:

# in typeform/client.py L39:
if type(body) is dict and body.get('code', None) is not None:
            print(f"Error code: {body.get('code')}")
            print(f"Error details: {body.get('details')}")
            raise Exception(body.get('description'))

paparaka avatar Dec 03 '22 20:12 paparaka