-
-
Notifications
You must be signed in to change notification settings - Fork 362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error message returned if the track is locked #871
Error message returned if the track is locked #871
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you so much @Jrank2013 !
Would you be able to write a test that reproduces the problem and shows the fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks so much for adding the test! I have a couple of suggestions/questions, added below.
cmd/download.go
Outdated
body, err := ioutil.ReadAll(res.Body) | ||
res.Body = ioutil.NopCloser(bytes.NewReader(body)) | ||
|
||
if err := json.Unmarshal(body, &d.payload); err != nil || res.StatusCode < 200 || res.StatusCode > 299 { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The decodedAPIError
already unmarshals the error, right? Can we move all the error handling logic into that method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The response body can only be read once. If we unmarshal or decode the body before saving a copy we wouldn't have a way to decode the JSON into the error struct.
@@ -185,7 +187,14 @@ func newDownload(flags *pflag.FlagSet, usrCfg *viper.Viper) (*download, error) { | |||
} | |||
defer res.Body.Close() | |||
|
|||
if err := json.NewDecoder(res.Body).Decode(&d.payload); err != nil { | |||
if res.StatusCode < 200 || res.StatusCode > 299 { | |||
return nil, decodedAPIError(res) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now we have the decodedAPIError
twice, which seems like it shouldn't be necessary.
I'm sorry if I'm being dumb, here. I'll take some time after work to pull this down and play around with it to see if I understand it better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, I played with this and I totally see what's going on.
Sorry for being slow!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No problem, just glad I can help!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to merge this because it totally fixes the error.
I'm also going to spend a little bit of time rethinking the decodedAPIError
function, because I'm not really happy with it right now (but that's nothing to do with this PR, more about decisions made previously).
Thanks for your patience in walking me through the fix, @Jrank2013 !
This fixes #870.
If the user has not unlocked the track that they have requested to download, the cli will return an error.