You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by mt...@apache.org on 2016/02/06 19:35:09 UTC

avro git commit: AVRO-1793. Python2: Retain original exception on schema parsing failure.

Repository: avro
Updated Branches:
  refs/heads/master 499b756e7 -> a50068f11


AVRO-1793. Python2: Retain original exception on schema parsing failure.

Contributed by Jakob Homan (jghoman).

Previously, when a parse call failed the original exception was
swallowed and replaced by a generic SchemaParseException. The original
stack trace is not retained along with the actual exception message.


Project: http://git-wip-us.apache.org/repos/asf/avro/repo
Commit: http://git-wip-us.apache.org/repos/asf/avro/commit/a50068f1
Tree: http://git-wip-us.apache.org/repos/asf/avro/tree/a50068f1
Diff: http://git-wip-us.apache.org/repos/asf/avro/diff/a50068f1

Branch: refs/heads/master
Commit: a50068f11e121bf87fa8bc24639af0fc62064633
Parents: 499b756
Author: Matthieu Monsch <mo...@alum.mit.edu>
Authored: Sat Feb 6 09:58:42 2016 -0800
Committer: Matthieu Monsch <mt...@apache.org>
Committed: Sat Feb 6 10:34:33 2016 -0800

----------------------------------------------------------------------
 CHANGES.txt                 |  3 +++
 lang/py/src/avro/schema.py  |  9 +++++----
 lang/py/test/test_schema.py | 14 ++++++++++++++
 3 files changed, 22 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/avro/blob/a50068f1/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 110f9c3..47d99bc 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -10,6 +10,9 @@ Trunk (not yet released)
 
   IMPROVEMENTS
 
+    AVRO-1793. Python2: Retain stack trace and original exception when failing
+    to parse schemas. Contributed by Jakob Homan (jghoman).
+
   BUG FIXES
 
 

http://git-wip-us.apache.org/repos/asf/avro/blob/a50068f1/lang/py/src/avro/schema.py
----------------------------------------------------------------------
diff --git a/lang/py/src/avro/schema.py b/lang/py/src/avro/schema.py
index f946d0a..6a7fbbb 100644
--- a/lang/py/src/avro/schema.py
+++ b/lang/py/src/avro/schema.py
@@ -443,7 +443,7 @@ class EnumSchema(NamedSchema):
       fail_msg = 'Enum Schema requires a JSON array for the symbols property.'
       raise AvroException(fail_msg)
     elif False in [isinstance(s, basestring) for s in symbols]:
-      fail_msg = 'Enum Schems requires All symbols to be JSON strings.'
+      fail_msg = 'Enum Schema requires all symbols to be JSON strings.'
       raise AvroException(fail_msg)
     elif len(set(symbols)) < len(symbols):
       fail_msg = 'Duplicate symbol: %s' % symbols
@@ -770,12 +770,13 @@ def make_avsc_object(json_data, names=None):
 # TODO(hammer): make method for reading from a file?
 def parse(json_string):
   """Constructs the Schema from the JSON text."""
-  # TODO(hammer): preserve stack trace from JSON parse
   # parse the JSON
   try:
     json_data = json.loads(json_string)
-  except:
-    raise SchemaParseException('Error parsing JSON: %s' % json_string)
+  except Exception, e:
+    import sys
+    raise SchemaParseException('Error parsing JSON: %s, error = %s'
+                               % (json_string, e)), None, sys.exc_info()[2]
 
   # Initialize the names object
   names = Names()

http://git-wip-us.apache.org/repos/asf/avro/blob/a50068f1/lang/py/test/test_schema.py
----------------------------------------------------------------------
diff --git a/lang/py/test/test_schema.py b/lang/py/test/test_schema.py
index 204d1b1..00e2a05 100644
--- a/lang/py/test/test_schema.py
+++ b/lang/py/test/test_schema.py
@@ -477,5 +477,19 @@ class TestSchema(unittest.TestCase):
         self.assertEqual(type(v), list)
     self.assertEqual(correct,len(OTHER_PROP_EXAMPLES))
 
+  def test_exception_is_not_swallowed_on_parse_error(self):
+    print_test_name('TEST EXCEPTION NOT SWALLOWED ON PARSE ERROR')
+
+    try:
+        schema.parse('/not/a/real/file')
+        caught_exception = False
+    except schema.SchemaParseException, e:
+        expected_message = 'Error parsing JSON: /not/a/real/file, error = ' \
+                           'No JSON object could be decoded'
+        self.assertEqual(expected_message, e.args[0])
+        caught_exception = True
+
+    self.assertTrue(caught_exception, 'Exception was not caught')
+
 if __name__ == '__main__':
   unittest.main()