icoretech/spotify-client

block script for error

Closed this issue · 2 comments

thanks for your gem is very good work,
but i have an error :

when i call this method:

trks = client.user_playlist_tracks(auth.uid, ply['id'], params = {}) unless ply.blank?

i have this error:

"error_message"=>"Error: Expected([200]) <=> Actual(404 Not Found)\n", "error_class"=>"Spotify::ResourceNotFound", "failed_at"=>1411490778.3330739, "retry_count"=>2, "retried_at"=>1411490839.1890712}

and when i receive this error the script crashes ...

the code:

plys['items'].each do |ply|

  trks = client.user_playlist_tracks(auth.uid, ply['id'], params = {}) unless ply.blank?

  p = Hash.new
  p['uri'] = ply['uri']
  p['ref_id'] = ply['id']
  p['url'] = ply['external_urls']['spotify']
  p['name'] = ply['name']

  playlist = Playlist.new(p)           

  trks['items'].each do |trk|

    t = Hash.new
    t['created_at']   = trk['added_at']
    t['ref_id']       = trk['track']['id']
    t['name']         = trk['track']['name']
    t['popularity']   = trk['track']['popularity']
    t['uri']          = trk['track']['uri']

    t['album_id']     = trk['track']['album']['id']
    t['album_name']   = trk['track']['album']['name']
    t['album_uri']    = trk['track']['album']['uri']
    t['album_href']   = trk['track']['album']['href']
    t['album_image']  = trk['track']['album']['images'][1]['url']

    t['artists_id']   = trk['track']['artists'][0]['id']
    t['artists_name'] = trk['track']['artists'][0]['name']
    t['artists_uri']  = trk['track']['artists'][0]['uri']
    t['artists_href'] = trk['track']['artists'][0]['href']

    track = Track.new(t)
    track.save()

    playlist.tracks << track

  end unless trks.blank?

  playlist.save()

  spotify = self.new(p)
  spotify.playlists << playlist
  spotify.save()

end

thanks for all!!!

Hello,
did you create the client with this option enabled:

@raise_errors = config[:raise_errors] || false
?

This option controls if the code should throw an exception or not, in this case you are getting 404 because (probably) the playlist you are looking for in client.user_playlist_tracks(auth.uid, ply['id']) cannot be found on Spotify. Playlists are tricky though, make sure the client is authenticated by passing an access token during construction:

@access_token = config[:access_token]

closing as stale