[Python-bugs-list] [ python-Bugs-662099 ] urllib2 exceptions need improvement

SourceForge.net noreply@sourceforge.net
Sun, 05 Jan 2003 03:16:45 -0800


Bugs item #662099, was opened at 2003-01-04 13:00
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=662099&group_id=5470

Category: Python Library
Group: Python 2.2.2
Status: Open
Resolution: None
Priority: 5
Submitted By: Thomas Viehmann (t-v)
Assigned to: Nobody/Anonymous (nobody)
Summary: urllib2 exceptions need improvement

Initial Comment:
Hi.

Thanks for urllib2.
Urllib2 would be even better if Exceptions were more
unified, i.e. if there was way to test any URLException
for common failure causes, regardless of the protocol.
Example: 'File not found' can happen with
http/ftp/gopher/...

Cheers

Thomas

----------------------------------------------------------------------

>Comment By: Thomas Viehmann (t-v)
Date: 2003-01-05 12:16

Message:
Logged In: YES 
user_id=680463

Maybe I was unclear (and maybe it's possible already to do
what I want but I'm just to blind to see it). So here is
what I want:
Attempt one:
I'd something like a HTTP status code returned for all
protocols, with the library making up the appropriate ones
as necessary, if there is none. (So that I can test a
gopher-URLError for "e.code==404" 
Attempt two:
When trying to retrieve an URL, I'd like to have a method to
detect "Not found", so I would like the following code
fragment, which (modulo mistakes introduced in copying this
from my code) works with http, to work with gopher (someday
ftp or rsync?) as well.

try:
  f = urllib2.open(anurl)
except urllib2.URLError,e:
  if e.code == 404:
     print "Not found"
     ... # e.g. try alternative location
  else:
     print "Something more serious...."
     raise e

Does this make it more clear?
Similar to urllib/urllib2 providing common method to
retrieve data for multiple protocols, there should be way to
be able to identify errors that are not protocol-specific.
(And the letter I did not find.)

Thanks

Thomas


----------------------------------------------------------------------

Comment By: Neal Norwitz (nnorwitz)
Date: 2003-01-05 01:32

Message:
Logged In: YES 
user_id=33168

Perhaps I'm missing something, but I don't understand what
you want.  All the exceptions in urllib2 derive from
URLError, which itself derives from IOError.  There are a
few cases where IOError is raised.  All other cases raise
URLError, HTTPError, or GopherError.  Isn't IOError (or
possibly URLError) what you are looking for?

So you would do:

  try:
    # your code here
  except IOError:
    # handle any urllib2 exception

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=662099&group_id=5470