Trap and do basic reporting on encoding exceptions, both client and server-side
authorKarl O. Pinc <kop@karlpinc.com>
Tue, 5 Mar 2024 21:30:06 +0000 (15:30 -0600)
committerKarl O. Pinc <kop@karlpinc.com>
Tue, 5 Mar 2024 21:30:06 +0000 (15:30 -0600)
setup.py
src/pgwui_copy/views/copy.py

index 4461e120d13944f18e65ec1b85df3a0581e9ec57..9fbfc22b3c3cda257f3a8e1fd748802544cbc905 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -148,6 +148,7 @@ setup(
     install_requires=[
         'markupsafe',
         'pgwui_common==' + version,
+        'psycopg',
         'pyramid',
         'attrs',
     ],
index 42b82aff3c2570042c3b21c25e6516799c292984..ade541442fe873132763ba2f74f8f790f8eb6167 100644 (file)
@@ -21,6 +21,7 @@
 import attr
 
 import logging
+import psycopg.errors
 import subprocess
 import tempfile
 
@@ -147,9 +148,25 @@ class CopySchemaForm(CredsLoadedForm):
 
 def schema_exists(cur, schema):
     '''Does the schema exist?'''
-    cur.execute('SELECT 1 FROM pg_namespace'
-                '  WHERE nspname = %s',
-                (schema,))
+    try:
+        cur.execute('SELECT 1 FROM pg_namespace'
+                    '  WHERE nspname = %s',
+                    (schema,))
+    except UnicodeEncodeError as ex:
+        raise copy_ex.InvalidSchemaError(
+            ex,
+            ("Data cannot be represented in the database"
+             " connection's client-side character encoding"),
+            (f'The schema name ({schema}) has character(s) that cannot'
+             ' be sent to the server'))
+    except psycopg.errors.UntranslatableCharacter as ex:
+        raise copy_ex.InvalidSchemaError(
+            ex,
+            ("Data cannot be represented in the"
+             " character encoding of the database"),
+            (f'The schema name ({schema}) has character(s) that cannot'
+             ' be represented in the database'))
+
     return cur.fetchone() is not None