Skip to content

Commit

Permalink
Merge pull request #16 from datopian/fix/connection-error-on-resource…
Browse files Browse the repository at this point in the history
…-request

Catch unhandled errors when loading a resource from a URL
  • Loading branch information
MuhammadIsmailShahzad authored Apr 30, 2021
2 parents 6ef09e0 + eb5f5f3 commit 0b9a381
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions ckanext/opendatani/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,15 @@ def resource_read(self, id, resource_id):
'dataset_type': dataset_type}
print MAX_FILE_SIZE
if c.resource['url_type'] != 'upload':
r = requests.head(c.resource['url'])
if r.status_code not in (400, 403, 405) and r.ok:
size = r.headers.get('content-length')
if size and int(size) > MAX_FILE_SIZE:
h.flash_error('Sorry, this file is too large to be able to display in the browser, please download the data resource to examine it further.')
try:
r = requests.head(c.resource['url'])
if r.status_code not in (400, 403, 405) and r.ok:
size = r.headers.get('content-length')
if size and int(size) > MAX_FILE_SIZE:
h.flash_error('Sorry, this file is too large to be able to display in the browser, please download the data resource to examine it further.')
except Exception as e:
print(e)
h.flash_error('Unable to get resource')
template = self._resource_template(dataset_type)
return render(template, extra_vars=vars)

Expand Down

0 comments on commit 0b9a381

Please sign in to comment.