diff --git a/routers/web/admin/users.go b/routers/web/admin/users.go index ed48c3d5683..45557c4bf51 100644 --- a/routers/web/admin/users.go +++ b/routers/web/admin/users.go @@ -366,7 +366,7 @@ func ViewUser(ctx *context.Context) { func getTargetBot(ctx *context.Context) *user_model.User { u, err := user_model.GetUserByID(ctx, ctx.PathParamInt64("userid")) if err != nil { - ctx.NotFoundOrServerError("GetUserByID", user_model.IsErrUserNotExist, err) + ctx.ServerError("GetUserByID", err) return nil } if !u.IsTypeBot() { diff --git a/routers/web/org/projects.go b/routers/web/org/projects.go index 4cf346aef84..33719637a2a 100644 --- a/routers/web/org/projects.go +++ b/routers/web/org/projects.go @@ -195,12 +195,12 @@ func ChangeProjectStatus(ctx *context.Context) { project, err := project_model.GetProjectByIDAndOwner(ctx, id, ctx.ContextUser.ID) if err != nil { - ctx.NotFoundOrServerError("GetProjectByID", project_model.IsErrProjectNotExist, err) + ctx.ServerError("GetProjectByID", err) return } if err := project_model.ChangeProjectStatusByRepoIDAndID(ctx, 0, project.ID, toClose); err != nil { - ctx.NotFoundOrServerError("ChangeProjectStatusByRepoIDAndID", project_model.IsErrProjectNotExist, err) + ctx.ServerError("ChangeProjectStatusByRepoIDAndID", err) return } ctx.JSONRedirect(project_model.ProjectLinkForOrg(ctx.ContextUser, project.ID)) @@ -210,7 +210,7 @@ func ChangeProjectStatus(ctx *context.Context) { func DeleteProject(ctx *context.Context) { p, err := project_model.GetProjectByIDAndOwner(ctx, ctx.PathParamInt64("id"), ctx.ContextUser.ID) if err != nil { - ctx.NotFoundOrServerError("GetProjectByID", project_model.IsErrProjectNotExist, err) + ctx.ServerError("GetProjectByID", err) return } @@ -239,7 +239,7 @@ func RenderEditProject(ctx *context.Context) { p, err := project_model.GetProjectByIDAndOwner(ctx, ctx.PathParamInt64("id"), ctx.ContextUser.ID) if err != nil { - ctx.NotFoundOrServerError("GetProjectByID", project_model.IsErrProjectNotExist, err) + ctx.ServerError("GetProjectByID", err) return } @@ -277,7 +277,7 @@ func EditProjectPost(ctx *context.Context) { p, err := project_model.GetProjectByIDAndOwner(ctx, projectID, ctx.ContextUser.ID) if err != nil { - ctx.NotFoundOrServerError("GetProjectByID", project_model.IsErrProjectNotExist, err) + ctx.ServerError("GetProjectByID", err) return } @@ -302,7 +302,7 @@ func EditProjectPost(ctx *context.Context) { func ViewProject(ctx *context.Context) { project, err := project_model.GetProjectByIDAndOwner(ctx, ctx.PathParamInt64("id"), ctx.ContextUser.ID) if err != nil { - ctx.NotFoundOrServerError("GetProjectByID", project_model.IsErrProjectNotExist, err) + ctx.ServerError("GetProjectByID", err) return } diff --git a/routers/web/repo/commit.go b/routers/web/repo/commit.go index 7ce02765905..94c4920ac61 100644 --- a/routers/web/repo/commit.go +++ b/routers/web/repo/commit.go @@ -270,7 +270,7 @@ func LoadBranchesAndTags(ctx *context.Context) { ctx.JSON(http.StatusOK, response) return } - ctx.NotFoundOrServerError(fmt.Sprintf("could not load branches and tags the commit %s belongs to", ctx.PathParam("sha")), git.IsErrNotExist, err) + ctx.ServerError("LoadBranchesAndTags", err) } // Diff show different from current commit to previous commit diff --git a/routers/web/repo/editor.go b/routers/web/repo/editor.go index d65186e3f50..616c2552831 100644 --- a/routers/web/repo/editor.go +++ b/routers/web/repo/editor.go @@ -405,7 +405,7 @@ func DeleteFilePost(ctx *context.Context) { // Check if the path is a directory entry, err := ctx.Repo.Commit.GetTreeEntryByPath(ctx, ctx.Repo.GitRepo, treePath) if err != nil { - ctx.NotFoundOrServerError("GetTreeEntryByPath", git.IsErrNotExist, err) + ctx.ServerError("GetTreeEntryByPath", err) return } diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index 511085bcdd4..b044257198e 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -182,7 +182,7 @@ func retrieveProjectsInternal(ctx *context.Context, repo *repo_model.Repository) func GetActionIssue(ctx *context.Context) *issues_model.Issue { issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) if err != nil { - ctx.NotFoundOrServerError("GetIssueByIndex", issues_model.IsErrIssueNotExist, err) + ctx.ServerError("GetIssueByIndex", err) return nil } issue.Repo = ctx.Repo.Repository diff --git a/routers/web/repo/issue_comment.go b/routers/web/repo/issue_comment.go index 478c03f28ef..04c8814ee8f 100644 --- a/routers/web/repo/issue_comment.go +++ b/routers/web/repo/issue_comment.go @@ -150,12 +150,12 @@ func NewComment(ctx *context.Context) { func UpdateCommentContent(ctx *context.Context) { comment, err := issues_model.GetCommentByID(ctx, ctx.PathParamInt64("id")) if err != nil { - ctx.NotFoundOrServerError("GetCommentByID", issues_model.IsErrCommentNotExist, err) + ctx.ServerError("GetCommentByID", err) return } if err := comment.LoadIssue(ctx); err != nil { - ctx.NotFoundOrServerError("LoadIssue", issues_model.IsErrIssueNotExist, err) + ctx.ServerError("LoadIssue", err) return } @@ -234,12 +234,12 @@ func UpdateCommentContent(ctx *context.Context) { func DeleteComment(ctx *context.Context) { comment, err := issues_model.GetCommentByID(ctx, ctx.PathParamInt64("id")) if err != nil { - ctx.NotFoundOrServerError("GetCommentByID", issues_model.IsErrCommentNotExist, err) + ctx.ServerError("GetCommentByID", err) return } if err := comment.LoadIssue(ctx); err != nil { - ctx.NotFoundOrServerError("LoadIssue", issues_model.IsErrIssueNotExist, err) + ctx.ServerError("LoadIssue", err) return } @@ -269,12 +269,12 @@ func ChangeCommentReaction(ctx *context.Context) { form := web.GetForm[*forms.ReactionForm](ctx) comment, err := issues_model.GetCommentByID(ctx, ctx.PathParamInt64("id")) if err != nil { - ctx.NotFoundOrServerError("GetCommentByID", issues_model.IsErrCommentNotExist, err) + ctx.ServerError("GetCommentByID", err) return } if err := comment.LoadIssue(ctx); err != nil { - ctx.NotFoundOrServerError("LoadIssue", issues_model.IsErrIssueNotExist, err) + ctx.ServerError("LoadIssue", err) return } @@ -374,12 +374,12 @@ func ChangeCommentReaction(ctx *context.Context) { func GetCommentAttachments(ctx *context.Context) { comment, err := issues_model.GetCommentByID(ctx, ctx.PathParamInt64("id")) if err != nil { - ctx.NotFoundOrServerError("GetCommentByID", issues_model.IsErrCommentNotExist, err) + ctx.ServerError("GetCommentByID", err) return } if err := comment.LoadIssue(ctx); err != nil { - ctx.NotFoundOrServerError("LoadIssue", issues_model.IsErrIssueNotExist, err) + ctx.ServerError("LoadIssue", err) return } diff --git a/routers/web/repo/issue_view.go b/routers/web/repo/issue_view.go index 06d55f938e2..f4f57ee1497 100644 --- a/routers/web/repo/issue_view.go +++ b/routers/web/repo/issue_view.go @@ -286,7 +286,7 @@ func combineLabelComments(issue *issues_model.Issue) { func prepareIssueViewLoad(ctx *context.Context) *issues_model.Issue { issue, err := issues_model.GetIssueByIndex(ctx, ctx.Repo.Repository.ID, ctx.PathParamInt64("index")) if err != nil { - ctx.NotFoundOrServerError("GetIssueByIndex", issues_model.IsErrIssueNotExist, err) + ctx.ServerError("GetIssueByIndex", err) return nil } issue.Repo = ctx.Repo.Repository diff --git a/routers/web/repo/projects.go b/routers/web/repo/projects.go index ab1ec7bbd64..872a78c930b 100644 --- a/routers/web/repo/projects.go +++ b/routers/web/repo/projects.go @@ -170,7 +170,7 @@ func ChangeProjectStatus(ctx *context.Context) { id := ctx.PathParamInt64("id") if err := project_model.ChangeProjectStatusByRepoIDAndID(ctx, ctx.Repo.Repository.ID, id, toClose); err != nil { - ctx.NotFoundOrServerError("ChangeProjectStatusByRepoIDAndID", project_model.IsErrProjectNotExist, err) + ctx.ServerError("ChangeProjectStatusByRepoIDAndID", err) return } ctx.JSONRedirect(project_model.ProjectLinkForRepo(ctx.Repo.Repository, id)) @@ -475,12 +475,12 @@ func UpdateIssueProject(ctx *context.Context) { func UpdateIssueProjectColumn(ctx *context.Context) { issue, err := issues_model.GetIssueByRepoID(ctx, ctx.Repo.Repository.ID, ctx.FormInt64("issue_id")) if err != nil { - ctx.NotFoundOrServerError("GetIssueByID", issues_model.IsErrIssueNotExist, err) + ctx.ServerError("GetIssueByID", err) return } column, err := project_model.GetColumn(ctx, ctx.FormInt64("id")) if err != nil { - ctx.NotFoundOrServerError("GetColumn", project_model.IsErrProjectColumnNotExist, err) + ctx.ServerError("GetColumn", err) return } diff --git a/routers/web/repo/repo.go b/routers/web/repo/repo.go index 4da2f8484c7..40d9f279ff7 100644 --- a/routers/web/repo/repo.go +++ b/routers/web/repo/repo.go @@ -295,7 +295,7 @@ func handleRepoActionError(ctx *context.Context, err error) { case errors.Is(err, util.ErrPermissionDenied): ctx.JSONError(ctx.Tr("error.permission_denied")) default: - ctx.ServerError(fmt.Sprintf("Action (%s)", ctx.PathParam("action")), err) + ctx.ServerError("RepoAction:"+ctx.PathParam("action"), err) } } diff --git a/routers/web/shared/project/project.go b/routers/web/shared/project/project.go index 999e4ec62a1..599544f427e 100644 --- a/routers/web/shared/project/project.go +++ b/routers/web/shared/project/project.go @@ -26,7 +26,7 @@ func findProject(ctx *context.Context) *project_model.Project { project, err = project_model.GetProjectByIDAndOwner(ctx, ctx.PathParamInt64("id"), ctx.ContextUser.ID) } if err != nil { - ctx.NotFoundOrServerError("GetProject", project_model.IsErrProjectNotExist, err) + ctx.ServerError("GetProject", err) return nil } return project @@ -39,7 +39,7 @@ func findColumn(ctx *context.Context) (*project_model.Project, *project_model.Co } column, err := project_model.GetColumnByIDAndProjectID(ctx, ctx.PathParamInt64("columnID"), project.ID) if err != nil { - ctx.NotFoundOrServerError("GetColumnByIDAndProjectID", project_model.IsErrProjectColumnNotExist, err) + ctx.ServerError("GetColumnByIDAndProjectID", err) return nil, nil } return project, column @@ -175,7 +175,7 @@ func MoveIssues(ctx *context.Context) { } movedIssues, err := issues_model.GetIssuesByIDs(ctx, issueIDs) if err != nil { - ctx.NotFoundOrServerError("GetIssueByID", issues_model.IsErrIssueNotExist, err) + ctx.ServerError("GetIssueByID", err) return } diff --git a/routers/web/user/avatar.go b/routers/web/user/avatar.go index fa179758f63..080413744a4 100644 --- a/routers/web/user/avatar.go +++ b/routers/web/user/avatar.go @@ -27,7 +27,7 @@ func AvatarByUsernameSize(ctx *context.Context) { if user == nil { var err error if user, err = user_model.GetUserByName(ctx, username); err != nil { - ctx.NotFoundOrServerError("GetUserByName", user_model.IsErrUserNotExist, err) + ctx.ServerError("GetUserByName", err) return } } diff --git a/routers/web/user/setting/oauth2_common.go b/routers/web/user/setting/oauth2_common.go index 86dd41fce3a..a882b8b93fe 100644 --- a/routers/web/user/setting/oauth2_common.go +++ b/routers/web/user/setting/oauth2_common.go @@ -177,7 +177,7 @@ func (oa *OAuth2CommonHandlers) RegenerateSecret(ctx *context.Context) { func (oa *OAuth2CommonHandlers) DeleteApp(ctx *context.Context) { app, err := auth.GetOAuth2ApplicationByID(ctx, ctx.PathParamInt64("id")) if err != nil { - ctx.NotFoundOrServerError("GetOAuth2ApplicationByID", auth.IsErrOAuthApplicationNotFound, err) + ctx.ServerError("GetOAuth2ApplicationByID", err) return } @@ -208,7 +208,7 @@ func (oa *OAuth2CommonHandlers) RevokeGrant(ctx *context.Context) { app, err := auth.GetOAuth2ApplicationByID(ctx, grant.ApplicationID) if err != nil { - ctx.NotFoundOrServerError("GetOAuth2ApplicationByID", auth.IsErrOAuthApplicationNotFound, err) + ctx.ServerError("GetOAuth2ApplicationByID", err) return } diff --git a/services/context/context_response.go b/services/context/context_response.go index bd342c43fa4..cbaccfc20c7 100644 --- a/services/context/context_response.go +++ b/services/context/context_response.go @@ -190,15 +190,3 @@ func (ctx *Context) serverErrorInternal(skip int, logMsg string, logErr error) { ctx.Data["ErrorMsg"] = userErrorMsg ctx.HTML(http.StatusInternalServerError, tplStatus500) } - -// NotFoundOrServerError use error check function to determine if the error -// is about not found. It responds with 404 status code for not found error, -// or error context description for logging purpose of 500 server error. -// TODO: remove the "errCheck" and use util.ErrNotFound to check -func (ctx *Context) NotFoundOrServerError(logMsg string, errCheck func(error) bool, logErr error) { - if errCheck(logErr) { - ctx.notFoundInternal(1, logMsg, logErr) - return - } - ctx.serverErrorInternal(1, logMsg, logErr) -}