Skip to content

Commit

Permalink
Merge pull request #473 from okfn/473-consistent-api-validation
Browse files Browse the repository at this point in the history
Action API is inconsistent
  • Loading branch information
johnglover committed Jun 13, 2013
2 parents 0e96f74 + fb7e336 commit 349e1cb
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 101 deletions.
33 changes: 17 additions & 16 deletions ckan/controllers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,11 @@ def action(self, logic_function, ver=None):
return_dict['result'] = result
except DataError, e:
log.error('Format incorrect: %s - %s' % (e.error, request_data))
#TODO make better error message
return self._finish(400, _(u'Integrity Error') +
': %s - %s' % (e.error, request_data))
return_dict['error'] = {'__type': 'Integrity Error',
'message': e.error,
'data': request_data}
return_dict['success'] = False
return self._finish(400, return_dict, content_type='json')
except NotAuthorized:
return_dict['error'] = {'__type': 'Authorization Error',
'message': _('Access denied')}
Expand All @@ -210,13 +212,6 @@ def action(self, logic_function, ver=None):
return_dict['success'] = False
log.error('Validation error: %r' % str(e.error_dict))
return self._finish(409, return_dict, content_type='json')
except logic.ParameterError, e:
return_dict['error'] = {'__type': 'Parameter Error',
'message': '%s: %s' %
(_('Parameter Error'), e.extra_msg)}
return_dict['success'] = False
log.error('Parameter error: %r' % e.extra_msg)
return self._finish(409, return_dict, content_type='json')
except search.SearchQueryError, e:
return_dict['error'] = {'__type': 'Search Query Error',
'message': 'Search Query is invalid: %r' %
Expand Down Expand Up @@ -366,9 +361,12 @@ def create(self, ver=None, register=None, subregister=None,
return self._finish(409, e.error_dict, content_type='json')
except DataError, e:
log.error('Format incorrect: %s - %s' % (e.error, request_data))
#TODO make better error message
return self._finish(400, _(u'Integrity Error') +
': %s - %s' % (e.error, request_data))
error_dict = {
'success': False,
'error': {'__type': 'Integrity Error',
'message': e.error,
'data': request_data}}
return self._finish(400, error_dict, content_type='json')
except search.SearchIndexError:
log.error('Unable to add package to search index: %s' %
request_data)
Expand Down Expand Up @@ -418,9 +416,12 @@ def update(self, ver=None, register=None, subregister=None,
return self._finish(409, e.error_dict, content_type='json')
except DataError, e:
log.error('Format incorrect: %s - %s' % (e.error, request_data))
#TODO make better error message
return self._finish(400, _(u'Integrity Error') +
': %s - %s' % (e.error, request_data))
error_dict = {
'success': False,
'error': {'__type': 'Integrity Error',
'message': e.error,
'data': request_data}}
return self._finish(400, error_dict, content_type='json')
except search.SearchIndexError:
log.error('Unable to update search index: %s' % request_data)
return self._finish(500, _(u'Unable to update search index') %
Expand Down
4 changes: 2 additions & 2 deletions ckan/lib/email_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def string_to_timedelta(s):
7 days, 3:23:34.087465
.087465 (microseconds only)
:raises ckan.logic.ParameterError: if the given string does not match any
:raises ckan.logic.ValidationError: if the given string does not match any
of the recognised formats
'''
Expand All @@ -56,7 +56,7 @@ def string_to_timedelta(s):
break

if not match:
raise logic.ParameterError('Not a valid time: {0}'.format(s))
raise logic.ValidationError('Not a valid time: {0}'.format(s))

gd = match.groupdict()
days = int(gd.get('days', '0'))
Expand Down
2 changes: 1 addition & 1 deletion ckan/lib/navl/dictization_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def augment_data(data, schema):

full_schema = make_full_schema(data, schema)

new_data = copy.deepcopy(data)
new_data = copy.copy(data)

## fill junk and extras

Expand Down
26 changes: 13 additions & 13 deletions ckan/logic/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from ckan.common import _, c

log = logging.getLogger(__name__)
_validate = df.validate


class AttributeDict(dict):
Expand Down Expand Up @@ -46,13 +47,11 @@ class NotAuthorized(ActionError):
pass


class ParameterError(ActionError):
pass


class ValidationError(ParameterError):
class ValidationError(ActionError):

def __init__(self, error_dict, error_summary=None, extra_msg=None):
if not isinstance(error_dict, dict):
error_dict = {'message': error_dict}
# tags errors are a mess so let's clean them up
if 'tags' in error_dict:
tag_errors = []
Expand Down Expand Up @@ -352,19 +351,20 @@ def get_or_bust(data_dict, keys):
e.g single_value = get_or_bust(data_dict, 'a_key')
value_1, value_2 = get_or_bust(data_dict, ['key1', 'key2'])
'''
values = []
errors = {}

if isinstance(keys, basestring):
keys = [keys]
for key in keys:
try:
value = data_dict[key]
values.append(value)
except KeyError:
errors[key] = _('Missing value')

import ckan.logic.schema as schema
schema = schema.create_schema_for_required_keys(keys)

data_dict, errors = _validate(data_dict, schema)

if errors:
raise ValidationError(errors)

# preserve original key order
values = [data_dict[key] for key in keys]
if len(values) == 1:
return values[0]
return tuple(values)
Expand Down
Loading

0 comments on commit 349e1cb

Please sign in to comment.