|
72 | 72 | MQTT_PKT_TYPE_MASK = const(0xF0)
|
73 | 73 |
|
74 | 74 |
|
| 75 | +CONNACK_ERROR_INCORRECT_PROTOCOL = const(0x01) |
| 76 | +CONNACK_ERROR_ID_REJECTED = const(0x02) |
| 77 | +CONNACK_ERROR_SERVER_UNAVAILABLE = const(0x03) |
| 78 | +CONNACK_ERROR_INCORECT_USERNAME_PASSWORD = const(0x04) |
| 79 | +CONNACK_ERROR_UNAUTHORIZED = const(0x05) |
| 80 | + |
75 | 81 | CONNACK_ERRORS = {
|
76 |
| - const(0x01): "Connection Refused - Incorrect Protocol Version", |
77 |
| - const(0x02): "Connection Refused - ID Rejected", |
78 |
| - const(0x03): "Connection Refused - Server unavailable", |
79 |
| - const(0x04): "Connection Refused - Incorrect username/password", |
80 |
| - const(0x05): "Connection Refused - Unauthorized", |
| 82 | + CONNACK_ERROR_INCORRECT_PROTOCOL: "Connection Refused - Incorrect Protocol Version", |
| 83 | + CONNACK_ERROR_ID_REJECTED: "Connection Refused - ID Rejected", |
| 84 | + CONNACK_ERROR_SERVER_UNAVAILABLE: "Connection Refused - Server unavailable", |
| 85 | + CONNACK_ERROR_INCORECT_USERNAME_PASSWORD: "Connection Refused - Incorrect username/password", |
| 86 | + CONNACK_ERROR_UNAUTHORIZED: "Connection Refused - Unauthorized", |
81 | 87 | }
|
82 | 88 |
|
83 | 89 | _default_sock = None # pylint: disable=invalid-name
|
|
87 | 93 | class MMQTTException(Exception):
|
88 | 94 | """MiniMQTT Exception class."""
|
89 | 95 |
|
| 96 | + def __init__(self, error, code=None): |
| 97 | + super().__init__(error, code) |
| 98 | + self.code = code |
| 99 | + |
90 | 100 |
|
91 | 101 | class NullLogger:
|
92 | 102 | """Fake logger class that does not do anything"""
|
@@ -428,8 +438,14 @@ def connect(
|
428 | 438 | self.logger.warning(f"Socket error when connecting: {e}")
|
429 | 439 | backoff = False
|
430 | 440 | except MMQTTException as e:
|
431 |
| - last_exception = e |
432 | 441 | self.logger.info(f"MMQT error: {e}")
|
| 442 | + if e.code in [ |
| 443 | + CONNACK_ERROR_INCORECT_USERNAME_PASSWORD, |
| 444 | + CONNACK_ERROR_UNAUTHORIZED, |
| 445 | + ]: |
| 446 | + # No sense trying these again, re-raise |
| 447 | + raise |
| 448 | + last_exception = e |
433 | 449 | backoff = True
|
434 | 450 |
|
435 | 451 | if self._reconnect_attempts_max > 1:
|
@@ -535,7 +551,7 @@ def _connect(
|
535 | 551 | rc = self._sock_exact_recv(3)
|
536 | 552 | assert rc[0] == 0x02
|
537 | 553 | if rc[2] != 0x00:
|
538 |
| - raise MMQTTException(CONNACK_ERRORS[rc[2]]) |
| 554 | + raise MMQTTException(CONNACK_ERRORS[rc[2]], code=rc[2]) |
539 | 555 | self._is_connected = True
|
540 | 556 | result = rc[0] & 1
|
541 | 557 | if self.on_connect is not None:
|
|
0 commit comments