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

runservice: return right error on not existing run #451

Merged
merged 1 commit into from
Oct 20, 2023

Conversation

alessandro-sorint
Copy link
Contributor

@alessandro-sorint alessandro-sorint commented Oct 20, 2023

A missing return after nil run check caused the return of a wrong error.
Fix this by moving the logic to a dedicated function instead of doing everything in ServeHTTP.

@sgotti
Copy link
Member

sgotti commented Oct 20, 2023

@alessandro-sorint Instead of doing a fix by just adding a missing return lets convert it to a style that avoid such errors using a new function instead of putting all the logic in ServeHTTP.

See for example 1e5cb9e where we use a do function.

@alessandro-sorint
Copy link
Contributor Author

@alessandro-sorint Instead of doing a fix by just adding a missing return lets convert it to a style that avoid such errors using a new function instead of putting all the logic in ServeHTTP.

See for example 1e5cb9e where we use a do function.

@sgotti I added a do function

}

cgts, err := types.MarshalChangeGroupsUpdateToken(cgt)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return nil, util.NewAPIError(util.ErrInternal, err)
Copy link
Member

Choose a reason for hiding this comment

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

As above.

@@ -540,22 +550,19 @@ func (h *RunByGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
})
if err != nil {
h.log.Err(err).Send()
http.Error(w, err.Error(), http.StatusInternalServerError)
return
return nil, util.NewAPIError(util.ErrInternal, err)
Copy link
Member

Choose a reason for hiding this comment

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

Just return errors.WithStack(err). No need to explicitly define an ErrInternal. Every error not of type APIError is considered by http util an internal error. See the other APIs.

@@ -540,22 +550,19 @@ func (h *RunByGroupHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
})
if err != nil {
h.log.Err(err).Send()
Copy link
Member

Choose a reason for hiding this comment

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

remove logging.

@@ -5780,6 +5780,39 @@ func TestGetProjectRuns(t *testing.T) {
}
})
}

t.Run("test get run not exists", func(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

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

test get not existing run

A missing return after nil run check caused the return of a wrong error.
Fix this by moving the logic to a dedicated function instead of doing everything in ServeHTTP.
@alessandro-sorint alessandro-sorint changed the title runservice: fix run by group api runservice: return right error on not existing run Oct 20, 2023
@sgotti sgotti merged commit 52b37fe into agola-io:master Oct 20, 2023
1 check passed
@alessandro-sorint alessandro-sorint deleted the fix-apirunbygroup branch October 20, 2023 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants