diff options
author | wxiaoguang <wxiaoguang@gmail.com> | 2025-02-18 04:41:03 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-17 12:41:03 -0800 |
commit | 15e020eec85d7ec2ef0f1e9ad10a1ffde2ca0f34 (patch) | |
tree | 57625d44a60e5c01bcbf712695fa576aa79683f4 /services | |
parent | 7df09e31fa2700454beecbaf3c0721e13d6086f4 (diff) | |
download | gitea-15e020eec85d7ec2ef0f1e9ad10a1ffde2ca0f34.tar.gz gitea-15e020eec85d7ec2ef0f1e9ad10a1ffde2ca0f34.zip |
Refactor error system (#33626)
Diffstat (limited to 'services')
-rw-r--r-- | services/actions/workflow.go | 2 | ||||
-rw-r--r-- | services/context/api.go | 16 | ||||
-rw-r--r-- | services/context/user.go | 2 |
3 files changed, 13 insertions, 7 deletions
diff --git a/services/actions/workflow.go b/services/actions/workflow.go index ab320aa453..9aecad171b 100644 --- a/services/actions/workflow.go +++ b/services/actions/workflow.go @@ -104,7 +104,7 @@ func EnableOrDisableWorkflow(ctx *context.APIContext, workflowID string, isEnabl func ListActionWorkflows(ctx *context.APIContext) ([]*api.ActionWorkflow, error) { defaultBranchCommit, err := ctx.Repo.GitRepo.GetBranchCommit(ctx.Repo.Repository.DefaultBranch) if err != nil { - ctx.APIError(http.StatusInternalServerError, err.Error()) + ctx.APIErrorInternal(err) return nil, err } diff --git a/services/context/api.go b/services/context/api.go index 9c3ee2a4ac..230c3456d1 100644 --- a/services/context/api.go +++ b/services/context/api.go @@ -5,6 +5,7 @@ package context import ( + "errors" "fmt" "net/http" "net/url" @@ -17,6 +18,7 @@ import ( "code.gitea.io/gitea/modules/httpcache" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" + "code.gitea.io/gitea/modules/util" "code.gitea.io/gitea/modules/web" web_types "code.gitea.io/gitea/modules/web/types" ) @@ -108,7 +110,11 @@ type APIRepoArchivedError struct { // APIErrorInternal responds with error message, status is 500 func (ctx *APIContext) APIErrorInternal(err error) { - log.ErrorWithSkip(1, "InternalServerError: %v", err) + ctx.apiErrorInternal(1, err) +} + +func (ctx *APIContext) apiErrorInternal(skip int, err error) { + log.ErrorWithSkip(skip+1, "InternalServerError: %v", err) var message string if !setting.IsProd || (ctx.Doer != nil && ctx.Doer.IsAdmin) { @@ -273,7 +279,7 @@ func ReferencesGitRepo(allowEmpty ...bool) func(ctx *APIContext) { var err error ctx.Repo.GitRepo, err = gitrepo.RepositoryFromRequestContextOrOpen(ctx, ctx.Repo.Repository) if err != nil { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) return } } @@ -344,12 +350,12 @@ func (ctx *APIContext) GetErrMsg() string { // 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. -func (ctx *APIContext) NotFoundOrServerError(logMsg string, errCheck func(error) bool, logErr error) { - if errCheck(logErr) { +func (ctx *APIContext) NotFoundOrServerError(err error) { + if errors.Is(err, util.ErrNotExist) { ctx.JSON(http.StatusNotFound, nil) return } - ctx.APIError(http.StatusInternalServerError, logMsg) + ctx.APIErrorInternal(err) } // IsUserSiteAdmin returns true if current user is a site admin diff --git a/services/context/user.go b/services/context/user.go index 4037354955..c09ded8339 100644 --- a/services/context/user.go +++ b/services/context/user.go @@ -44,7 +44,7 @@ func UserIDAssignmentAPI() func(ctx *APIContext) { if user_model.IsErrUserNotExist(err) { ctx.APIError(http.StatusNotFound, err) } else { - ctx.APIError(http.StatusInternalServerError, err) + ctx.APIErrorInternal(err) } } } |