Repository: ckan/ckan

Issue:
KeyError 'resources' in error handler
https://github.com/ckan/ckan/blob/master/ckan/logic/action/create.py#L305

Extension-code dependent, it can happen that a perfectly normal validation error leads to this block and CKAN throws an exception concerning a missing resources key when there is no resources key in the dictionary. This can lead to serious confusion for a developer.

KeyError 'resources' in error handler
https://github.com/ckan/ckan/blob/master/ckan/logic/action/create.py#L305

Extension-code dependent, it can happen that a perfectly normal validation error leads to this block and CKAN throws an exception concerning a missing resources key when there is no resources key in the dictionary. This can lead to serious confusion for a developer.



Discussion:
I went with a hacky patch for this; if there's not a resource error, there must be a package validation error so assume that and dump the error dict for good measure https://github.com/datagovau/ckanext-agls/blob/master/patches/resource-edit-error.patch
At least it doesn't just Error 500 on the user then.

Yes I've run into this too.

What we need is to check for errors outside that last resource and include a message something like what @maxious suggests. "Can't save resource because of validation errors in the dataset/other resource fields: {field_names}"

Anyone care to submit a PR?

Ugh, I just hit this, it's pretty horrible. PR incoming

PR here https://github.com/ckan/ckan/issues/3027

I went with a hacky patch for this; if there's not a resource error, there must be a package validation error so assume that and dump the error dict for good measure https://github.com/datagovau/ckanext-agls/blob/master/patches/resource-edit-error.patch
At least it doesn't just Error 500 on the user then.

Yes I've run into this too.

What we need is to check for errors outside that last resource and include a message something like what @maxious suggests. "Can't save resource because of validation errors in the dataset/other resource fields: {field_names}"

Anyone care to submit a PR?

Ugh, I just hit this, it's pretty horrible. PR incoming

PR here https://github.com/ckan/ckan/issues/3027


Resolving patch:
diff --git a/ckan/logic/action/create.py b/ckan/logic/action/create.py
--- a/ckan/logic/action/create.py
+++ b/ckan/logic/action/create.py
@@ -300,8 +300,10 @@ def resource_create(context, data_dict):
         _get_action('package_update')(context, pkg_dict)
         context.pop('defer_commit')
     except ValidationError, e:
-        errors = e.error_dict['resources'][-1]
-        raise ValidationError(errors)
+        try:
+            raise ValidationError(e.error_dict['resources'][-1])
+        except (KeyError, IndexError):
+            raise ValidationError(e.error_dict)
 
     ## Get out resource_id resource from model as it will not appear in
     ## package_show until after commit
diff --git a/ckan/logic/action/update.py b/ckan/logic/action/update.py
--- a/ckan/logic/action/update.py
+++ b/ckan/logic/action/update.py
@@ -98,8 +98,10 @@ def resource_update(context, data_dict):
         updated_pkg_dict = _get_action('package_update')(context, pkg_dict)
         context.pop('defer_commit')
     except ValidationError, e:
-        errors = e.error_dict['resources'][n]
-        raise ValidationError(errors)
+        try:
+            raise ValidationError(e.error_dict['resources'][-1])
+        except (KeyError, IndexError):
+            raise ValidationError(e.error_dict)
 
     upload.upload(id, uploader.get_max_resource_size())
     model.repo.commit()


Test patch: