Updating some of neobolts exceptions to extract pertinent information


(Emehrkay) #1

I ran into a an issue with a constraint returning an error, but I do not want to display that error to my users.

`Node(136) already exists with label` User` and property `email_address` = 'test@test.com'

I figured if we could easily add a method/property to the exceptions that will pull the relevant info out of the message.

class ConstraintError(ClientError):
    """
    """
    @property
    def data(self):
        import re
        r = re.compile("(\w+)\((\d+)\) already exists with label `(.*)` and property `(.*)` = '(.*)'")
        m = r.findall(self.message)[0]

        return {
            'entity': m[0],
            'id': m[1],
            'labels': m[2],
            'field': m[3],
            'value': m[4],
        }

I would put this functionality in another class that would allow users to type check for its existence, something like DataError, but that is the gist of it. Given a list of example return messages, we could easily come up with the regexes and update the lib.

What do you think?


(Nigel Small) #2

Parsing the text from error messages is pretty fragile in the general case. Consider in particular the difficulties in parsing the serialised forms of complex data types, escape characters and internationalised text.

Note also that these error messages are generated by the server and can be very context specific. That makes it very hard to produce a meaningful set of extracted data as part of a general purpose solution. That said, you might have some success with a solution very specific to your domain or application.

I'd personally also consider whether this approach to solving your original problem might be a dead end. What types of actions can your users undertake? How complex can those actions get? Is this approach future proof? Why do you not want to show them the original message verbatim?

So I'm afraid I don't consider this to be a helpful general-purpose feature to add to the drivers.


(Emehrkay) #3

Parsing the message is the only way, without updating the actual response from the server, to get that information to display to your users.

I could check for the existence of an entity with certain values before I attempt to add a new one, but that defeats the point of constraints. And if my entities have multiple constraints, I am not sure what field caused the ConstraintError. Pulling out that relevant data from the string returned from the server is probably the only approach, this is how it is handled in the SQL world.

That is fine, I can just add the extractions to my layer. Can you point me to a list of common error messages or where I should look for them in the code?

Thanks