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

feat: Simplifying error handling a little bit #2504

Closed
Closed
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
10 changes: 6 additions & 4 deletions server/api/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ import (
)

func handlePipelineErr(c *gin.Context, err error) {
if errors.Is(err, &pipeline.ErrNotFound{}) {
var notFound *pipeline.ErrNotFound
var badRequest *pipeline.ErrBadRequest
if errors.As(err, &notFound) {
c.String(http.StatusNotFound, "%s", err)
} else if errors.Is(err, &pipeline.ErrBadRequest{}) {
} else if errors.As(err, &badRequest) {
c.String(http.StatusBadRequest, "%s", err)
} else if errors.Is(err, &pipeline.ErrFiltered{}) {
c.Status(http.StatusNoContent)
} else if errors.Is(err, pipeline.ErrFilteredRestrictions) || errors.Is(err, pipeline.ErrFilteredSteps) {
c.String(http.StatusNoContent, "%s", err)
} else {
_ = c.AbortWithError(http.StatusInternalServerError, err)
}
Expand Down
45 changes: 45 additions & 0 deletions server/api/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package api

import (
"net/http"
"net/http/httptest"
"testing"

"github.com/gin-gonic/gin"

"github.com/woodpecker-ci/woodpecker/server/pipeline"
)

func TestHandlePipelineError(t *testing.T) {
tests := []struct {
err error
code int
}{
{
err: pipeline.ErrFilteredRestrictions,
code: http.StatusNoContent,
},
{
err: pipeline.ErrFilteredSteps,
code: http.StatusNoContent,
},
{
err: &pipeline.ErrNotFound{Msg: "pipeline not found"},
code: http.StatusNotFound,
},
{
err: &pipeline.ErrBadRequest{Msg: "bad request error"},
code: http.StatusBadRequest,
},
}

for _, tt := range tests {
r := httptest.NewRecorder()
c, _ := gin.CreateTestContext(r)
handlePipelineErr(c, tt.err)
if r.Code != tt.code {
t.Errorf("status code: %d - expected: %d", r.Code, tt.code)
}
}

}
4 changes: 2 additions & 2 deletions server/pipeline/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ func Create(ctx context.Context, _store store.Store, repo *model.Repo, pipeline
filtered, parseErr = checkIfFiltered(repo, pipeline, forgeYamlConfigs)
if parseErr == nil {
if filtered {
err := ErrFiltered{Msg: "global when filter of all workflows do skip this pipeline"}
err := ErrFilteredRestrictions
log.Debug().Str("repo", repo.FullName).Msgf("%v", err)
return nil, err
Comment on lines +69 to 71
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can replace here (and below) too the local var err directly with ErrFilteredRestrictions

}

if zeroSteps(pipeline, forgeYamlConfigs) {
err := ErrFiltered{Msg: "step conditions yield zero runnable steps"}
err := ErrFilteredSteps
log.Debug().Str("repo", repo.FullName).Msgf("%v", err)
return nil, err
}
Expand Down
29 changes: 6 additions & 23 deletions server/pipeline/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

package pipeline

import "errors"

type ErrNotFound struct {
Msg string
}
Expand All @@ -38,26 +40,7 @@ func (e ErrBadRequest) Error() string {
return e.Msg
}

func (e ErrBadRequest) Is(target error) bool {
_, ok := target.(ErrBadRequest) //nolint:errorlint
if !ok {
_, ok = target.(*ErrBadRequest) //nolint:errorlint
}
return ok
}

type ErrFiltered struct {
Msg string
}

func (e ErrFiltered) Error() string {
return "ignoring hook: " + e.Msg
}

func (e *ErrFiltered) Is(target error) bool {
_, ok := target.(ErrFiltered) //nolint:errorlint
if !ok {
_, ok = target.(*ErrFiltered) //nolint:errorlint
}
return ok
}
var (
ErrFilteredRestrictions = errors.New("ignoring hook: branch does not match restrictions defined in yaml") // global when filter of all workflows do skip this pipeline
ErrFilteredSteps = errors.New("ignoring hook: step conditions yield zero runnable steps")
)