From f3cde63e37d43b433de2217e6407c2837d246ee5 Mon Sep 17 00:00:00 2001 From: saces Date: Fri, 6 Feb 2026 16:13:55 +0100 Subject: [PATCH] python: improve api error handling --- smal/src/smal/pygomx.py | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/smal/src/smal/pygomx.py b/smal/src/smal/pygomx.py index 2f0dfa2..7dc28e5 100644 --- a/smal/src/smal/pygomx.py +++ b/smal/src/smal/pygomx.py @@ -7,6 +7,22 @@ from .errors import APIError logger = logging.getLogger(__name__) +def checkApiError(cstr): + result = ffi.string(cstr).decode("utf-8") + lib.FreeCString(cstr) + + if result.startswith("ERR:"): + raise APIError(result) + + if result == "SUCCESS.": + return + + logger.debug(result) + + result_dict = json.loads(result) + return result_dict + + class _MXClient: """ core binding @@ -49,19 +65,11 @@ class _MXClient: def _sync(self): r = lib.apiv0_startclient(self.client_id) - result = ffi.string(r) - lib.FreeCString(r) - # if result.startswith(b"ERR:"): - # raise APIError(result) - print("_sync: ", result) + checkApiError(r) def _stopsync(self): r = lib.apiv0_stopclient(self.client_id) - result = ffi.string(r) - lib.FreeCString(r) - # if result.startswith(b"ERR:"): - # raise APIError(result) - print("_stopsync: ", result) + checkApiError(r) def _sendmessage(self, data_dict): data = json.dumps(data_dict).encode(encoding="utf-8")