Skip to content

Commit d95de6b

Browse files
author
Katrina Owen
authored
Merge pull request #871 from Jrank2013/locked_exercise_reporting
Error message returned if the track is locked
2 parents a51b6ec + 31c4c10 commit d95de6b

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

cmd/download.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"errors"
67
"fmt"
78
"io"
9+
"io/ioutil"
810
"net/http"
911
netURL "net/url"
1012
"os"
@@ -187,7 +189,14 @@ func newDownload(flags *pflag.FlagSet, usrCfg *viper.Viper) (*download, error) {
187189
}
188190
defer res.Body.Close()
189191

190-
if err := json.NewDecoder(res.Body).Decode(&d.payload); err != nil {
192+
if res.StatusCode < 200 || res.StatusCode > 299 {
193+
return nil, decodedAPIError(res)
194+
}
195+
196+
body, _ := ioutil.ReadAll(res.Body)
197+
res.Body = ioutil.NopCloser(bytes.NewReader(body))
198+
199+
if err := json.Unmarshal(body, &d.payload); err != nil {
191200
return nil, decodedAPIError(res)
192201
}
193202

cmd/download_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,40 @@ func assertDownloadedCorrectFiles(t *testing.T, targetDir string) {
272272
assert.True(t, os.IsNotExist(err), "It should not write the file if empty.")
273273
}
274274

275+
func TestDownloadError(t *testing.T) {
276+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
277+
w.WriteHeader(http.StatusBadRequest)
278+
fmt.Fprintf(w, `{"error": {"type": "error", "message": "test error"}}`)
279+
})
280+
281+
ts := httptest.NewServer(handler)
282+
defer ts.Close()
283+
284+
tmpDir, err := ioutil.TempDir("", "submit-err-tmp-dir")
285+
defer os.RemoveAll(tmpDir)
286+
assert.NoError(t, err)
287+
288+
v := viper.New()
289+
v.Set("token", "abc123")
290+
v.Set("workspace", tmpDir)
291+
v.Set("apibaseurl", ts.URL)
292+
293+
cfg := config.Config{
294+
Persister: config.InMemoryPersister{},
295+
UserViperConfig: v,
296+
DefaultBaseURL: "http://example.com",
297+
}
298+
299+
flags := pflag.NewFlagSet("fake", pflag.PanicOnError)
300+
setupDownloadFlags(flags)
301+
flags.Set("uuid", "value")
302+
303+
err = runDownload(cfg, flags, []string{})
304+
305+
assert.Equal(t, "test error", err.Error())
306+
307+
}
308+
275309
const payloadTemplate = `
276310
{
277311
"solution": {

0 commit comments

Comments
 (0)