fix: raise MQTTError with message instead of class itself - #1075
Open
reachsridhard wants to merge 2 commits into
Open
fix: raise MQTTError with message instead of class itself#1075reachsridhard wants to merge 2 commits into
reachsridhard wants to merge 2 commits into
Conversation
Raising without parentheses raises the class object, not an instance, which produces a confusing TypeError instead of a meaningful error message. All other call sites already pass a descriptive string.
📝 WalkthroughWalkthrough
ChangesMQTT timeout handling
Estimated code review effort: 1 (Trivial) | ~5 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR: fix: raise MQTTError with message instead of class itself
Description
This PR fixes a bug in
MQTTClient.__enter__whereexceptions.MQTTErrorwas raised without creating an exception instance.Previously, when the client failed to connect to the MQTT broker within the configured timeout, Python attempted to raise the exception class itself rather than an instance. This resulted in a confusing
TypeErrorinstead of a meaningful MQTT connection error.Problem
In
client.py, the original implementation contained:Raising the exception class directly is incorrect because
MQTTErrorexpects an error message.Instead of reporting the actual connection timeout, users would receive a misleading traceback caused by Python attempting to instantiate the exception without the required arguments.
This behavior was also inconsistent with every other
MQTTErrorusage in the same file, where descriptive messages are always provided:This was the only call site raising the exception incorrectly.
Changes
Updated
client.pyto raise anMQTTErrorinstance with a descriptive timeout message.Before
After
Fix
The fix replaces the invalid class-only raise with a properly instantiated
MQTTError, including the configured timeout value in the error message.This provides users with a clear and actionable exception while maintaining consistency with the rest of the MQTT error handling in the file.
Testing
MQTTClient.__enter__times out waiting for the broker.