Skip to content

Raise exception (or not) when a JSON response with errorCode is received #2

@femtotrader

Description

@femtotrader

Hello,

I think most of json.loads(...) should be replaced by self.parse_response(...)

def parse_response_without_exception(self, *args, **kwargs):
    response = json.loads(*args, **kwargs)
    return(response)

def parse_response_with_exception(self, *args, **kwargs):
    response = json.loads(*args, **kwargs)
    if 'errorCode' in response:
        raise(Exception(response['errorCode']))
    return(response)

so if an error message is returned it will raise an exception so it can be catch (or not)

if you want to keep current behaviour you should set inside __init__

self.parse_response = self.parse_response_without_exception

Moreover it will also allow user to easily monkey pach this code to redefine this parse_response
function.
I personally prefer to return Bunch object instead of dict so my own parse_response looks like

from bunch import Bunch, bunchify

 def parse_response_with_exception(self, *args, **kwargs):
    response = json.loads(*args, **kwargs)
    if 'errorCode' in response:
        raise(Exception(response['errorCode']))
    return(bunchify(response))

Kind regards

Femto

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions