From 550e6d9e88ceed20d181fdfd4dde95db878257ca Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Piotr=20Ma=C5=9Blanka?= <piotr.maslanka@henrietta.com.pl>
Date: Sat, 4 Jan 2020 19:15:41 +0100
Subject: [PATCH] exceptions will display now correctly

---
 CHANGELOG.md           |  1 +
 coolamqp/exceptions.py | 15 +++++++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4b379fe..edfe613 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,7 @@
 * bugfix: a race condition during connection setup itself
 * moved __version__ to coolamqp root
 * split `compile_definitions` into a separate package
+* exceptions will display their reply_text correctly if fed a memoryview
 
 # v0.100:
 
diff --git a/coolamqp/exceptions.py b/coolamqp/exceptions.py
index f55e68d..34f5404 100644
--- a/coolamqp/exceptions.py
+++ b/coolamqp/exceptions.py
@@ -27,11 +27,22 @@ class AMQPError(CoolAMQPError):
         return self.reply_code in HARD_ERRORS
 
     def __str__(self):  # type: () -> str
-        return 'AMQP error %s: %s' % (self.reply_code, self.reply_text)
+        return 'AMQP error %s: %s' % (self.reply_code, self.__get_reply_text())
+
+    def __get_reply_text(self):  # type: () -> str
+        if isinstance(self.reply_text, memoryview):
+            reply_text = self.reply_text.tobytes().decode('utf8')
+        else:
+            reply_text = self.reply_text
+
+        if isinstance(self.reply_text, bytes):
+            reply_text = self.reply_text.decode('utf8')
+
+        return reply_text
 
     def __repr__(self):  # type: () -> str
         return 'AMQPError(' + repr(self.reply_code) + ', ' + repr(
-            self.reply_text) + \
+            self.__get_reply_text()) + \
                ', ' + repr(self.class_id) + ', ' + repr(self.method_id) + ')'
 
     def __init__(self, *args):
-- 
GitLab