From: Karl O. Pinc Date: Tue, 5 Mar 2024 21:30:06 +0000 (-0600) Subject: Trap and do basic reporting on encoding exceptions, both client and server-side X-Git-Url: https://papio.biology.duke.edu/gitweb/?a=commitdiff_plain;h=bcd6ff2ae272de35b36cb33f955a1c9f3c24f815;p=pgwui_copy Trap and do basic reporting on encoding exceptions, both client and server-side --- diff --git a/setup.py b/setup.py index 4461e12..9fbfc22 100644 --- a/setup.py +++ b/setup.py @@ -148,6 +148,7 @@ setup( install_requires=[ 'markupsafe', 'pgwui_common==' + version, + 'psycopg', 'pyramid', 'attrs', ], diff --git a/src/pgwui_copy/views/copy.py b/src/pgwui_copy/views/copy.py index 42b82af..ade5414 100644 --- a/src/pgwui_copy/views/copy.py +++ b/src/pgwui_copy/views/copy.py @@ -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