Skip to content

fix: raise MQTTError with message instead of class itself - #1075

Open
reachsridhard wants to merge 2 commits into
taverntesting:masterfrom
reachsridhard:fix/mqtt-error-no-message
Open

fix: raise MQTTError with message instead of class itself#1075
reachsridhard wants to merge 2 commits into
taverntesting:masterfrom
reachsridhard:fix/mqtt-error-no-message

Conversation

@reachsridhard

@reachsridhard reachsridhard commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

PR: fix: raise MQTTError with message instead of class itself

Description

This PR fixes a bug in MQTTClient.__enter__ where exceptions.MQTTError was 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 TypeError instead of a meaningful MQTT connection error.


Problem

In client.py, the original implementation contained:

raise exceptions.MQTTError

Raising the exception class directly is incorrect because MQTTError expects 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 MQTTError usage in the same file, where descriptive messages are always provided:

raise exceptions.MQTTError("could not publish message") from e
raise exceptions.MQTTError(
    "err {:s}: {:s}".format(...)
)
raise exceptions.MQTTError(
    f"Error subscribing to '{topic}' (err code {status})"
)

This was the only call site raising the exception incorrectly.


Changes

Updated client.py to raise an MQTTError instance with a descriptive timeout message.

Before

self._disconnect()
logger.error(
    "Could not connect to broker after %s seconds",
    self._connect_timeout,
)
raise exceptions.MQTTError

After

self._disconnect()
logger.error(
    "Could not connect to broker after %s seconds",
    self._connect_timeout,
)
raise exceptions.MQTTError(
    "Could not connect to broker after {} seconds".format(
        self._connect_timeout
    )
)

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

  • Existing MQTT integration tests already exercise the connection workflow.
  • This change only affects the error path when MQTTClient.__enter__ times out waiting for the broker.

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.
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

MQTTClient.__enter__ now raises MQTTError with the configured timeout when the broker connection is not established in time.

Changes

MQTT timeout handling

Layer / File(s) Summary
Report connection timeout
tavern/_plugins/mqtt/client.py
The timeout failure raises MQTTError with a formatted message that includes the configured timeout.

Estimated code review effort: 1 (Trivial) | ~5 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: raising an MQTTError instance with a message instead of the exception class.
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant