server/func: handle download errors

This commit is contained in:
rr- 2016-09-25 14:52:47 +02:00
parent 4f497d311a
commit 71a4ce8764
2 changed files with 7 additions and 2 deletions

1
API.md
View file

@ -169,6 +169,7 @@ List of possible error names:
- `InvalidPasswordError`
- `InvalidRankError`
- `InvalidAvatarError`
- `ProcessingError` (failed to generate thumbnail or download remote file)
- `ValidationError` (catch all for odd validation errors)

View file

@ -1,9 +1,13 @@
import urllib.request
from szurubooru import errors
def download(url):
assert url
request = urllib.request.Request(url)
request.add_header('Referer', url)
with urllib.request.urlopen(request) as handle:
return handle.read()
try:
with urllib.request.urlopen(request) as handle:
return handle.read()
except Exception as e:
raise errors.ProcessingError('Error downloading %s (%s)' % (url, e))