Skip to content

Fix unclear error message when connecting cluster #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 6 additions & 3 deletions cmd/gitops-repo-broker/internal/handlers/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ func RegisterCluster(db *gorm.DB, validate *validator.Validate, unmarshalFn Unma
err = validate.Struct(crr)
if err != nil {
log.Errorf("Failed to validate payload: %v", err)
common.WriteError(w, ErrInvalidPayload, http.StatusBadRequest)
combinedError := fmt.Errorf("%v, %v", ErrInvalidPayload, "Error parsing 'url' in field 'Ingress URL'")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! It be interesting to test the output from the validator too. From:

https://github.com/go-playground/validator/#error-return-value

it says we should cast to validation errors after the nil check to get the actual validation error which might be useful too wdyt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! Thank you so much 🙏 I tried this at first but thought it was too long so I used a shorter error message but it's better to pass the validation error of course 👍 🙂

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah hah! What does it look like? A bit crazy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for the url string: Key: 'ClusterUpdateRequest.IngressURL' Error:Field validation for 'IngressURL' failed on the 'url' tag 👌
And this is for the ID 🤔: strconv.ParseUint: parsing \"\": invalid syntax

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooft! Alright I take it back that error is a bit rough. Your shorter one is much better. Maybe we go back to that one?

Thanks for testing!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure! Thank you very much for reviewing Simon 🙏 🙂

common.WriteError(w, combinedError, http.StatusBadRequest)
return
}

Expand Down Expand Up @@ -213,14 +214,16 @@ func UpdateCluster(db *gorm.DB, validate *validator.Validate, unmarshalFn Unmars
err = validate.Struct(updated)
if err != nil {
log.Errorf("Failed to validate payload: %v", err)
common.WriteError(w, ErrInvalidPayload, http.StatusBadRequest)
combinedError := fmt.Errorf("%v, %v", ErrInvalidPayload, "Error parsing 'url' in field 'Ingress URL'")
common.WriteError(w, combinedError, http.StatusBadRequest)
return
}

id, err := getClusterIDFromRequest(r)
if err != nil {
log.Errorf("Failed to get id param from path: %v", err)
common.WriteError(w, ErrInvalidPayload, http.StatusBadRequest)
combinedError := fmt.Errorf("%v, %v", ErrInvalidPayload, "Error parsing 'id' param from path")
common.WriteError(w, combinedError, http.StatusBadRequest)
return
}

Expand Down
36 changes: 35 additions & 1 deletion cmd/gitops-repo-broker/internal/handlers/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,31 @@ func TestGetCluster(t *testing.T) {
}
}

func TestUpdateCluster_ValidateRequestBody(t *testing.T) {
db, err := utils.Open("", "sqlite", "", "", "")
assert.NoError(t, err)
err = utils.MigrateTables(db)
assert.NoError(t, err)

// Request body
data, _ := json.MarshalIndent(views.ClusterUpdateRequest{
Name: "derp",
IngressURL: "not a url",
}, "", " ")
response := executeUpdate(t, bytes.NewReader(data), db, json.Unmarshal, json.MarshalIndent, NewFakeTokenGenerator("fake token", nil).Generate)
assert.Equal(t, http.StatusBadRequest, response.Code)
assert.Equal(t, "{\"message\":\"Invalid payload, Error parsing 'url' in field 'Ingress URL'\"}\n", response.Body.String())

data, _ = json.MarshalIndent(views.ClusterUpdateRequest{
Name: "derp",
IngressURL: "",
}, "", " ")
response = executeUpdate(t, bytes.NewReader(data), db, json.Unmarshal, json.MarshalIndent, NewFakeTokenGenerator("fake token", nil).Generate)
assert.Equal(t, http.StatusBadRequest, response.Code)
assert.Equal(t, "{\"message\":\"Invalid payload, Error parsing 'id' param from path\"}\n", response.Body.String())

}

func TestUpdateCluster(t *testing.T) {
requestTests := []struct {
description string
Expand Down Expand Up @@ -1039,7 +1064,7 @@ func TestRegisterCluster_ValidateRequestBody(t *testing.T) {
}, "", " ")
response := executePost(t, bytes.NewReader(data), db, json.Unmarshal, json.MarshalIndent, NewFakeTokenGenerator("fake token", nil).Generate)
assert.Equal(t, http.StatusBadRequest, response.Code)
assert.Equal(t, "{\"message\":\"Invalid payload\"}\n", response.Body.String())
assert.Equal(t, "{\"message\":\"Invalid payload, Error parsing 'url' in field 'Ingress URL'\"}\n", response.Body.String())
}

func TestRegisterCluster(t *testing.T) {
Expand Down Expand Up @@ -1067,6 +1092,15 @@ func executePost(t *testing.T, r io.Reader, db *gorm.DB, unmarshalFn api.Unmarsh
return rec
}

func executeUpdate(t *testing.T, r io.Reader, db *gorm.DB, unmarshalFn api.Unmarshal, marshalFn api.MarshalIndent, generateTokenFn api.GenerateToken) *httptest.ResponseRecorder {
req, err := http.NewRequest("POST", "", r)
require.Nil(t, err)
rec := httptest.NewRecorder()
handler := http.HandlerFunc(api.UpdateCluster(db, validator.New(), unmarshalFn, marshalFn))
handler.ServeHTTP(rec, req)
return rec
}

func TestListAlerts(t *testing.T) {
db, err := utils.Open("", "sqlite", "", "", "")
assert.NoError(t, err)
Expand Down