Skip to content

JSONDecodeError not handled in _request() method #13

@nanda-mik

Description

@nanda-mik

My team uses postgrid-python(v1.0.14) to integrate with postgrid and send letter. Recently encountered an error for this which is not handled in the exception catch and led to unexpected failure. We are handling only PMError & NotImplementedError from your side, but got JSONDecodeError which breaks our flow. So in your code it should be handled in line no 139.

Code snippet

def _request(endpoint, method="GET", idempotency_key=None, **kwargs):
    .......
    ......

        for key, value in kwargs.items():
            files = flatten(files, data, key, value)
        res = requests.post(pm_base + endpoint, data=data, files=files, headers=headers)
    elif method == "DELETE":
        res = requests.delete(pm_base + endpoint, params=kwargs, headers=headers)
    else:
        raise NotImplementedError()

    value = res.json() -> This line needs to be inside try-catch...

    try:
    ........
    .......
    ```

Attaching the stack track below:
**Error trace**
    File "/home/appuser/src/CommunicationService/utils/postgrid_letter_backend.py", line 112, in send_letters
letter_obj = postgrid.Letter.create(
File "/usr/local/lib/python3.9/site-packages/postgrid/__init__.py", line 342, in create
return super().create(locals())
File "/usr/local/lib/python3.9/site-packages/postgrid/__init__.py", line 190, in create
return _pm_post(cls.endpoint.format(parent_resource_id), **locals_except_kwargs_and_cls, **locals_['kwargs'])
File "/usr/local/lib/python3.9/site-packages/postgrid/__init__.py", line 163, in _pm_post
return _request(pm_base, endpoint, method='POST', **kwargs)
File "/usr/local/lib/python3.9/site-packages/postgrid/__init__.py", line 139, in _request
value = res.json()
File "/usr/local/lib/python3.9/site-packages/requests/models.py", line 975, in json
raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Can i give you a PR to fix this and work on your codebase? Please let me know about it.    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions