Skip to content

Commit

Permalink
Use json return but not 500 directly
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Apr 30, 2024
1 parent 833ec56 commit 81d455f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
32 changes: 16 additions & 16 deletions routers/web/repo/projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,11 +380,11 @@ func UpdateIssueProject(ctx *context.Context) {
}

if err := issues.LoadProjects(ctx); err != nil {
ctx.ServerError("LoadProjects", err)
ctx.JSONServerError("Load projects failed", err)
return
}
if _, err := issues.LoadRepositories(ctx); err != nil {
ctx.ServerError("LoadProjects", err)
ctx.JSONServerError("Load repositories failed", err)
return
}

Expand All @@ -393,18 +393,18 @@ func UpdateIssueProject(ctx *context.Context) {
if projectID > 0 {
dstProject, err := project_model.GetProjectByID(ctx, projectID)
if err != nil {
ctx.ServerError("GetProjectByID", err)
ctx.JSONServerError("Get project failed", err)
return
}
for _, issue := range issues {
if dstProject.RepoID != ctx.Repo.Repository.ID && dstProject.OwnerID != issue.Repo.OwnerID {
ctx.Error(http.StatusBadRequest, "project doesn't belong to the repository")
ctx.JSON(http.StatusBadRequest, "project doesn't belong to the repository")
return
}
}
dstDefaultColumn, err := dstProject.GetDefaultBoard(ctx)
if err != nil {
ctx.ServerError("GetDefaultBoard", err)
ctx.JSONServerError("Get default board failed", err)
return
}
dstColumnID = dstDefaultColumn.ID
Expand All @@ -418,7 +418,7 @@ func UpdateIssueProject(ctx *context.Context) {
}

if err := issues_model.ChangeProjectAssign(ctx, issue, ctx.Doer, projectID, dstColumnID); err != nil {
ctx.ServerError("ChangeProjectAssign", err)
ctx.JSONServerError("Change project assign failed", err)
return
}
}
Expand All @@ -445,16 +445,16 @@ func DeleteProjectBoard(ctx *context.Context) {
project, err := project_model.GetProjectByID(ctx, ctx.ParamsInt64(":id"))
if err != nil {
if project_model.IsErrProjectNotExist(err) {
ctx.NotFound("", nil)
ctx.JSONServerError("Project doesn't exist", err)
} else {
ctx.ServerError("GetProjectByID", err)
ctx.JSONServerError("Get project failed", err)
}
return
}

pb, err := project_model.GetBoard(ctx, ctx.ParamsInt64(":boardID"))
if err != nil {
ctx.ServerError("GetProjectBoard", err)
ctx.JSONServerError("Get project board failed", err)
return
}
if pb.ProjectID != ctx.ParamsInt64(":id") {
Expand All @@ -472,7 +472,7 @@ func DeleteProjectBoard(ctx *context.Context) {
}

if err := project_model.DeleteBoardByID(ctx, ctx.ParamsInt64(":boardID")); err != nil {
ctx.ServerError("DeleteProjectBoardByID", err)
ctx.JSONServerError("Delete project board failed", err)
return
}

Expand All @@ -492,9 +492,9 @@ func AddBoardToProjectPost(ctx *context.Context) {
project, err := project_model.GetProjectForRepoByID(ctx, ctx.Repo.Repository.ID, ctx.ParamsInt64(":id"))
if err != nil {
if project_model.IsErrProjectNotExist(err) {
ctx.NotFound("", nil)
ctx.JSONServerError("Project doesn't exist", err)
} else {
ctx.ServerError("GetProjectByID", err)
ctx.JSONServerError("Get project failed", err)
}
return
}
Expand All @@ -505,7 +505,7 @@ func AddBoardToProjectPost(ctx *context.Context) {
Color: form.Color,
CreatorID: ctx.Doer.ID,
}); err != nil {
ctx.ServerError("NewProjectBoard", err)
ctx.JSONServerError("create project board failed", err)
return
}

Expand All @@ -530,16 +530,16 @@ func checkProjectBoardChangePermissions(ctx *context.Context) (*project_model.Pr
project, err := project_model.GetProjectByID(ctx, ctx.ParamsInt64(":id"))
if err != nil {
if project_model.IsErrProjectNotExist(err) {
ctx.NotFound("", nil)
ctx.JSONServerError("Project doesn't exist", err)
} else {
ctx.ServerError("GetProjectByID", err)
ctx.JSONServerError("Get project failed", err)
}
return nil, nil
}

board, err := project_model.GetBoard(ctx, ctx.ParamsInt64(":boardID"))
if err != nil {
ctx.ServerError("GetProjectBoard", err)
ctx.JSONServerError("Get project board failed", err)
return nil, nil
}
if board.ProjectID != ctx.ParamsInt64(":id") {
Expand Down
12 changes: 12 additions & 0 deletions services/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"code.gitea.io/gitea/modules/cache"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/httpcache"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/session"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/templates"
Expand Down Expand Up @@ -267,3 +268,14 @@ func (ctx *Context) JSONError(msg any) {
panic(fmt.Sprintf("unsupported type: %T", msg))
}
}

func (ctx *Context) JSONServerError(msg string, err error) {
errorMessage := msg
if err != nil {
log.Log(1, log.ERROR, "%s: %v", msg, err)
if ctx.Doer != nil && ctx.Doer.IsAdmin {
errorMessage = fmt.Sprintf("%s: %v", errorMessage, err)
}
}
ctx.JSON(http.StatusBadRequest, map[string]any{"errorMessage": errorMessage, "renderFormat": "text"})
}

0 comments on commit 81d455f

Please sign in to comment.