Skip to content

Commit

Permalink
use numbers and not http.Status___ enum
Browse files Browse the repository at this point in the history
  • Loading branch information
6543 committed Dec 20, 2019
1 parent e2fc0a0 commit b1ff386
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 96 deletions.
9 changes: 4 additions & 5 deletions routers/api/v1/misc/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package misc

import (
"net/http"
"strings"

"code.gitea.io/gitea/modules/context"
Expand Down Expand Up @@ -65,20 +64,20 @@ func Markdown(ctx *context.APIContext, form api.MarkdownOption) {
if form.Wiki {
_, err := ctx.Write([]byte(markdown.RenderWiki(md, urlPrefix, meta)))
if err != nil {
ctx.Error(http.StatusInternalServerError, "", err)
ctx.Error(500, "", err)
return
}
} else {
_, err := ctx.Write(markdown.Render(md, urlPrefix, meta))
if err != nil {
ctx.Error(http.StatusInternalServerError, "", err)
ctx.Error(500, "", err)
return
}
}
default:
_, err := ctx.Write(markdown.RenderRaw([]byte(form.Text), "", false))
if err != nil {
ctx.Error(http.StatusInternalServerError, "", err)
ctx.Error(500, "", err)
return
}
}
Expand Down Expand Up @@ -112,7 +111,7 @@ func MarkdownRaw(ctx *context.APIContext) {
}
_, err = ctx.Write(markdown.RenderRaw(body, "", false))
if err != nil {
ctx.Error(http.StatusInternalServerError, "", err)
ctx.Error(500, "", err)
return
}
}
1 change: 0 additions & 1 deletion routers/api/v1/misc/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package misc

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"net/url"
"strings"
Expand Down
3 changes: 1 addition & 2 deletions routers/api/v1/misc/signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package misc

import (
"fmt"
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
Expand Down Expand Up @@ -57,6 +56,6 @@ func SigningKey(ctx *context.Context) {
_, err = ctx.Write([]byte(content))
if err != nil {
log.Error("Error writing key content %v", err)
ctx.Error(http.StatusInternalServerError, fmt.Sprintf("%v", err))
ctx.Error(500, fmt.Sprintf("%v", err))
}
}
7 changes: 3 additions & 4 deletions routers/api/v1/repo/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package repo

import (
"net/http"

"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/repofiles"
Expand Down Expand Up @@ -40,12 +39,12 @@ func GetBlob(ctx *context.APIContext) {

sha := ctx.Params("sha")
if len(sha) == 0 {
ctx.Error(http.StatusBadRequest, "", "sha not provided")
ctx.Error(400, "", "sha not provided")
return
}
if blob, err := repofiles.GetBlobBySHA(ctx.Repo.Repository, sha); err != nil {
ctx.Error(http.StatusBadRequest, "", err)
ctx.Error(400, "", err)
} else {
ctx.JSON(http.StatusOK, blob)
ctx.JSON(200, blob)
}
}
31 changes: 15 additions & 16 deletions routers/api/v1/repo/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package repo

import (
"encoding/base64"
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
Expand Down Expand Up @@ -53,12 +52,12 @@ func GetRawFile(ctx *context.APIContext) {
if git.IsErrNotExist(err) {
ctx.NotFound()
} else {
ctx.Error(http.StatusInternalServerError, "GetBlobByPath", err)
ctx.Error(500, "GetBlobByPath", err)
}
return
}
if err = repo.ServeBlob(ctx.Context, blob); err != nil {
ctx.Error(http.StatusInternalServerError, "ServeBlob", err)
ctx.Error(500, "ServeBlob", err)
}
}

Expand Down Expand Up @@ -91,7 +90,7 @@ func GetArchive(ctx *context.APIContext) {
repoPath := models.RepoPath(ctx.Params(":username"), ctx.Params(":reponame"))
gitRepo, err := git.OpenRepository(repoPath)
if err != nil {
ctx.Error(http.StatusInternalServerError, "OpenRepository", err)
ctx.Error(500, "OpenRepository", err)
return
}
ctx.Repo.GitRepo = gitRepo
Expand Down Expand Up @@ -131,7 +130,7 @@ func GetEditorconfig(ctx *context.APIContext) {
if git.IsErrNotExist(err) {
ctx.NotFound(err)
} else {
ctx.Error(http.StatusInternalServerError, "GetEditorconfig", err)
ctx.Error(500, "GetEditorconfig", err)
}
return
}
Expand All @@ -142,7 +141,7 @@ func GetEditorconfig(ctx *context.APIContext) {
ctx.NotFound(err)
return
}
ctx.JSON(http.StatusOK, def)
ctx.JSON(200, def)
}

// CanWriteFiles returns true if repository is editable and user has proper access level.
Expand Down Expand Up @@ -211,9 +210,9 @@ func CreateFile(ctx *context.APIContext, apiOpts api.CreateFileOptions) {
}

if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
ctx.Error(http.StatusInternalServerError, "CreateFile", err)
ctx.Error(500, "CreateFile", err)
} else {
ctx.JSON(http.StatusCreated, fileResponse)
ctx.JSON(201, fileResponse)
}
}

Expand Down Expand Up @@ -275,9 +274,9 @@ func UpdateFile(ctx *context.APIContext, apiOpts api.UpdateFileOptions) {
}

if fileResponse, err := createOrUpdateFile(ctx, opts); err != nil {
ctx.Error(http.StatusInternalServerError, "UpdateFile", err)
ctx.Error(500, "UpdateFile", err)
} else {
ctx.JSON(http.StatusOK, fileResponse)
ctx.JSON(200, fileResponse)
}
}

