From bcd6ff2ae272de35b36cb33f955a1c9f3c24f815 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Tue, 5 Mar 2024 15:30:06 -0600 Subject: [PATCH] Trap and do basic reporting on encoding exceptions, both client and server-side --- setup.py | 1 + src/pgwui_copy/views/copy.py | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) 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 -- 2.34.1