@@ -143,9 +143,8 @@ static int MqttSocket_WriteDo(MqttClient *client, const byte* buf, int buf_len,
143143
144144 rc = wolfSSL_write (client -> tls .ssl , (char * )buf , buf_len );
145145 if (rc < 0 ) {
146- #if defined(WOLFMQTT_DEBUG_SOCKET ) || defined(WOLFSSL_ASYNC_CRYPT )
147146 int error = wolfSSL_get_error (client -> tls .ssl , 0 );
148- #endif
147+ client -> tls . lastError = error ;
149148 #ifdef WOLFMQTT_DEBUG_SOCKET
150149 if (error != WOLFSSL_ERROR_WANT_WRITE
151150 #ifdef WOLFSSL_ASYNC_CRYPT
@@ -248,6 +247,7 @@ static int MqttSocket_ReadDo(MqttClient *client, byte* buf, int buf_len,
248247 rc = wolfSSL_read (client -> tls .ssl , (char * )buf , buf_len );
249248 if (rc < 0 ) {
250249 int error = wolfSSL_get_error (client -> tls .ssl , 0 );
250+ client -> tls .lastError = error ;
251251 #ifdef WOLFMQTT_DEBUG_SOCKET
252252 if (error != WOLFSSL_ERROR_WANT_READ
253253 #ifdef WOLFSSL_ASYNC_CRYPT
@@ -404,6 +404,9 @@ int MqttSocket_Connect(MqttClient *client, const char* host, word16 port,
404404#if defined(ENABLE_MQTT_TLS ) && !defined(ENABLE_MQTT_CURL ) && \
405405 !defined(ENABLE_MQTT_WEBSOCKET )
406406 if (use_tls ) {
407+ /* Clear any previous TLS error */
408+ client -> tls .lastError = 0 ;
409+
407410 if (client -> tls .ctx == NULL ) {
408411 #ifdef DEBUG_WOLFSSL
409412 wolfSSL_Debugging_ON ();
@@ -502,6 +505,8 @@ int MqttSocket_Connect(MqttClient *client, const char* host, word16 port,
502505 ) {
503506 return MQTT_CODE_CONTINUE ;
504507 }
508+ /* Preserve the error to be used later, e.g. in net callback */
509+ client -> tls .lastError = errnum ;
505510 #ifdef WOLFMQTT_DEBUG_SOCKET
506511 errstr = wolfSSL_ERR_reason_error_string (errnum );
507512 #endif
0 commit comments