Repository: chezou/tabula-py
Pull request #113 (2018-09-23 06:07:55)

Improve error messages


Patch:
diff --git a/tabula/errors/__init__.py b/tabula/errors/__init__.py
new file mode 100644
index 0000000..6a92bed
--- /dev/null
+++ b/tabula/errors/__init__.py
@@ -0,0 +1,11 @@
+from pandas.errors import ParserError
+
+
+class CSVParseError(ParserError):
+    def __init__(self, message, cause):
+        super(CSVParseError, self).__init__(message + ', caused by ' + repr(cause))
+        self.cause = cause
+
+
+class JavaNotFoundError(Exception):
+    pass
diff --git a/tabula/wrapper.py b/tabula/wrapper.py
index 0f2c78f..0734e1a 100644
--- a/tabula/wrapper.py
+++ b/tabula/wrapper.py
@@ -21,6 +21,7 @@ import sys
 import errno
 
 from .util import deprecated_option
+from .errors import CSVParseError, JavaNotFoundError
 from .file_util import localize_file
 from .template import load_template
 
@@ -107,12 +108,10 @@ def read_pdf(input_path,
         output = subprocess.check_output(args)
 
     except FileNotFoundError as e:
-        sys.stderr.write("Error: {}".format(e))
-        sys.stderr.write("Error: {}".format(JAVA_NOT_FOUND_ERROR))
-        raise
+        raise JavaNotFoundError(JAVA_NOT_FOUND_ERROR)
 
     except subprocess.CalledProcessError as e:
-        sys.stderr.write("Error: {}".format(e.output.decode(encoding)))
+        sys.stderr.write("Error: {}\n".format(e.output.decode(encoding)))
         raise
 
     finally:
@@ -140,9 +139,10 @@ def read_pdf(input_path,
             return pd.read_csv(io.BytesIO(output), **pandas_options)
 
         except pd.errors.ParserError as e:
-            sys.stderr.write("Error: Failed to create DataFrame with different column tables.\n")
-            sys.stderr.write("Error: Try to set `multiple_tables=True`.\n")
-            raise
+            message = "Error failed to create DataFrame with different column tables.\n"
+            message += "Try to set `multiple_tables=True` or set `names` option for `pandas_options`. \n"
+
+            raise CSVParseError(message, e)
 
 
 def read_pdf_with_template(
@@ -232,9 +232,7 @@ def convert_into(input_path, output_path, output_format='csv', java_options=None
         subprocess.check_output(args)
 
     except FileNotFoundError as e:
-        sys.stderr.write("Error: {}\n".format(e))
-        sys.stderr.write("Error: {}\n".format(JAVA_NOT_FOUND_ERROR))
-        raise
+        raise JavaNotFoundError(JAVA_NOT_FOUND_ERROR)
 
     except subprocess.CalledProcessError as e:
         sys.stderr.write("Error: {}\n".format(e.output))
@@ -284,12 +282,10 @@ def convert_into_by_batch(input_dir, output_format='csv', java_options=None, **k
         subprocess.check_output(args)
 
     except FileNotFoundError as e:
-        print("Error: {}".format(e))
-        print("Error: {}".format(JAVA_NOT_FOUND_ERROR))
-        raise
+        raise JavaNotFoundError(JAVA_NOT_FOUND_ERROR)
 
     except subprocess.CalledProcessError as e:
-        print("Error: {}".format(e.output))
+        sys.stderr.write("Error: {}\n".format(e.output))
         raise
 
 


Test patch:
diff --git a/tests/test_read_pdf_table.py b/tests/test_read_pdf_table.py
index e82f559..0665909 100644
--- a/tests/test_read_pdf_table.py
+++ b/tests/test_read_pdf_table.py
@@ -115,7 +115,7 @@ class TestReadPdfTable(unittest.TestCase):
         self.assertEqual(len(tabula.read_pdf(pdf_path, pages=2, multiple_tables=True)), 2)
         self.assertTrue(tabula.read_pdf(pdf_path, pages=1, multiple_tables=True)[0].equals(
             pd.read_csv(expected_csv1, header=None)))
-        with self.assertRaises(pd.errors.ParserError):
+        with self.assertRaises(tabula.errors.CSVParseError):
             tabula.read_pdf(pdf_path, pages=2)
 
     def test_read_pdf_exception(self):