Skip to content

Commit

Permalink
Fixed issue with registry error (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
nrwiersma authored Apr 23, 2019
1 parent 45eff89 commit 9000dad
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
8 changes: 6 additions & 2 deletions registry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (c *Client) request(method, uri string, in, out interface{}) error {

if resp.StatusCode >= 400 {
err := Error{StatusCode: resp.StatusCode}
jsoniter.NewDecoder(resp.Body).Decode(err)
_ = jsoniter.NewDecoder(resp.Body).Decode(&err)
return err
}

Expand All @@ -235,5 +235,9 @@ type Error struct {

// Error returns the error message.
func (e Error) Error() string {
return e.Message
if e.Message != "" {
return e.Message
}

return "registry error: " + strconv.Itoa(e.StatusCode)
}
30 changes: 30 additions & 0 deletions registry/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ func TestNewClient_UrlError(t *testing.T) {
assert.Error(t, err)
}

func TestClient_PopulatesError(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/vnd.schemaregistry.v1+json")
w.WriteHeader(422)
_, _ = w.Write([]byte(`{"error_code": 42202, "message": "schema may not be empty"}"`))
}))
defer s.Close()
client, _ := registry.NewClient(s.URL)

_, _, err := client.CreateSchema("test", "")

assert.Error(t, err)
assert.IsType(t, registry.Error{}, err)
assert.Equal(t, "schema may not be empty", err.Error())
regError := err.(registry.Error)
assert.Equal(t, 422, regError.StatusCode)
assert.Equal(t, 42202, regError.Code)
assert.Equal(t, "schema may not be empty", regError.Message)
}

func TestClient_GetSchema(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "GET", r.Method)
Expand Down Expand Up @@ -403,3 +423,13 @@ func TestError_Error(t *testing.T) {

assert.Equal(t, "Schema not found", str)
}

func TestError_ErrorEmptyMEssage(t *testing.T) {
err := registry.Error{
StatusCode: 404,
}

str := err.Error()

assert.Equal(t, "registry error: 404", str)
}

0 comments on commit 9000dad

Please sign in to comment.