Skip to content

FrappeClient eats too many useful errors #56

@peerDE

Description

@peerDE

When using FrappeClient for inserting objects, too many useful errors are suppressed.
This can lead to hours-long head-scratching and thus quick baldness, which calls into question the sustainability of its use during cold weather.

The reason is in the post_process method https://github.com/frappe/frappe-client/blame/aad36de5eb27be1bc6a8357791ee26ecb7cecd99/frappeclient/frappeclient.py#L277-L291 which replaces the useful error message with just:
None

A proper way to output errors would yield a more informative result similar to this:

<Response [417]>
EXPECTATION FAILED
{"exc_type":"MandatoryError","_server_messages":"[\"{\\\"message\\\": \\\"Fehler: Wert fehlt f\\\\u00fcr None: parent\\\", \\\"title\\\": \\\"Nachricht\\\"}\", \"{\\\"message\\\": \\\"Fehler: Wert fehlt f\\\\u00fcr None: parenttype\\\", \\\"title\\\": \\\"Nachricht\\\"}\"]"}

Please note that a suppressed "Expectation failed" can be quite a hurdle for new devs, but might throw off path for a while even seasoned ones, in other words, it's a hassle for most everyone. That might be the reason why the post_process method visibly goes to some lenght (see linked-above code) to display what might be useful to help.

But instead of providing code along the lines of:

if rjson and ('exc_type' in rjson) and rjson['exc_type']:
    raise FrappeException(rjson['exc_type'])

and/or:

if rjson and ('_server_messages' in rjson) and rjson['_server_messages']:
    raise FrappeException(rjson['_server_messages'])

the otherwise properly JSON formatted error as shown above slips through all the catch-useful-error code and just vanishes like my scratched head's hair. There might be other vanishing useful errors having produced more baldness world-wide.

My debugging workaround looks like this (run it after importing FrappeClient):

### DEBUG
# Overload insert method of FrappeClient with this in order to see more errors which otherwise would just be eaten by default by "return None" in FrappeClient's method "post_process":
def MYinsert(self, doc):
    # The following 2 lines are local imports needed to make the overload work without influencing the surrounding CuT (Code under Test)
    from urllib.parse import quote
    import json
    '''Insert a document to the remote server
        :param doc: A dict or Document object to be inserted remotely'''
    res = self.session.post(self.url + "/api/resource/" + quote(doc.get("doctype")),
        data={"data":json.dumps(doc)})
    # The following 3 lines are my custom Error-Not-Eater:
    print(res)
    print(res.reason)
    print(res.text)
    return self.post_process(res)

FrappeClient.insert = MYinsert

and allowed me at least to actually get the suppressed errors which FrappeFramework so usefully supplies and which I needed to now very quickly debug the import scripts I am working on.

Thank you for your attention to this matter, ;-)
and enjoy the workaround as long as this issue is not fixed otherwise.

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