Move exceptions into their own module
authorKarl O. Pinc <kop@karlpinc.com>
Mon, 31 Aug 2020 22:01:26 +0000 (17:01 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Tue, 1 Sep 2020 13:24:47 +0000 (08:24 -0500)
src/pgwui_upload/check_settings.py
src/pgwui_upload/exceptions.py [new file with mode: 0644]
src/pgwui_upload/views/upload.py
tests/test_check_settings.py

index 330d9438bfe9435b36bcedfdfafa2819912328f1..1b725dd6ed89b275d96d0216046e6aac55944759 100644 (file)
@@ -19,8 +19,8 @@
 
 # Karl O. Pinc <kop@karlpinc.com>
 
-from pgwui_common import exceptions as common_ex
 from pgwui_common import checkset
+from . import exceptions as upload_ex
 
 
 PGWUI_COMPONENT = 'pgwui_upload'
@@ -31,17 +31,6 @@ REQUIRED_SETTINGS = []
 BOOLEAN_SETTINGS = []
 
 
-class UploadError(common_ex.Error):
-    pass
-
-
-class BadLiteralColumnHeadingsError(UploadError):
-    def __init__(self, value):
-        super().__init__(
-            'The "pgwui.literal_column_headings" PGWUI setting must be'
-            '"on", "off", "ask", or not present')
-
-
 def validate_literal_column_headings(errors, settings):
     '''Make sure the values are those allowed
     '''
@@ -49,7 +38,7 @@ def validate_literal_column_headings(errors, settings):
     if value is None:
         return
     if value not in ('on', 'off', 'ask'):
-        errors.append(BadLiteralColumnHeadingsError(value))
+        errors.append(upload_ex.BadLiteralColumnHeadingsError(value))
 
 
 def check_settings(component_config):
diff --git a/src/pgwui_upload/exceptions.py b/src/pgwui_upload/exceptions.py
new file mode 100644 (file)
index 0000000..d893ae8
--- /dev/null
@@ -0,0 +1,74 @@
+# Copyright (C) 2020 The Meme Factory, Inc.  http://www.karlpinc.com/
+
+# This file is part of PGWUI_Upload.
+#
+# This program is free software: you can redistribute it and/or
+# modify it under the terms of the GNU Affero General Public License
+# as published by the Free Software Foundation, either version 3 of
+# the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public
+# License along with this program.  If not, see
+# <http://www.gnu.org/licenses/>.
+#
+
+# Karl O. Pinc <kop@karlpinc.com>
+
+from pgwui_common import exceptions as common_ex
+from pgwui_core import exceptions as core_ex
+
+
+# PGWUI setting related exceptions
+
+class UploadError(common_ex.Error):
+    pass
+
+
+class BadLiteralColumnHeadingsError(UploadError):
+    def __init__(self, value):
+        super().__init__(
+            'The "pgwui.literal_column_headings" PGWUI setting must be'
+            '"on", "off", "ask", or not present')
+
+
+# Upload related exceptions
+
+class NoTableError(core_ex.PGWUIError):
+    '''No table uploaded'''
+    def __init__(self, e, descr='', detail=''):
+        super(NoTableError, self).__init__(e, descr, detail)
+
+
+class BadTableError(core_ex.PGWUIError):
+    '''Supplied name does not work for a table or view'''
+    def __init__(self, e, descr='', detail=''):
+        super(BadTableError, self).__init__(e, descr, detail)
+
+
+class MissingTableError(BadTableError):
+    '''The supplied table or view does not exist'''
+    def __init__(self, e, descr='', detail=''):
+        super(MissingTableError, self).__init__(e, descr, detail)
+
+
+class MissingSchemaError(BadTableError):
+    '''The schema portion of the supplied table or view does not exist'''
+    def __init__(self, e, descr='', detail=''):
+        super(MissingSchemaError, self).__init__(e, descr, detail)
+
+
+class CannotInsertError(BadTableError):
+    '''Cannot insert into the supplied table or view'''
+    def __init__(self, e, descr='', detail=''):
+        super(CannotInsertError, self).__init__(e, descr, detail)
+
+
+class BadHeadersError(core_ex.PGWUIError):
+    '''The headers in the uploaded file are bad.'''
+    def __init__(self, e, descr='', detail=''):
+        super(BadHeadersError, self).__init__(e, descr, detail)
index a0d63fa3ee6fb7271d00cd70828e8c5a5825f2da..b5718180d754a1ad6c897ad4a2e7c30166800f33 100644 (file)
@@ -36,7 +36,6 @@ import psycopg2.errorcodes
 from psycopg2 import ProgrammingError
 
 from pgwui_common import auth_base_view
-from pgwui_core.exceptions import PGWUIError
 from pgwui_core.core import (
     UploadEngine,
     DataLineProcessor,
@@ -48,43 +47,10 @@ from pgwui_core.core import (
     is_checked,
 )
 
-log = logging.getLogger(__name__)
-
-
-class NoTableError(PGWUIError):
-    '''No table uploaded'''
-    def __init__(self, e, descr='', detail=''):
-        super(NoTableError, self).__init__(e, descr, detail)
-
-
-class BadTableError(PGWUIError):
-    '''Supplied name does not work for a table or view'''
-    def __init__(self, e, descr='', detail=''):
-        super(BadTableError, self).__init__(e, descr, detail)
-
+from pgwui_upload import exceptions as upload_ex
 
-class MissingTableError(BadTableError):
-    '''The supplied table or view does not exist'''
-    def __init__(self, e, descr='', detail=''):
-        super(MissingTableError, self).__init__(e, descr, detail)
 
-
-class MissingSchemaError(BadTableError):
-    '''The schema portion of the supplied table or view does not exist'''
-    def __init__(self, e, descr='', detail=''):
-        super(MissingSchemaError, self).__init__(e, descr, detail)
-
-
-class CannotInsertError(BadTableError):
-    '''Cannot insert into the supplied table or view'''
-    def __init__(self, e, descr='', detail=''):
-        super(CannotInsertError, self).__init__(e, descr, detail)
-
-
-class BadHeadersError(PGWUIError):
-    '''The headers in the uploaded file are bad.'''
-    def __init__(self, e, descr='', detail=''):
-        super(BadHeadersError, self).__init__(e, descr, detail)
+log = logging.getLogger(__name__)
 
 
 class SaveLine(DataLineProcessor):
@@ -146,7 +112,8 @@ class TableUploadHandler(TabularFileUploadHandler):
 
         qualified_table = uf['table']
         if qualified_table == '':
-            errors.append(NoTableError('No table or view name supplied'))
+            errors.append(upload_ex.NoTableError(
+                'No table or view name supplied'))
 
         self.double_validator(errors)
 
@@ -173,11 +140,11 @@ class TableUploadHandler(TabularFileUploadHandler):
         except ProgrammingError as err:
             pgcode = err.pgcode
             if pgcode == psycopg2.errorcodes.INVALID_SCHEMA_NAME:
-                raise MissingSchemaError(
+                raise upload_ex.MissingSchemaError(
                     'No such schema',
                     err.diag.message_primary,)
             elif pgcode == psycopg2.errorcodes.UNDEFINED_TABLE:
-                raise MissingTableError(
+                raise upload_ex.MissingTableError(
                     'No such table or view',
                     err.diag.message_primary,
                     ('<p>Hint: Check spelling or try qualifying the'
@@ -236,7 +203,7 @@ class TableUploadHandler(TabularFileUploadHandler):
         schema, table = self.resolve_table(qualified_table)
 
         if not self.good_table(schema, table):
-            raise CannotInsertError(
+            raise upload_ex.CannotInsertError(
                 'Cannot insert into supplied table or view',
                 ('({0}) is either is a view'
                  ' that cannot be inserted into'
@@ -284,7 +251,7 @@ class TableUploadHandler(TabularFileUploadHandler):
             for bad_col in bad_cols:
                 detail += '<li>{0}</li>'.format(markupsafe.escape(bad_col))
             detail += '</ul>'
-            raise BadHeadersError(
+            raise upload_ex.BadHeadersError(
                 'Header line contains unknown column names',
                 detail=detail)
 
index 330508c666f41c30bf8ef80a49e43bad9f6af164..d6aac1d5e3d3d357634b943edf268107f06afa9e 100644 (file)
@@ -23,6 +23,7 @@ import pgwui_upload.check_settings as check_settings
 
 from pgwui_common import checkset
 from pgwui_testing import testing
+from pgwui_upload import exceptions as upload_ex
 
 # Activiate our pytest plugin
 pytest_plugins = ("pgwui",)
@@ -95,7 +96,7 @@ def test_validate_literal_column_headings_bad():
 
     assert errors
     assert isinstance(
-        errors[0], check_settings.BadLiteralColumnHeadingsError)
+        errors[0], upload_ex.BadLiteralColumnHeadingsError)
 
 
 literal_err = 'literal column headings error'