Skip to content

Commit 8012a7e

Browse files
committed
fix "not found" usage
1 parent ad5234f commit 8012a7e

File tree

4 files changed

+13
-5
lines changed

4 files changed

+13
-5
lines changed

routers/web/repo/actions/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ func Delete(ctx *context_module.Context) {
584584
run, err := actions_model.GetRunByIndex(ctx, repoID, runIndex)
585585
if err != nil {
586586
if errors.Is(err, util.ErrNotExist) {
587-
ctx.JSONError(ctx.Tr("error.not_found"))
587+
ctx.JSONErrorNotFound()
588588
return
589589
}
590590
ctx.ServerError("GetRunByIndex", err)

routers/web/repo/branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func MergeUpstream(ctx *context.Context) {
264264
_, err := repo_service.MergeUpstream(ctx, ctx.Doer, ctx.Repo.Repository, branchName)
265265
if err != nil {
266266
if errors.Is(err, util.ErrNotExist) {
267-
ctx.JSONError(ctx.Tr("error.not_found"))
267+
ctx.JSONErrorNotFound()
268268
return
269269
} else if pull_service.IsErrMergeConflicts(err) {
270270
ctx.JSONError(ctx.Tr("repo.pulls.merge_conflict"))

services/context/api.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ func APIContexter() func(http.Handler) http.Handler {
245245
// APIErrorNotFound handles 404s for APIContext
246246
// String will replace message, errors will be added to a slice
247247
func (ctx *APIContext) APIErrorNotFound(objs ...any) {
248-
message := ctx.Locale.TrString("error.not_found")
248+
var message string
249249
var errs []string
250250
for _, obj := range objs {
251251
// Ignore nil
@@ -259,9 +259,8 @@ func (ctx *APIContext) APIErrorNotFound(objs ...any) {
259259
message = obj.(string)
260260
}
261261
}
262-
263262
ctx.JSON(http.StatusNotFound, map[string]any{
264-
"message": message,
263+
"message": util.IfZero(message, "not found"), // do not use locale in API
265264
"url": setting.API.SwaggerURL,
266265
"errors": errs,
267266
})

services/context/context.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"code.gitea.io/gitea/modules/setting"
2424
"code.gitea.io/gitea/modules/templates"
2525
"code.gitea.io/gitea/modules/translation"
26+
"code.gitea.io/gitea/modules/util"
2627
"code.gitea.io/gitea/modules/web"
2728
"code.gitea.io/gitea/modules/web/middleware"
2829
web_types "code.gitea.io/gitea/modules/web/types"
@@ -261,3 +262,11 @@ func (ctx *Context) JSONError(msg any) {
261262
panic(fmt.Sprintf("unsupported type: %T", msg))
262263
}
263264
}
265+
266+
func (ctx *Context) JSONErrorNotFound(optMsg ...string) {
267+
msg := util.OptionalArg(optMsg)
268+
if msg == "" {
269+
msg = ctx.Locale.TrString("error.not_found")
270+
}
271+
ctx.JSON(http.StatusNotFound, map[string]any{"errorMessage": msg, "renderFormat": "text"})
272+
}

0 commit comments

Comments
 (0)