LaurentCR / pylast

Automatically exported from code.google.com/p/pylast
Apache License 2.0
0 stars 0 forks source link

ExpatError on response from Last.fm #58

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Please type example code that produces the issue:

Did an update of the currently playing track. What track it was is unknown.

What is the expected output? What do you see instead?

Got the following exception. Would like to get a WSError or such instead.

ERROR    junk after document element: line 2, column 0
Traceback (most recent call last):
  File "/home/jodal/dev/mopidy/mopidy/utils/process.py", line 66, in run
    self.run_inside_try()
  File "/home/jodal/dev/mopidy/mopidy/frontends/lastfm.py", line 69, in run_insi
de_try
    self.process_message(message)
  File "/home/jodal/dev/mopidy/mopidy/frontends/lastfm.py", line 87, in process_
message
    self.started_playing(message['track'])
  File "/home/jodal/dev/mopidy/mopidy/frontends/lastfm.py", line 105, in started_playing
    mbid=(track.musicbrainz_id or ''))
  File "/usr/lib/pymodules/python2.6/pylast.py", line 459, in update_now_playing
    _Request(self, "track.updateNowPlaying", params).execute()
  File "/usr/lib/pymodules/python2.6/pylast.py", line 801, in execute
    response = self._download_response()
  File "/usr/lib/pymodules/python2.6/pylast.py", line 792, in _download_response
    self._check_response_for_errors(response_text)
  File "/usr/lib/pymodules/python2.6/pylast.py", line 808, in _check_response_for_errors
    doc = minidom.parseString(_string(response))
  File "/usr/lib/python2.6/xml/dom/minidom.py", line 1928, in parseString
    return expatbuilder.parseString(string)  
  File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 940, in parseString
    return builder.parseString(string)
  File "/usr/lib/python2.6/xml/dom/expatbuilder.py", line 223, in parseString
    parser.Parse(string, True)
ExpatError: junk after document element: line 2, column 0

What versions of Pylast and Python are you using?

Pylast 0.5.6, Python 2.6.6.

Please provide any additional information below.

N/A

Original issue reported on code.google.com by stein.ma...@jodal.no on 21 Jan 2011 at 8:29

GoogleCodeExporter commented 9 years ago
I can't debug this issue. Please only submit issues with code that reproduces 
it.

Original comment by amr.hassan on 21 Jan 2011 at 8:35

GoogleCodeExporter commented 9 years ago
Actually, I did have full logging turned on. The code that was executed was 
approximately this:

network.update_now_playing(
    u'Massive Attack',
    u'Pray For Rain',
    album=u'Heligoland',
    duration='404',
    track_number='1',
    mbid='')

Original comment by stein.ma...@jodal.no on 21 Jan 2011 at 8:37

GoogleCodeExporter commented 9 years ago
Can you still reproduce the issue with that code? Because I just ran it on 
python 2.7 and it ran fine with no errors.

Original comment by amr.hassan on 21 Jan 2011 at 8:42

GoogleCodeExporter commented 9 years ago
Sorry, but I can't ensure that Last.fm always behaves identically.

You are though in the position of catching and wrapping these exceptions, so I, 
as a user of your library, does not have to worry about the exceptions each and 
every library you use could raise. Doing that for all libraries I use and their 
libraries again would quite fast become rather tedious and infeasible.

Original comment by stein.ma...@jodal.no on 21 Jan 2011 at 8:47

GoogleCodeExporter commented 9 years ago
It's in The Zen of Python that "Errors should never pass silently." 
(http://www.python.org/dev/peps/pep-0020/).
Your code should be prepared however to catch exceptions resulting from network 
data corruption or funky last.fm responses and retrying or ignoring the request 
based on your liking.

Original comment by amr.hassan on 21 Jan 2011 at 8:54

GoogleCodeExporter commented 9 years ago
Of course they should not pass silently. And of course I should take care and 
perform proper actions upon such events.

Still, wrapping exceptions does not silence them. It just means that I can 
catch one well-known exception related to intermittent problems like network 
outages and handle that with care.

The alternative is that all users of the library will detect all the possible 
error conditions over time and will have to catch each and every one of them 
themselves. Not to mention the fact that new possible exceptions could be 
introduced by the library developer introducing new dependencies with new 
exceptions himself.

Or do you want me to "except Exception:"?

Original comment by stein.ma...@jodal.no on 21 Jan 2011 at 9:00

GoogleCodeExporter commented 9 years ago
You can catch the well known exceptions and also finally catch any other 
unknown exception as well in areas where you see that can produce unknown 
exceptions like so:

try:
    #code
except pylast.WSError:
    # handle that error
except Exception as e:
    # Handle any other errors too.

I will look into handling malformed responses from Last.fm and raising one 
single exception for that if that's what you're proposing.

Original comment by amr.hassan on 21 Jan 2011 at 9:13

GoogleCodeExporter commented 9 years ago
Yes, that would be lovely! :-)

This far, I've gotten the following exceptions from Pylast upon 
connection/response problems:

- UnicodeDecodeError
- socket.error
- xml.parsers.expat.ExpatError
- httplib.HTTPException

Original comment by stein.ma...@jodal.no on 21 Jan 2011 at 9:16

GoogleCodeExporter commented 9 years ago
Err, I've got httplib.BadStatusLine, which is a subclass of 
httplib.HTTPException.

Original comment by stein.ma...@jodal.no on 21 Jan 2011 at 9:17

GoogleCodeExporter commented 9 years ago
Stacktrace from the httplib error:

...
  File "/home/jodal/dev/mopidy/mopidy/frontends/lastfm.py", line 78, in setup
    username=username, password_hash=password_hash)
  File "/usr/lib/pymodules/python2.6/pylast.py", line 572, in __init__
    "user": "user/%(name)s",
  File "/usr/lib/pymodules/python2.6/pylast.py", line 161, in __init__
    self.session_key = sk_gen.get_session_key(self.username, self.password_hash)
  File "/usr/lib/pymodules/python2.6/pylast.py", line 898, in get_session_key
    doc = request.execute()
  File "/usr/lib/pymodules/python2.6/pylast.py", line 801, in execute
    response = self._download_response()
  File "/usr/lib/pymodules/python2.6/pylast.py", line 790, in _download_response
    response = conn.getresponse()
  File "/usr/lib/python2.6/httplib.py", line 990, in getresponse
    response.begin()
  File "/usr/lib/python2.6/httplib.py", line 391, in begin
    version, status, reason = self._read_status()
  File "/usr/lib/python2.6/httplib.py", line 355, in _read_status
    raise BadStatusLine(line)
BadStatusLine

I guess this is a good day for testing connection issues with Last.fm: 
http://blog.last.fm/2011/01/21/about-the-last-24-hours

Original comment by stein.ma...@jodal.no on 21 Jan 2011 at 9:20

GoogleCodeExporter commented 9 years ago
Okay, keep the stracktraces coming.

Original comment by amr.hassan on 21 Jan 2011 at 9:39

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r242.

Original comment by amr.hassan on 21 Jan 2011 at 9:49