Skip to content

Commit 665c503

Browse files
authored
Surface publish and unsubscribe errors back to Python (#167)
1 parent 325dc24 commit 665c503

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

awscrt/mqtt.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,10 +543,11 @@ def unsubscribe(self, topic):
543543
future = Future()
544544
packet_id = 0
545545

546-
def unsuback(packet_id):
547-
future.set_result(dict(
548-
packet_id=packet_id
549-
))
546+
def unsuback(packet_id, error_code):
547+
if error_code != 0:
548+
future.set_exception(awscrt.exceptions.from_code(error_code))
549+
else:
550+
future.set_result(dict(packet_id=packet_id))
550551

551552
try:
552553
packet_id = _awscrt.mqtt_client_connection_unsubscribe(self._binding, topic, unsuback)
@@ -626,10 +627,11 @@ def publish(self, topic, payload, qos, retain=False):
626627
future = Future()
627628
packet_id = 0
628629

629-
def puback(packet_id):
630-
future.set_result(dict(
631-
packet_id=packet_id
632-
))
630+
def puback(packet_id, error_code):
631+
if error_code != 0:
632+
future.set_exception(awscrt.exceptions.from_code(error_code))
633+
else:
634+
future.set_result(dict(packet_id=packet_id))
633635

634636
try:
635637
packet_id = _awscrt.mqtt_client_connection_publish(self._binding, topic, payload, qos.value, retain, puback)

source/mqtt_client_connection.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -692,7 +692,6 @@ static void s_publish_complete(
692692
int error_code,
693693
void *userdata) {
694694
(void)connection;
695-
(void)error_code;
696695

697696
struct publish_complete_userdata *metadata = userdata;
698697
assert(metadata);
@@ -703,7 +702,7 @@ static void s_publish_complete(
703702
}
704703

705704
if (metadata->callback != Py_None) {
706-
PyObject *result = PyObject_CallFunction(metadata->callback, "(H)", packet_id);
705+
PyObject *result = PyObject_CallFunction(metadata->callback, "(Hi)", packet_id, error_code);
707706
if (result) {
708707
Py_DECREF(result);
709708
} else {
@@ -956,7 +955,6 @@ static void s_unsuback_callback(
956955
int error_code,
957956
void *userdata) {
958957
(void)connection;
959-
(void)error_code;
960958

961959
PyObject *callback = userdata;
962960

@@ -965,7 +963,7 @@ static void s_unsuback_callback(
965963
return; /* Python has shut down. Nothing matters anymore, but don't crash */
966964
}
967965

968-
PyObject *result = PyObject_CallFunction(callback, "(H)", packet_id);
966+
PyObject *result = PyObject_CallFunction(callback, "(Hi)", packet_id, error_code);
969967
if (result) {
970968
Py_DECREF(result);
971969
} else {

0 commit comments

Comments
 (0)