-
Notifications
You must be signed in to change notification settings - Fork 2
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
Added error handling #18
Open
alireza1111
wants to merge
1
commit into
borsna:main
Choose a base branch
from
alireza1111:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,14 @@ | ||
class ResolveError(ValueError): | ||
pass | ||
def __init__(self, message, http_response_code=None): | ||
super().__init__(message) | ||
self.http_response_code = http_response_code | ||
|
||
class RepoError(Exception): | ||
pass | ||
class RepoError(ValueError): | ||
def __init__(self, message, url, supported_urls=None, http_response_code=None): | ||
super().__init__(message) | ||
self.url = url | ||
self.supported_urls = supported_urls or ["dataverse.harvard.edu", "dataverse.no", "snd.se/catalogue", "su.figshare.com", "figshare.scilifelab.se", "zenodo.org"] | ||
self.http_response_code = http_response_code | ||
|
||
if url not in self.supported_urls: | ||
raise self |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,39 @@ | ||
import re | ||
import socket | ||
import urllib, urllib.error | ||
from daget.exceptions import RepoError, ResolveError | ||
|
||
|
||
def get_redirect_url(url): | ||
# if url provided is a shorthand doi (TODO: check with regex) | ||
if not url.startswith(('http://', 'https://')): | ||
if not re.match(r'^https?://', url): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
url = 'https://doi.org/' + url | ||
|
||
opener = urllib.request.build_opener() | ||
opener.addheaders = [('User-Agent', 'daget')] | ||
urllib.request.install_opener(opener) | ||
try: | ||
r = urllib.request.urlopen(url) | ||
return r.geturl() | ||
except urllib.error.HTTPError: | ||
raise ResolveError(f"{url} not found") | ||
|
||
try: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. looks good, giving more precise errors is good |
||
response = urllib.request.urlopen(url) | ||
return response.geturl() | ||
|
||
except urllib.error.HTTPError as e: | ||
# Catch HTTP errors and extract relevant information | ||
error_message = f"HTTPError: {e.code} - {e.reason}" | ||
raise ResolveError(f"{url} not found. {error_message}") | ||
|
||
except urllib.error.URLError as e: | ||
# Catch URL errors (e.g., network issues) and provide relevant information | ||
if isinstance(e.reason, str): | ||
error_message = f"URLError: {e.reason}" | ||
else: | ||
error_message = f"URLError: {str(e.reason)}" | ||
|
||
# Additional handling for socket.gaierror | ||
if isinstance(e.reason, socket.gaierror): | ||
error_message += f", errno: {e.reason.errno}, strerror: {e.reason.strerror}" | ||
|
||
raise ResolveError(f"Error connecting to {url}. {error_message}") | ||
|
||
def download_file(url, target): | ||
opener = urllib.request.build_opener() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hardcoded list of repository url:s should be removed.
daget should try to get a file list via schema.org distribution (if it´s not figshare or zenodo) and if this fails it should throw the error instead. keeping a list of all suported url:s in the source coude is not a sustainable soultion