Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch error in datastore #8149

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions changes/8149.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Catch an error in datastore to avoid 500 error in POSTs to `datatables/ajax/<resource_view_id>`
The `datastore_search` action raises this error, it's captured later in this function but not here.
32 changes: 23 additions & 9 deletions ckanext/datatablesview/blueprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@

from ckan.common import json
from ckan.lib.helpers import decode_view_request_filters
from ckan.plugins.toolkit import get_action, request, h
from ckan.plugins.toolkit import (
get_action,
h,
NotAuthorized,
ObjectNotFound,
request,
)
import re

datatablesview = Blueprint(u'datatablesview', __name__)
Expand Down Expand Up @@ -59,13 +65,18 @@ def ajax(resource_view_id: str):
filters = merge_filters(view_filters, user_filters)

datastore_search = get_action(u'datastore_search')
unfiltered_response = datastore_search(
{}, {
u"resource_id": resource_view[u'resource_id'],
u"limit": 0,
u"filters": view_filters,
}
)
try:
unfiltered_response = datastore_search(
{}, {
u"resource_id": resource_view[u'resource_id'],
u"limit": 0,
u"filters": view_filters,
}
)
except ObjectNotFound:
return json.dumps({'error': 'Object not found'}), 404
except NotAuthorized:
return json.dumps({'error': 'Not Authorized'}), 403

cols = [f[u'id'] for f in unfiltered_response[u'fields']]
if u'show_fields' in resource_view:
Expand Down Expand Up @@ -120,6 +131,7 @@ def ajax(resource_view_id: str):
except Exception:
query_error = u'Invalid search query... ' + search_text
dtdata = {u'error': query_error}
status = 400
else:
data = []
null_label = h.datatablesview_null_label()
Expand All @@ -137,8 +149,10 @@ def ajax(resource_view_id: str):
u'recordsFiltered': response.get(u'total', 0),
u'data': data
}
status = 200

return json.dumps(dtdata)
# return the response as JSON with status
return json.dumps(dtdata), status


def filtered_download(resource_view_id: str):
Expand Down