Re: [pgAdmin4][Patch]: To decode database errors properly

2017-11-27 Thread Dave Page
Thanks, applied.

On Mon, Nov 27, 2017 at 6:33 AM, Murtuza Zabuawala <
murtuza.zabuaw...@enterprisedb.com> wrote:

> Hi,
>
> PFA patch to fix the issue where if the database server is installed on
> the windows system then in most cases the parameter 'lc_messages' has
> environment dependent encoding.
> RM#2806
> RM#2821
>
> --
> Regards,
> Murtuza Zabuawala
> EnterpriseDB: http://www.enterprisedb.com
> The Enterprise PostgreSQL Company
>
>


-- 
Dave Page
Blog: http://pgsnake.blogspot.com
Twitter: @pgsnake

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company


[pgAdmin4][Patch]: To decode database errors properly

2017-11-26 Thread Murtuza Zabuawala
Hi,

PFA patch to fix the issue where if the database server is installed on the
windows system then in most cases the parameter 'lc_messages' has
environment dependent encoding.
RM#2806
RM#2821

--
Regards,
Murtuza Zabuawala
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
diff --git a/web/pgadmin/utils/driver/psycopg2/__init__.py 
b/web/pgadmin/utils/driver/psycopg2/__init__.py
index 48a7c93..ece1d12 100644
--- a/web/pgadmin/utils/driver/psycopg2/__init__.py
+++ b/web/pgadmin/utils/driver/psycopg2/__init__.py
@@ -1589,11 +1589,29 @@ Failed to reset the connection to the server due to 
following error:
 Returns:
 Decoded string
 """
+is_error = False
 if hasattr(str, 'decode'):
 try:
 value = value.decode('utf-8')
+except UnicodeDecodeError:
+# Let's try with python's preferred encoding
+# On Windows lc_messages mostly has environment dependent
+# encoding like 'French_France.1252'
+try:
+import locale
+pref_encoding = locale.getpreferredencoding()
+value = value.decode(pref_encoding)\
+.encode('utf-8')\
+.decode('utf-8')
+except:
+is_error = True
 except:
-pass
+is_error = True
+
+# If still not able to decode then
+if is_error:
+value = value.decode('ascii', 'ignore')
+
 return value
 
 def _formatted_exception_msg(self, exception_obj, formatted_msg):