From 284eea0cc3cea0ac4fc604a2fd748276fb47d35c Mon Sep 17 00:00:00 2001 From: Igor Abdrakhimov Date: Wed, 8 Jan 2025 15:51:16 -0800 Subject: [PATCH] Check reason string in MQTT5 samples (#772) --- samples/mqtt5/mqtt5_pubsub/main.cpp | 22 +++++++++---------- .../mqtt5/mqtt5_shared_subscription/main.cpp | 21 ++++++++++-------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/samples/mqtt5/mqtt5_pubsub/main.cpp b/samples/mqtt5/mqtt5_pubsub/main.cpp index 479146c5a..070797c60 100644 --- a/samples/mqtt5/mqtt5_pubsub/main.cpp +++ b/samples/mqtt5/mqtt5_pubsub/main.cpp @@ -141,11 +141,11 @@ int main(int argc, char *argv[]) { if (reasonCode > Mqtt5::SubAckReasonCode::AWS_MQTT5_SARC_UNSPECIFIED_ERROR) { - fprintf( - stdout, - "MQTT5 Client Subscription failed with server error code: (%d)%s\n", - reasonCode, - suback->getReasonString()->c_str()); + fprintf(stdout, "MQTT5 Client Subscription failed with server error code: %d\n", reasonCode); + if (suback->getReasonString().has_value()) + { + fprintf(stdout, "\tError reason string: %s\n", suback->getReasonString()->c_str()); + } subscribeSuccess.set_value(false); return; } @@ -185,13 +185,13 @@ int main(int argc, char *argv[]) } else { - fprintf( - stdout, - "PubACK reason code: %d : %s\n", - puback->getReasonCode(), - puback->getReasonString()->c_str()); + fprintf(stdout, "PubACK reason code: %d\n", puback->getReasonCode()); + if (puback->getReasonString().has_value()) + { + fprintf(stdout, "\nError reason string: %s\n", puback->getReasonString()->c_str()); + } } - }; + } }; uint32_t publishedCount = 0; diff --git a/samples/mqtt5/mqtt5_shared_subscription/main.cpp b/samples/mqtt5/mqtt5_shared_subscription/main.cpp index 6f8085699..88a2c9fd2 100644 --- a/samples/mqtt5/mqtt5_shared_subscription/main.cpp +++ b/samples/mqtt5/mqtt5_shared_subscription/main.cpp @@ -246,11 +246,11 @@ int main(int argc, char *argv[]) { if (reasonCode >= Mqtt5::SubAckReasonCode::AWS_MQTT5_SARC_UNSPECIFIED_ERROR) { - fprintf( - stdout, - "MQTT5 Client Subscription failed with server error code: (%d)%s\n", - reasonCode, - suback->getReasonString()->c_str()); + fprintf(stdout, "MQTT5 Client Subscription failed with server error code: %d\n", reasonCode); + if (suback->getReasonString().has_value()) + { + fprintf(stdout, "\tError reason string: %s\n", suback->getReasonString()->c_str()); + } subscribeSuccess.set_value(reasonCode); return; } @@ -331,12 +331,15 @@ int main(int argc, char *argv[]) { fprintf( stdout, - "[%s] Publish failed. PubACK reason code: %d : %s\n", + "[%s] Publish failed. PubACK reason code: %d\n", publisher->name.c_str(), - puback->getReasonCode(), - puback->getReasonString()->c_str()); + puback->getReasonCode()); + if (puback->getReasonString().has_value()) + { + fprintf(stdout, "\nError reason string: %s\n", puback->getReasonString()->c_str()); + } } - }; + } }; uint64_t publishedCount = 0;