From 59cc98156604c6d5a8ec07c80d70414956a3a343 Mon Sep 17 00:00:00 2001 From: Markus Zolliker Date: Thu, 22 Dec 2022 13:12:15 +0100 Subject: [PATCH] client: detect original frappy error class The text part of the error report contains the original error class - convert it to the frappy error class, if possible. This makes the error messages on the client nicer, as the error class would be duplicated in the error message on the client side. Change-Id: If2e0c3eb15ac2dd1b80a851ff42e4076557a325d --- frappy/errors.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/frappy/errors.py b/frappy/errors.py index 5affb4a..dcb9f75 100644 --- a/frappy/errors.py +++ b/frappy/errors.py @@ -22,6 +22,9 @@ # ***************************************************************************** """Define (internal) SECoP Errors""" +import re +from ast import literal_eval + class SECoPError(RuntimeError): silent = False # silent = True indicates that the error is already logged @@ -121,8 +124,27 @@ class HardwareError(SECoPError): name = 'HardwareError' +FRAPPY_ERROR = re.compile(r'(.*)\(.*\)$') + + def make_secop_error(name, text): - errcls = EXCEPTIONS.get(name, InternalError) + """create an instance of SECoPError from an error report + + :param name: the error class from the SECoP error report + :param text: the second item of a SECoP error report + :return: the built instance of SECoPError + """ + try: + # try to interprete the error text as a repr() + # as it would be created by a Frappy server + cls, textarg = FRAPPY_ERROR.match(text).groups() + errcls = locals()[cls] + if errcls.name == name: + # convert repr() to + text = literal_eval(textarg) + except Exception: + # probably not a Frappy server, or running a different version + errcls = EXCEPTIONS.get(name, InternalError) return errcls(text)