From eb5f5f3e45becf8b5ec36af8e73df92eb5fb6d3d Mon Sep 17 00:00:00 2001 From: Isaac Aderonmu Date: Fri, 30 Apr 2021 09:51:56 +0100 Subject: [PATCH] [fix] [s]: add try except block to catch errors on resource request --- ckanext/opendatani/controller.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/ckanext/opendatani/controller.py b/ckanext/opendatani/controller.py index aeed2fd..0d0022a 100644 --- a/ckanext/opendatani/controller.py +++ b/ckanext/opendatani/controller.py @@ -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)