Skip to content

Commit

Permalink
[#718] Move pg error codes in a separate dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz committed Mar 28, 2013
1 parent 2ceae03 commit f947fbb
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions ckanext/datastore/db.py
Expand Up @@ -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'
Expand Down Expand Up @@ -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'],
Expand All @@ -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']
})
Expand Down Expand Up @@ -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'],
Expand All @@ -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']
})
Expand Down Expand Up @@ -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']
})
Expand Down Expand Up @@ -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']
})
Expand Down

0 comments on commit f947fbb

Please sign in to comment.