Skip to content
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

PMM-13658 fix updates error response #9

Merged
merged 2 commits into from
Jan 17, 2025
Merged
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion pkg/api/update/update.go
Original file line number Diff line number Diff line change
@@ -93,7 +93,8 @@ func (handle *Handler) Handle(w http.ResponseWriter, r *http.Request) {

func handleError(w http.ResponseWriter, err error) {
if err != nil {
if errors.Is(err, &types.ValidationError{}) {
Copy link
Author

Choose a reason for hiding this comment

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

errors.Is was only working with sentinel errors (e.g errors created with errors.New) so it was failing in this case.

var validationErr *types.ValidationError
if errors.As(err, &validationErr) {
log.Warning(err)
w.WriteHeader(http.StatusPreconditionFailed)
w.Write([]byte(err.Error()))
46 changes: 46 additions & 0 deletions pkg/api/update/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package update

import (
"errors"
"github.com/containrrr/watchtower/pkg/types"
"net/http"
"net/http/httptest"
"testing"
)
Copy link
Member

Choose a reason for hiding this comment

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

please format imports


func TestHandleError(t *testing.T) {
tests := []struct {
name string
error error
expectedStatus int
}{
{
name: "no error returns OK",
error: nil,
expectedStatus: http.StatusOK,
},
{
name: "validation error returns message",
error: types.NewValidationError("no new image available"),
expectedStatus: http.StatusPreconditionFailed,
},
{
name: "generic server error",
error: errors.New("server error"),
expectedStatus: http.StatusInternalServerError,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_ = httptest.NewRequest(http.MethodGet, "/v1/update", nil)
Copy link
Member

Choose a reason for hiding this comment

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

do we need this?

Copy link
Author

Choose a reason for hiding this comment

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

No actually, was a spill over

rr := httptest.NewRecorder()

handleError(rr, tt.error)

if status := rr.Result().StatusCode; status != tt.expectedStatus {
t.Errorf("the handler wrote status code %d, expected: %d", status, tt.expectedStatus)
}
})
}
}