diff options
author | Matus Valo <matusvalo@users.noreply.github.com> | 2018-12-29 11:20:56 +0100 |
---|---|---|
committer | Asif Saif Uddin <auvipy@gmail.com> | 2018-12-29 16:20:56 +0600 |
commit | a7dd9d4e3d8e1a9db8f13f6feffeabd903988513 (patch) | |
tree | 25c2135686d59fdfc2060b188b72df3ed5e73c61 | |
parent | f16df2a17630c9804a6da614443c5e862271823f (diff) | |
download | py-amqp-a7dd9d4e3d8e1a9db8f13f6feffeabd903988513.tar.gz |
Improved empty AMQPError string representation (#231)
-rw-r--r-- | amqp/exceptions.py | 4 | ||||
-rw-r--r-- | t/unit/test_exceptions.py | 25 |
2 files changed, 26 insertions, 3 deletions
diff --git a/amqp/exceptions.py b/amqp/exceptions.py index ed767b4..0ce48f3 100644 --- a/amqp/exceptions.py +++ b/amqp/exceptions.py @@ -49,7 +49,9 @@ class AMQPError(Exception): def __str__(self): if self.method: return '{0.method}: ({0.reply_code}) {0.reply_text}'.format(self) - return self.reply_text or '<AMQPError: unknown error>' + return self.reply_text or '<{}: unknown error>'.format( + type(self).__name__ + ) @property def method(self): diff --git a/t/unit/test_exceptions.py b/t/unit/test_exceptions.py index a6a2a50..113b7a8 100644 --- a/t/unit/test_exceptions.py +++ b/t/unit/test_exceptions.py @@ -1,16 +1,37 @@ from __future__ import absolute_import, unicode_literals from case import Mock +import pytest +import amqp.exceptions from amqp.exceptions import AMQPError, error_for_code +AMQP_EXCEPTIONS = [ + 'ConnectionError', 'ChannelError', + 'RecoverableConnectionError', 'IrrecoverableConnectionError', + 'RecoverableChannelError', 'IrrecoverableChannelError', + 'ConsumerCancelled', 'ContentTooLarge', 'NoConsumers', + 'ConnectionForced', 'InvalidPath', 'AccessRefused', 'NotFound', + 'ResourceLocked', 'PreconditionFailed', 'FrameError', 'FrameSyntaxError', + 'InvalidCommand', 'ChannelNotOpen', 'UnexpectedFrame', 'ResourceError', + 'NotAllowed', 'AMQPNotImplementedError', 'InternalError', +] + class test_AMQPError: def test_str(self): - assert str(AMQPError()) + assert str(AMQPError()) == '<AMQPError: unknown error>' x = AMQPError(method_sig=(50, 60)) - assert str(x) + assert str(x) == '(50, 60): (0) None' + x = AMQPError('Test Exception') + assert str(x) == 'Test Exception' + + @pytest.mark.parametrize("amqp_exception", AMQP_EXCEPTIONS) + def test_str_subclass(self, amqp_exception): + exp = '<{}: unknown error>'.format(amqp_exception) + exception_class = getattr(amqp.exceptions, amqp_exception) + assert str(exception_class()) == exp class test_error_for_code: |