Skip to content

Use raw status codes when calling ctx.Handle #3330

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

Closed
wants to merge 1 commit into from
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
22 changes: 11 additions & 11 deletions routers/repo/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func HTTP(ctx *context.Context) {
authUser, err = models.UserSignIn(authUsername, authPasswd)
if err != nil {
if !models.IsErrUserNotExist(err) {
ctx.Handle(http.StatusInternalServerError, "UserSignIn error: %v", err)
ctx.Handle(500, "UserSignIn error: %v", err)
return
}
}
Expand All @@ -140,7 +140,7 @@ func HTTP(ctx *context.Context) {
if models.IsErrUserNotExist(err) {
ctx.HandleText(http.StatusUnauthorized, "invalid credentials")
} else {
ctx.Handle(http.StatusInternalServerError, "GetUserByName", err)
ctx.Handle(500, "GetUserByName", err)
}
return
}
Expand All @@ -152,15 +152,15 @@ func HTTP(ctx *context.Context) {
if models.IsErrAccessTokenNotExist(err) || models.IsErrAccessTokenEmpty(err) {
ctx.HandleText(http.StatusUnauthorized, "invalid credentials")
} else {
ctx.Handle(http.StatusInternalServerError, "GetAccessTokenBySha", err)
ctx.Handle(500, "GetAccessTokenBySha", err)
}
return
}

if isUsernameToken {
authUser, err = models.GetUserByID(token.UID)
if err != nil {
ctx.Handle(http.StatusInternalServerError, "GetUserByID", err)
ctx.Handle(500, "GetUserByID", err)
return
}
} else if authUser.ID != token.UID {
Expand All @@ -170,7 +170,7 @@ func HTTP(ctx *context.Context) {

token.UpdatedUnix = util.TimeStampNow()
if err = models.UpdateAccessToken(token); err != nil {
ctx.Handle(http.StatusInternalServerError, "UpdateAccessToken", err)
ctx.Handle(500, "UpdateAccessToken", err)
}
} else {
_, err = models.GetTwoFactorByUID(authUser.ID)
Expand All @@ -180,21 +180,21 @@ func HTTP(ctx *context.Context) {
ctx.HandleText(http.StatusUnauthorized, "Users with two-factor authentication enabled cannot perform HTTP/HTTPS operations via plain username and password. Please create and use a personal access token on the user settings page")
return
} else if !models.IsErrTwoFactorNotEnrolled(err) {
ctx.Handle(http.StatusInternalServerError, "IsErrTwoFactorNotEnrolled", err)
ctx.Handle(500, "IsErrTwoFactorNotEnrolled", err)
return
}
}

if !isPublicPull {
has, err := models.HasAccess(authUser.ID, repo, accessMode)
if err != nil {
ctx.Handle(http.StatusInternalServerError, "HasAccess", err)
ctx.Handle(500, "HasAccess", err)
return
} else if !has {
if accessMode == models.AccessModeRead {
has, err = models.HasAccess(authUser.ID, repo, models.AccessModeWrite)
if err != nil {
ctx.Handle(http.StatusInternalServerError, "HasAccess2", err)
ctx.Handle(500, "HasAccess2", err)
return
} else if !has {
ctx.HandleText(http.StatusForbidden, "User permission denied")
Expand Down Expand Up @@ -369,7 +369,7 @@ func serviceRPC(h serviceHandler, service string) {
reqBody, err = gzip.NewReader(reqBody)
if err != nil {
log.GitLogger.Error(2, "fail to create gzip reader: %v", err)
h.w.WriteHeader(http.StatusInternalServerError)
h.w.WriteHeader(500)
return
}
}
Expand Down Expand Up @@ -501,7 +501,7 @@ func HTTPBackend(ctx *context.Context, cfg *serviceConfig) http.HandlerFunc {
dir, err := getGitRepoPath(m[1])
if err != nil {
log.GitLogger.Error(4, err.Error())
ctx.Handle(http.StatusNotFound, "HTTPBackend", err)
ctx.Handle(404, "HTTPBackend", err)
return
}

Expand All @@ -510,7 +510,7 @@ func HTTPBackend(ctx *context.Context, cfg *serviceConfig) http.HandlerFunc {
}
}

ctx.Handle(http.StatusNotFound, "HTTPBackend", nil)
ctx.Handle(404, "HTTPBackend", nil)
return
}
}
8 changes: 4 additions & 4 deletions routers/repo/issue_stopwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ func IssueStopwatch(c *context.Context) {
return
}
if !c.Repo.CanUseTimetracker(issue, c.User) {
c.Handle(http.StatusNotFound, "CanUseTimetracker", nil)
c.Handle(404, "CanUseTimetracker", nil)
return
}

if err := models.CreateOrStopIssueStopwatch(c.User, issue); err != nil {
c.Handle(http.StatusInternalServerError, "CreateOrStopIssueStopwatch", err)
c.Handle(500, "CreateOrStopIssueStopwatch", err)
return
}

Expand All @@ -38,12 +38,12 @@ func CancelStopwatch(c *context.Context) {
return
}
if !c.Repo.CanUseTimetracker(issue, c.User) {
c.Handle(http.StatusNotFound, "CanUseTimetracker", nil)
c.Handle(404, "CanUseTimetracker", nil)
return
}

if err := models.CancelStopwatch(c.User, issue); err != nil {
c.Handle(http.StatusInternalServerError, "CancelStopwatch", err)
c.Handle(500, "CancelStopwatch", err)
return
}

Expand Down
4 changes: 2 additions & 2 deletions routers/repo/issue_timetrack.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func AddTimeManually(c *context.Context, form auth.AddTimeManuallyForm) {
return
}
if !c.Repo.CanUseTimetracker(issue, c.User) {
c.Handle(http.StatusNotFound, "CanUseTimetracker", nil)
c.Handle(404, "CanUseTimetracker", nil)
return
}
url := issue.HTMLURL()
Expand All @@ -40,7 +40,7 @@ func AddTimeManually(c *context.Context, form auth.AddTimeManuallyForm) {
}

if _, err := models.AddTime(c.User, issue, int64(total.Seconds())); err != nil {
c.Handle(http.StatusInternalServerError, "AddTime", err)
c.Handle(500, "AddTime", err)
return
}

Expand Down
4 changes: 2 additions & 2 deletions routers/repo/issue_watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func IssueWatch(c *context.Context) {
watch, err := strconv.ParseBool(c.Req.PostForm.Get("watch"))
if err != nil {
c.Handle(http.StatusInternalServerError, "watch is not bool", err)
c.Handle(500, "watch is not bool", err)
return
}

Expand All @@ -27,7 +27,7 @@ func IssueWatch(c *context.Context) {
}

if err := models.CreateOrUpdateIssueWatch(c.User.ID, issue.ID, watch); err != nil {
c.Handle(http.StatusInternalServerError, "CreateOrUpdateIssueWatch", err)
c.Handle(500, "CreateOrUpdateIssueWatch", err)
return
}

Expand Down