From 26b45b6dbae8aac1859ebfcd9cfdf618c1404199 Mon Sep 17 00:00:00 2001 From: Dominik Moritz Date: Thu, 28 Mar 2013 19:50:25 +0100 Subject: [PATCH] [#718] Move pg error codes in a separate dictionary --- ckanext/datastore/db.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/ckanext/datastore/db.py b/ckanext/datastore/db.py index 6be6c9fefd0..63b026969f7 100644 --- a/ckanext/datastore/db.py +++ b/ckanext/datastore/db.py @@ -31,15 +31,21 @@ def __init__(self, error_dict): _type_names = set() _engines = {} +# See http://www.postgresql.org/docs/9.2/static/errcodes-appendix.html +_pg_err_code = { + 'unique_violation': 23505, + 'query_canceled': 57014 +} + _date_formats = ['%Y-%m-%d', - '%Y-%m-%d %H:%M:%S', - '%Y-%m-%dT%H:%M:%S', - '%Y-%m-%dT%H:%M:%SZ', - '%d/%m/%Y', - '%m/%d/%Y', - '%d-%m-%Y', - '%m-%d-%Y', - ] + '%Y-%m-%d %H:%M:%S', + '%Y-%m-%dT%H:%M:%S', + '%Y-%m-%dT%H:%M:%SZ', + '%d/%m/%Y', + '%m/%d/%Y', + '%d-%m-%Y', + '%m-%d-%Y', + ] INSERT = 'insert' UPSERT = 'upsert' UPDATE = 'update' @@ -956,7 +962,7 @@ def create(context, data_dict): trans.commit() return _unrename_json_field(data_dict) except IntegrityError, e: - if int(e.orig.pgcode) == 23505: + if int(e.orig.pgcode) == _pg_err_code['unique_violation']: raise ValidationError({ 'constraints': ['Cannot insert records or create index because ' 'of uniqueness constraint'], @@ -966,7 +972,7 @@ def create(context, data_dict): }) raise except DBAPIError, e: - if int(e.orig.pgcode) == 57014: + if int(e.orig.pgcode) == _pg_err_code['query_canceled']: raise ValidationError({ 'query': ['Query took too long'] }) @@ -999,7 +1005,7 @@ def upsert(context, data_dict): trans.commit() return _unrename_json_field(data_dict) except IntegrityError, e: - if int(e.orig.pgcode) == 23505: + if int(e.orig.pgcode) == _pg_err_code['unique_violation']: raise ValidationError({ 'constraints': ['Cannot insert records or create index because ' 'of uniqueness constraint'], @@ -1009,7 +1015,7 @@ def upsert(context, data_dict): }) raise except DBAPIError, e: - if int(e.orig.pgcode) == 57014: + if int(e.orig.pgcode) == _pg_err_code['query_canceled']: raise ValidationError({ 'query': ['Query took too long'] }) @@ -1076,7 +1082,7 @@ def search(context, data_dict): }) return search_data(context, data_dict) except DBAPIError, e: - if int(e.orig.pgcode) == 57014: + if int(e.orig.pgcode) == _pg_err_code['query_canceled']: raise ValidationError({ 'query': ['Search took too long'] }) @@ -1109,7 +1115,7 @@ def search_sql(context, data_dict): } }) except DBAPIError, e: - if int(e.orig.pgcode) == 57014: + if int(e.orig.pgcode) == _pg_err_code['query_canceled']: raise ValidationError({ 'query': ['Query took too long'] })