Skip to content

Commit

Permalink
Merge pull request exercism#770 from Smarticles101/development
Browse files Browse the repository at this point in the history
Submit prints out api error messages
  • Loading branch information
Katrina Owen authored Dec 28, 2018
2 parents 69aa991 + 099a54c commit 42e56dc
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
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

0 comments on commit 42e56dc

Please sign in to comment.