Skip to content
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

Merged
merged 3 commits into from
Aug 21, 2019
Merged

Error message returned if the track is locked #871

merged 3 commits into from
Aug 21, 2019

Conversation

Jrank2013
Copy link
Contributor

@Jrank2013 Jrank2013 commented Aug 13, 2019

This fixes #870.

If the user has not unlocked the track that they have requested to download, the cli will return an error.

Copy link
Member

@kytrinyx kytrinyx left a 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?

cmd/download.go Show resolved Hide resolved
Copy link
Member

@kytrinyx kytrinyx left a 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 {
Copy link
Member

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?

Copy link
Contributor Author

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.

cmd/download_test.go Show resolved Hide resolved
cmd/download_test.go Outdated Show resolved Hide resolved
cmd/download_test.go Outdated Show resolved Hide resolved
@@ -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)
Copy link
Member

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.

Copy link
Member

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!

Copy link
Contributor Author

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!

Copy link
Member

@kytrinyx kytrinyx left a 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 !

@kytrinyx kytrinyx merged commit d95de6b into exercism:master Aug 21, 2019
@Jrank2013 Jrank2013 deleted the locked_exercise_reporting branch August 21, 2019 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Exercism 3.0.12 no longer reports errors when trying to download a locked exercise
2 participants