Skip to content

Commit 42e56dc

Browse files
author
Katrina Owen
authored
Merge pull request #770 from Smarticles101/development
Submit prints out api error messages
2 parents 69aa991 + 099a54c commit 42e56dc

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

cmd/submit.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package cmd
22

33
import (
44
"bytes"
5+
"encoding/json"
56
"errors"
67
"fmt"
78
"io"
89
"mime/multipart"
10+
"net/http"
911
"os"
1012
"path/filepath"
1113

@@ -249,6 +251,15 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
249251
}
250252
defer resp.Body.Close()
251253

254+
if resp.StatusCode == http.StatusBadRequest {
255+
var jsonErrBody apiErrorMessage
256+
if err := json.NewDecoder(resp.Body).Decode(&jsonErrBody); err != nil {
257+
return fmt.Errorf("failed to parse error response - %s", err)
258+
}
259+
260+
return fmt.Errorf(jsonErrBody.Error.Message)
261+
}
262+
252263
bb := &bytes.Buffer{}
253264
_, err = bb.ReadFrom(resp.Body)
254265
if err != nil {
@@ -272,3 +283,10 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
272283
func init() {
273284
RootCmd.AddCommand(submitCmd)
274285
}
286+
287+
type apiErrorMessage struct {
288+
Error struct {
289+
Type string `json:"type"`
290+
Message string `json:"message"`
291+
} `json:"error,omitempty"`
292+
}

cmd/submit_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"bytes"
55
"encoding/json"
6+
"fmt"
67
"io/ioutil"
78
"net/http"
89
"net/http/httptest"
@@ -504,6 +505,8 @@ func fakeSubmitServer(t *testing.T, submittedFiles map[string]string) *httptest.
504505
}
505506
submittedFiles[fileHeader.Filename] = string(body)
506507
}
508+
509+
fmt.Fprint(w, "{}")
507510
})
508511
return httptest.NewServer(handler)
509512
}
@@ -553,6 +556,46 @@ func TestSubmitRelativePath(t *testing.T) {
553556
assert.Equal(t, "This is a file.", submittedFiles["file.txt"])
554557
}
555558

559+
func TestSubmitServerErr(t *testing.T) {
560+
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
561+
w.WriteHeader(http.StatusBadRequest)
562+
fmt.Fprintf(w, `{"error": {"type": "error", "message": "test error"}}`)
563+
})
564+
565+
ts := httptest.NewServer(handler)
566+
defer ts.Close()
567+
568+
tmpDir, err := ioutil.TempDir("", "submit-err-tmp-dir")
569+
defer os.RemoveAll(tmpDir)
570+
assert.NoError(t, err)
571+
572+
v := viper.New()
573+
v.Set("token", "abc123")
574+
v.Set("workspace", tmpDir)
575+
v.Set("apibaseurl", ts.URL)
576+
577+
cfg := config.Config{
578+
Persister: config.InMemoryPersister{},
579+
UserViperConfig: v,
580+
DefaultBaseURL: "http://example.com",
581+
}
582+
583+
dir := filepath.Join(tmpDir, "bogus-track", "bogus-exercise")
584+
os.MkdirAll(filepath.Join(dir, "subdir"), os.FileMode(0755))
585+
writeFakeMetadata(t, dir, "bogus-track", "bogus-exercise")
586+
587+
err = ioutil.WriteFile(filepath.Join(dir, "file-1.txt"), []byte("This is file 1"), os.FileMode(0755))
588+
assert.NoError(t, err)
589+
590+
files := []string{
591+
filepath.Join(dir, "file-1.txt"),
592+
}
593+
594+
err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), files)
595+
596+
assert.Regexp(t, "test error", err.Error())
597+
}
598+
556599
func TestSubmissionNotConnectedToRequesterAccount(t *testing.T) {
557600
submittedFiles := map[string]string{}
558601
ts := fakeSubmitServer(t, submittedFiles)

0 commit comments

Comments
 (0)