zmb3 / spotify

A Go wrapper for the Spotify Web API
Apache License 2.0
1.39k stars 291 forks source link

PlayerCurrentlyPlaying returns err io.EOF when not playing #72

Closed diamondo25 closed 5 years ago

diamondo25 commented 6 years ago

Sometimes, when there's no player registered (I suppose), the API returns an EOF. The token is correct and the error can be ignored.

    if ftl, err := client.PlayerCurrentlyPlaying(); err != nil {
        if err != io.EOF {
            log.Fatal(err)
        }
    } else {
        if !ftl.Playing {
            log.Println("Currently not playing...")
            return
        }
        // Do whatever
    }
zmb3 commented 6 years ago

Thanks for pointing this out. I don't see any action necessary here, so I'll close the issue.

chrisvdg commented 6 years ago

@zmb3, should this not at least be documented? We could also make it return a self defined error for this that's more readable (to compare with.)

chrisvdg commented 6 years ago

The API returns a 204 for this request without body, so we can check for that response code and return a ErrNoPlayerCurrentlyPlaying or something.

Looks like current implementation doesn't allow to do this easily, I'll try and figure something out.

zmb3 commented 6 years ago

Should be able to just update spotify/Client.execute() to skip reading the response body in the event of a 204.

Something like this, for example:

        if result != nil  && resp.Status != http.StatusNoContent {
            if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
                return err
            }
        }