Expand Down Expand Up @@ -333,7 +332,7 @@ func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) {
// "200":
// "$ref": "#/responses/FileDeleteResponse"
if !CanWriteFiles(ctx.Repo) {
ctx.Error(http.StatusInternalServerError, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{
ctx.Error(500, "DeleteFile", models.ErrUserDoesNotHaveAccessToRepo{
UserID: ctx.User.ID,
RepoName: ctx.Repo.Repository.LowerName,
})
Expand Down Expand Up @@ -361,9 +360,9 @@ func DeleteFile(ctx *context.APIContext, apiOpts api.DeleteFileOptions) {
}

if fileResponse, err := repofiles.DeleteRepoFile(ctx.Repo.Repository, ctx.User, opts); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteFile", err)
ctx.Error(500, "DeleteFile", err)
} else {
ctx.JSON(http.StatusOK, fileResponse)
ctx.JSON(200, fileResponse)
}
}

Expand Down Expand Up @@ -400,7 +399,7 @@ func GetContents(ctx *context.APIContext) {
// "$ref": "#/responses/ContentsResponse"

if !CanReadFiles(ctx.Repo) {
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", models.ErrUserDoesNotHaveAccessToRepo{
ctx.Error(500, "GetContentsOrList", models.ErrUserDoesNotHaveAccessToRepo{
UserID: ctx.User.ID,
RepoName: ctx.Repo.Repository.LowerName,
})
Expand All @@ -411,9 +410,9 @@ func GetContents(ctx *context.APIContext) {
ref := ctx.QueryTrim("ref")

if fileList, err := repofiles.GetContentsOrList(ctx.Repo.Repository, treePath, ref); err != nil {
ctx.Error(http.StatusInternalServerError, "GetContentsOrList", err)
ctx.Error(500, "GetContentsOrList", err)
} else {
ctx.JSON(http.StatusOK, fileList)
ctx.JSON(200, fileList)
}
}

Expand Down
3 changes: 1 addition & 2 deletions routers/api/v1/repo/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package repo

import (
"net/http"
"testing"

"code.gitea.io/gitea/models"
Expand All @@ -24,7 +23,7 @@ func TestTestHook(t *testing.T) {
test.LoadRepoCommit(t, ctx)
test.LoadUser(t, ctx, 2)
TestHook(&context.APIContext{Context: ctx, Org: nil})
assert.EqualValues(t, http.StatusNoContent, ctx.Resp.Status())
assert.EqualValues(t, 204, ctx.Resp.Status())

models.AssertExistsAndLoadBean(t, &models.HookTask{
RepoID: 1,
Expand Down
5 changes: 2 additions & 3 deletions routers/api/v1/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ package repo

import (
"fmt"
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -379,7 +378,7 @@ func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) {
if form.Closed {
if err := issue_service.ChangeStatus(issue, ctx.User, true); err != nil {
if models.IsErrDependenciesLeft(err) {
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
ctx.Error(412, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
return
}
ctx.Error(500, "ChangeStatus", err)
Expand Down Expand Up @@ -513,7 +512,7 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
if form.State != nil {
if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
if models.IsErrDependenciesLeft(err) {
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
ctx.Error(412, "DependenciesLeft", "cannot close this issue because it still has open dependencies")
return
}
ctx.Error(500, "ChangeStatus", err)
Expand Down
15 changes: 7 additions & 8 deletions routers/api/v1/repo/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package repo

import (
"fmt"
"net/http"
"strings"
"time"

Expand Down Expand Up @@ -360,7 +359,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {

err = pr.LoadIssue()
if err != nil {
ctx.Error(http.StatusInternalServerError, "LoadIssue", err)
ctx.Error(500, "LoadIssue", err)
return
}
issue := pr.Issue
Expand Down Expand Up @@ -443,7 +442,7 @@ func EditPullRequest(ctx *context.APIContext, form api.EditPullRequestOption) {
if form.State != nil {
if err = issue_service.ChangeStatus(issue, ctx.User, api.StateClosed == api.StateType(*form.State)); err != nil {
if models.IsErrDependenciesLeft(err) {
ctx.Error(http.StatusPreconditionFailed, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
ctx.Error(412, "DependenciesLeft", "cannot close this pull request because it still has open dependencies")
return
}
ctx.Error(500, "ChangeStatus", err)
Expand Down Expand Up @@ -561,7 +560,7 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {

err = pr.LoadIssue()
if err != nil {
ctx.Error(http.StatusInternalServerError, "LoadIssue", err)
ctx.Error(500, "LoadIssue", err)
return
}
pr.Issue.Repo = ctx.Repo.Repository
Expand Down Expand Up @@ -620,15 +619,15 @@ func MergePullRequest(ctx *context.APIContext, form auth.MergePullRequestForm) {
return
} else if models.IsErrMergeConflicts(err) {
conflictError := err.(models.ErrMergeConflicts)
ctx.JSON(http.StatusConflict, conflictError)
ctx.JSON(409, conflictError)
} else if models.IsErrRebaseConflicts(err) {
conflictError := err.(models.ErrRebaseConflicts)
ctx.JSON(http.StatusConflict, conflictError)
ctx.JSON(409, conflictError)
} else if models.IsErrMergeUnrelatedHistories(err) {
conflictError := err.(models.ErrMergeUnrelatedHistories)
ctx.JSON(http.StatusConflict, conflictError)
ctx.JSON(409, conflictError)
} else if models.IsErrMergePushOutOfDate(err) {
ctx.Status(http.StatusConflict)
ctx.Status(409)
return
}
ctx.Error(500, "Merge", err)
Expand Down
Loading

0 comments on commit b1ff386

Please sign in to comment.