From f9eff2d8b2bb18fce63a45956443303f94ed8d90 Mon Sep 17 00:00:00 2001 From: Jakub Skiepko Date: Thu, 26 Jun 2014 13:34:06 +0200 Subject: [PATCH] Fix handling errors. --- routeros_api/api_communicator.py | 10 ++++++---- routeros_api/sentence.py | 3 +++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/routeros_api/api_communicator.py b/routeros_api/api_communicator.py index d15f211..f2ed66b 100644 --- a/routeros_api/api_communicator.py +++ b/routeros_api/api_communicator.py @@ -65,11 +65,12 @@ class ApiCommunicator(object): if response.type == 'done': asynchronous_response.done = True elif response.type == 'trap': - asynchronous_response.done = True asynchronous_response.error = response.attributes['message'] elif response.type == 'fatal': del(self.response_buffor[tag]) - raise exceptions.RouterOsApiConnectionClosedError() + message = "Fatal error executing command {}".format( + asynchronous_response.command) + raise exceptions.RouterOsApiConnectionClosedError(message) def receive_synchronous(self): return self.receive_asynchronous('synchronous') @@ -80,8 +81,9 @@ class ApiCommunicator(object): self.process_single_response() del(self.response_buffor[tag]) if response.error: - raise exceptions.RouterOsApiCommunicationError( - response.error.decode()) + message = "Error \"{}\" executing command {}".format( + response.error.decode(), response.command) + raise exceptions.RouterOsApiCommunicationError(message) else: if not response.binary: response.decode() diff --git a/routeros_api/sentence.py b/routeros_api/sentence.py index c84de79..c7cda41 100644 --- a/routeros_api/sentence.py +++ b/routeros_api/sentence.py @@ -76,3 +76,6 @@ class CommandSentence(object): for key, value in kwargs.items(): self.queries.add(query.IsEqualQuery(key, value)) + + def __str__(self): + return ' '.join(self.get_api_format())