Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions cmd/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package cmd

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"path/filepath"

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

if resp.StatusCode == http.StatusBadRequest {
var jsonErrBody apiErrorMessage
if err := json.NewDecoder(resp.Body).Decode(&jsonErrBody); err != nil {
return fmt.Errorf("failed to parse error response - %s", err)
}

return fmt.Errorf(jsonErrBody.Error.Message)
}

bb := &bytes.Buffer{}
_, err = bb.ReadFrom(resp.Body)
if err != nil {
Expand All @@ -272,3 +283,10 @@ func runSubmit(cfg config.Config, flags *pflag.FlagSet, args []string) error {
func init() {
RootCmd.AddCommand(submitCmd)
}

type apiErrorMessage struct {
Error struct {
Type string `json:"type"`
Message string `json:"message"`
} `json:"error,omitempty"`
}
43 changes: 43 additions & 0 deletions cmd/submit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -504,6 +505,8 @@ func fakeSubmitServer(t *testing.T, submittedFiles map[string]string) *httptest.
}
submittedFiles[fileHeader.Filename] = string(body)
}

fmt.Fprint(w, "{}")
})
return httptest.NewServer(handler)
}
Expand Down Expand Up @@ -553,6 +556,46 @@ func TestSubmitRelativePath(t *testing.T) {
assert.Equal(t, "This is a file.", submittedFiles["file.txt"])
}

func TestSubmitServerErr(t *testing.T) {
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, `{"error": {"type": "error", "message": "test error"}}`)
})

ts := httptest.NewServer(handler)
defer ts.Close()

tmpDir, err := ioutil.TempDir("", "submit-err-tmp-dir")
defer os.RemoveAll(tmpDir)
assert.NoError(t, err)

v := viper.New()
v.Set("token", "abc123")
v.Set("workspace", tmpDir)
v.Set("apibaseurl", ts.URL)

cfg := config.Config{
Persister: config.InMemoryPersister{},
UserViperConfig: v,
DefaultBaseURL: "http://example.com",
}

dir := filepath.Join(tmpDir, "bogus-track", "bogus-exercise")
os.MkdirAll(filepath.Join(dir, "subdir"), os.FileMode(0755))
writeFakeMetadata(t, dir, "bogus-track", "bogus-exercise")

err = ioutil.WriteFile(filepath.Join(dir, "file-1.txt"), []byte("This is file 1"), os.FileMode(0755))
assert.NoError(t, err)

files := []string{
filepath.Join(dir, "file-1.txt"),
}

err = runSubmit(cfg, pflag.NewFlagSet("fake", pflag.PanicOnError), files)

assert.Regexp(t, "test error", err.Error())
}

func TestSubmissionNotConnectedToRequesterAccount(t *testing.T) {
submittedFiles := map[string]string{}
ts := fakeSubmitServer(t, submittedFiles)
Expand Down