Simplify exception handling

This commit is contained in:
Andrea Grandi 2018-10-30 09:52:38 +00:00
parent 6b6458f2ea
commit 6f82f19049
No known key found for this signature in database
GPG Key ID: F550DA7E2698006C
1 changed files with 3 additions and 16 deletions

View File

@ -1,16 +1,12 @@
import requests import requests
from requests.exceptions import HTTPError from requests.exceptions import RequestException
from requests.exceptions import InvalidURL
import httplib
import datetime import datetime
import socket
from urllib3.contrib import pyopenssl from urllib3.contrib import pyopenssl
from ckan import model from ckan import model
from ckan.logic import ValidationError, NotFound, get_action from ckan.logic import ValidationError, NotFound, get_action
from ckan.lib.helpers import json from ckan.lib.helpers import json
from ckan.lib.munge import munge_name
from ckan.plugins import toolkit from ckan.plugins import toolkit
from ckanext.harvest.model import HarvestObject from ckanext.harvest.model import HarvestObject
@ -46,17 +42,8 @@ class CKANHarvester(HarvesterBase):
try: try:
http_request = requests.get(url, headers=headers) http_request = requests.get(url, headers=headers)
except HTTPError as e: except RequestException as e:
if e.response.status_code == 404: raise ContentFetchError('HTTP error: %s' % e.code)
raise ContentNotFoundError('HTTP error: %s' % e.code)
else:
raise ContentFetchError('HTTP error: %s' % e.code)
except InvalidURL as e:
raise ContentFetchError('URL error: %s' % e.reason)
except httplib.HTTPException as e:
raise ContentFetchError('HTTP Exception: %s' % e)
except socket.error as e:
raise ContentFetchError('HTTP socket error: %s' % e)
except Exception as e: except Exception as e:
raise ContentFetchError('HTTP general exception: %s' % e) raise ContentFetchError('HTTP general exception: %s' % e)
return http_request.text return http_request.text