Permalink
Browse files

Carpet bombing exception handling is bad. You don't know if you have …

…run into a different error.

It's not a good idea to exit while in a function. Return error values and exit out of main, or some other "main"ish place. Functions should mutate data. They should not kill the program 2 stacks deep.

Also, where did the retry logic go?
  • Loading branch information...
1 parent cec546c commit 47ec6ce19eb61ac218689ddc762bf9796dde11ea @uberj uberj committed Nov 6, 2011
Showing with 8 additions and 4 deletions.
  1. +8 −4 soundcloud-dl
View
@@ -22,14 +22,16 @@ def open_url(url):
try:
request = urllib2.Request(options.url, headers=header_values)
response = opener.open(request)
- html = response.read()
- break
- except:
+ except urllib2.HTTPError, e:
retry += 1
time.sleep(1)
+ except ValueError, e:
+ print str(e)
+ return None
+ html = response.read()
if not html:
print "failed to fetch url"
- sys.exit()
+ return None
return html
def get_stream_token_uid(page):
@@ -71,6 +73,8 @@ if __name__ == '__main__':
# open up initial page to get stream token, uid, song title
html = open_url(options.url)
+ if not html:
+ sys.exit(1)
(uid, token) = get_stream_token_uid(html)
song_title = get_song_title(html) + '.mp3'

0 comments on commit 47ec6ce

Please sign in to comment.