← Back to team overview

txaws-dev team mailing list archive

[Merge] lp:~ack/txaws/apierror-unicode-fixes into lp:txaws

 

Alberto Donato has proposed merging lp:~ack/txaws/apierror-unicode-fixes into lp:txaws.

Requested reviews:
  txAWS Developers (txaws-dev)

For more details, see:
https://code.launchpad.net/~ack/txaws/apierror-unicode-fixes/+merge/84108

This branch does the following:

 - return the Parameter value as unicode in error messages, if possible.
 - convert APIError message to an ASCII string to avoid errors with twisted logger.
-- 
https://code.launchpad.net/~ack/txaws/apierror-unicode-fixes/+merge/84108
Your team txAWS Developers is requested to review the proposed merge of lp:~ack/txaws/apierror-unicode-fixes into lp:txaws.
=== modified file 'txaws/server/exception.py'
--- txaws/server/exception.py	2011-05-14 05:22:41 +0000
+++ txaws/server/exception.py	2011-12-01 14:46:14 +0000
@@ -23,3 +23,10 @@
             if self.code is not None or self.message is not None:
                 raise RuntimeError("If the full response payload is passed, "
                                    "code and message must not be set.")
+
+    def __str__(self):
+        # This avoids an exception when twisted logger logs the message, as it
+        # currently doesn't support unicode.
+        if self.message is not None:
+            return self.message.encode("ascii", "replace")
+        return ""

=== modified file 'txaws/server/schema.py'
--- txaws/server/schema.py	2011-10-14 12:42:48 +0000
+++ txaws/server/schema.py	2011-12-01 14:46:14 +0000
@@ -102,7 +102,7 @@
             return parsed
         except ValueError:
             try:
-                value = value.decode("ascii")
+                value = value.decode("utf-8")
                 message = "Invalid %s value %s" % (self.kind, value)
             except UnicodeDecodeError:
                 message = "Invalid %s value" % self.kind

=== modified file 'txaws/server/tests/test_exception.py'
--- txaws/server/tests/test_exception.py	2011-05-13 07:14:44 +0000
+++ txaws/server/tests/test_exception.py	2011-12-01 14:46:14 +0000
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
 from unittest import TestCase
 
 from txaws.server.exception import APIError
@@ -49,3 +51,11 @@
         """
         error = APIError("200", response="noes")
         self.assertEqual(200, error.status)
+
+    def test_with_unicode_message(self):
+        """
+        L{APIError} will convert message to plain ASCII if converted to string. 
+        """
+        error = APIError(400, code="APIError", message=u"cittá")
+        self.assertEqual(u"cittá", error.message)
+        self.assertEqual("citt?", str(error))

=== modified file 'txaws/server/tests/test_schema.py'
--- txaws/server/tests/test_schema.py	2011-10-14 12:42:48 +0000
+++ txaws/server/tests/test_schema.py	2011-12-01 14:46:14 +0000
@@ -1,3 +1,5 @@
+# -*- coding: utf-8 -*-
+
 from datetime import datetime
 
 from pytz import UTC, FixedOffset
@@ -102,6 +104,19 @@
         self.assertEqual("InvalidParameterValue", error.code)
         self.assertEqual("Invalid integer value foo", error.message)
 
+    def test_coerce_with_parameter_error_unicode(self):
+        """
+        L{Parameter.coerce} raises an L{APIError} if an invalid value is
+        passed as request argument and parameter value is unicode.
+        """
+        parameter = Parameter("Test")
+        parameter.parse = lambda value: int(value)
+        parameter.kind = "integer"
+        error = self.assertRaises(APIError, parameter.coerce, "citt\xc3\xa1")
+        self.assertEqual(400, error.status)
+        self.assertEqual("InvalidParameterValue", error.code)
+        self.assertEqual(u"Invalid integer value cittá", error.message)
+
     def test_coerce_with_empty_strings(self):
         """
         L{Parameter.coerce} returns C{None} if the value is an empty string and
@@ -180,6 +195,11 @@
         parameter = Unicode("Test")
         self.assertEqual(u"foo", parameter.parse("foo"))
 
+    def test_parse_unicode(self):
+        """L{Unicode.parse} works with unicode input."""
+        parameter = Unicode("Test")
+        self.assertEqual(u"cittá", parameter.parse("citt\xc3\xa1"))
+
     def test_format(self):
         """L{Unicode.format} encodes the given C{unicode} with utf-8."""
         parameter = Unicode("Test")