Skip to content

Commit cac9e6e

Browse files
jolheisertechknowlogick
authored andcommitted
Updates to API 404 responses (#6077)
1 parent d10a668 commit cac9e6e

30 files changed

+120
-91
lines changed

modules/context/api.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
// Copyright 2016 The Gogs Authors. All rights reserved.
2+
// Copyright 2019 The Gitea Authors. All rights reserved.
23
// Use of this source code is governed by a MIT-style
34
// license that can be found in the LICENSE file.
45

56
package context
67

78
import (
89
"fmt"
10+
"net/url"
11+
"path"
912
"strings"
1013

1114
"github.com/go-macaron/csrf"
@@ -140,3 +143,29 @@ func ReferencesGitRepo() macaron.Handler {
140143
}
141144
}
142145
}
146+
147+
// NotFound handles 404s for APIContext
148+
// String will replace message, errors will be added to a slice
149+
func (ctx *APIContext) NotFound(objs ...interface{}) {
150+
var message = "Not Found"
151+
var errors []string
152+
for _, obj := range objs {
153+
if err, ok := obj.(error); ok {
154+
errors = append(errors, err.Error())
155+
} else {
156+
message = obj.(string)
157+
}
158+
}
159+
160+
u, err := url.Parse(setting.AppURL)
161+
if err != nil {
162+
ctx.Error(500, "Invalid AppURL", err)
163+
return
164+
}
165+
u.Path = path.Join(u.Path, "api", "swagger")
166+
ctx.JSON(404, map[string]interface{}{
167+
"message": message,
168+
"documentation_url": u.String(),
169+
"errors": errors,
170+
})
171+
}

routers/api/v1/admin/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func DeleteUserPublicKey(ctx *context.APIContext) {
288288

289289
if err := models.DeletePublicKey(u, ctx.ParamsInt64(":id")); err != nil {
290290
if models.IsErrKeyNotExist(err) {
291-
ctx.Status(404)
291+
ctx.NotFound()
292292
} else if models.IsErrKeyAccessDenied(err) {
293293
ctx.Error(403, "", "You do not have access to this key")
294294
} else {

routers/api/v1/api.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import (
7474
api "code.gitea.io/sdk/gitea"
7575

7676
"github.com/go-macaron/binding"
77-
macaron "gopkg.in/macaron.v1"
77+
"gopkg.in/macaron.v1"
7878
)
7979

8080
func sudo() macaron.Handler {
@@ -89,7 +89,7 @@ func sudo() macaron.Handler {
8989
user, err := models.GetUserByName(sudo)
9090
if err != nil {
9191
if models.IsErrUserNotExist(err) {
92-
ctx.Status(404)
92+
ctx.NotFound()
9393
} else {
9494
ctx.Error(500, "GetUserByName", err)
9595
}
@@ -124,7 +124,7 @@ func repoAssignment() macaron.Handler {
124124
owner, err = models.GetUserByName(userName)
125125
if err != nil {
126126
if models.IsErrUserNotExist(err) {
127-
ctx.Status(404)
127+
ctx.NotFound()
128128
} else {
129129
ctx.Error(500, "GetUserByName", err)
130130
}
@@ -141,7 +141,7 @@ func repoAssignment() macaron.Handler {
141141
if err == nil {
142142
context.RedirectToRepo(ctx.Context, redirectRepoID)
143143
} else if models.IsErrRepoRedirectNotExist(err) {
144-
ctx.Status(404)
144+
ctx.NotFound()
145145
} else {
146146
ctx.Error(500, "LookupRepoRedirect", err)
147147
}
@@ -160,7 +160,7 @@ func repoAssignment() macaron.Handler {
160160
}
161161

162162
if !ctx.Repo.HasAccess() {
163-
ctx.Status(404)
163+
ctx.NotFound()
164164
return
165165
}
166166
}
@@ -268,7 +268,7 @@ func reqOrgMembership() macaron.Handler {
268268
if ctx.Org.Organization != nil {
269269
ctx.Error(403, "", "Must be an organization member")
270270
} else {
271-
ctx.Status(404)
271+
ctx.NotFound()
272272
}
273273
return
274274
}
@@ -294,7 +294,7 @@ func reqOrgOwnership() macaron.Handler {
294294
if ctx.Org.Organization != nil {
295295
ctx.Error(403, "", "Must be an organization owner")
296296
} else {
297-
ctx.Status(404)
297+
ctx.NotFound()
298298
}
299299
return
300300
}
@@ -320,7 +320,7 @@ func orgAssignment(args ...bool) macaron.Handler {
320320
ctx.Org.Organization, err = models.GetOrgByName(ctx.Params(":orgname"))
321321
if err != nil {
322322
if models.IsErrOrgNotExist(err) {
323-
ctx.Status(404)
323+
ctx.NotFound()
324324
} else {
325325
ctx.Error(500, "GetOrgByName", err)
326326
}
@@ -332,7 +332,7 @@ func orgAssignment(args ...bool) macaron.Handler {
332332
ctx.Org.Team, err = models.GetTeamByID(ctx.ParamsInt64(":teamid"))
333333
if err != nil {
334334
if models.IsErrUserNotExist(err) {
335-
ctx.Status(404)
335+
ctx.NotFound()
336336
} else {
337337
ctx.Error(500, "GetTeamById", err)
338338
}
@@ -344,36 +344,36 @@ func orgAssignment(args ...bool) macaron.Handler {
344344

345345
func mustEnableIssues(ctx *context.APIContext) {
346346
if !ctx.Repo.CanRead(models.UnitTypeIssues) {
347-
ctx.Status(404)
347+
ctx.NotFound()
348348
return
349349
}
350350
}
351351

352-
func mustAllowPulls(ctx *context.Context) {
352+
func mustAllowPulls(ctx *context.APIContext) {
353353
if !(ctx.Repo.Repository.CanEnablePulls() && ctx.Repo.CanRead(models.UnitTypePullRequests)) {
354-
ctx.Status(404)
354+
ctx.NotFound()
355355
return
356356
}
357357
}
358358

359-
func mustEnableIssuesOrPulls(ctx *context.Context) {
359+
func mustEnableIssuesOrPulls(ctx *context.APIContext) {
360360
if !ctx.Repo.CanRead(models.UnitTypeIssues) &&
361361
!(ctx.Repo.Repository.CanEnablePulls() && ctx.Repo.CanRead(models.UnitTypePullRequests)) {
362-
ctx.Status(404)
362+
ctx.NotFound()
363363
return
364364
}
365365
}
366366

367-
func mustEnableUserHeatmap(ctx *context.Context) {
367+
func mustEnableUserHeatmap(ctx *context.APIContext) {
368368
if !setting.Service.EnableUserHeatmap {
369-
ctx.Status(404)
369+
ctx.NotFound()
370370
return
371371
}
372372
}
373373

374-
func mustNotBeArchived(ctx *context.Context) {
374+
func mustNotBeArchived(ctx *context.APIContext) {
375375
if ctx.Repo.Repository.IsArchived {
376-
ctx.Status(404)
376+
ctx.NotFound()
377377
return
378378
}
379379
}
@@ -683,8 +683,8 @@ func RegisterRoutes(m *macaron.Macaron) {
683683
})
684684
}, orgAssignment(false, true), reqToken(), reqOrgMembership())
685685

686-
m.Any("/*", func(ctx *context.Context) {
687-
ctx.Error(404)
686+
m.Any("/*", func(ctx *context.APIContext) {
687+
ctx.NotFound()
688688
})
689689

690690
m.Group("/admin", func() {

routers/api/v1/org/hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ func DeleteHook(ctx *context.APIContext) {
164164
hookID := ctx.ParamsInt64(":id")
165165
if err := models.DeleteWebhookByOrgID(org.ID, hookID); err != nil {
166166
if models.IsErrWebhookNotExist(err) {
167-
ctx.Status(404)
167+
ctx.NotFound()
168168
} else {
169169
ctx.Error(500, "DeleteWebhookByOrgID", err)
170170
}

routers/api/v1/org/member.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,11 @@ func IsMember(ctx *context.APIContext) {
135135
} else if userToCheckIsMember {
136136
ctx.Status(204)
137137
} else {
138-
ctx.Status(404)
138+
ctx.NotFound()
139139
}
140140
return
141141
} else if ctx.User.ID == userToCheck.ID {
142-
ctx.Status(404)
142+
ctx.NotFound()
143143
return
144144
}
145145
}
@@ -177,7 +177,7 @@ func IsPublicMember(ctx *context.APIContext) {
177177
if userToCheck.IsPublicMember(ctx.Org.Organization.ID) {
178178
ctx.Status(204)
179179
} else {
180-
ctx.Status(404)
180+
ctx.NotFound()
181181
}
182182
}
183183

routers/api/v1/org/team.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ func GetTeamMembers(ctx *context.APIContext) {
242242
ctx.Error(500, "IsOrganizationMember", err)
243243
return
244244
} else if !isMember {
245-
ctx.Status(404)
245+
ctx.NotFound()
246246
return
247247
}
248248
team := ctx.Org.Team
@@ -391,7 +391,7 @@ func getRepositoryByParams(ctx *context.APIContext) *models.Repository {
391391
repo, err := models.GetRepositoryByName(ctx.Org.Team.OrgID, ctx.Params(":reponame"))
392392
if err != nil {
393393
if models.IsErrRepoNotExist(err) {
394-
ctx.Status(404)
394+
ctx.NotFound()
395395
} else {
396396
ctx.Error(500, "GetRepositoryByName", err)
397397
}

routers/api/v1/repo/branch.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ func GetBranch(ctx *context.APIContext) {
4242
// if TreePath != "", then URL contained extra slashes
4343
// (i.e. "master/subbranch" instead of "master"), so branch does
4444
// not exist
45-
ctx.Status(404)
45+
ctx.NotFound()
4646
return
4747
}
4848
branch, err := ctx.Repo.Repository.GetBranch(ctx.Repo.BranchName)
4949
if err != nil {
5050
if models.IsErrBranchNotExist(err) {
51-
ctx.Error(404, "GetBranch", err)
51+
ctx.NotFound(err)
5252
} else {
5353
ctx.Error(500, "GetBranch", err)
5454
}

routers/api/v1/repo/collaborators.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func IsCollaborator(ctx *context.APIContext) {
9292
if isColab {
9393
ctx.Status(204)
9494
} else {
95-
ctx.Status(404)
95+
ctx.NotFound()
9696
}
9797
}
9898

routers/api/v1/repo/file.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,14 @@ func GetRawFile(ctx *context.APIContext) {
4040
// 200:
4141
// description: success
4242
if ctx.Repo.Repository.IsEmpty {
43-
ctx.Status(404)
43+
ctx.NotFound()
4444
return
4545
}
4646

4747
blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
4848
if err != nil {
4949
if git.IsErrNotExist(err) {
50-
ctx.Status(404)
50+
ctx.NotFound()
5151
} else {
5252
ctx.Error(500, "GetBlobByPath", err)
5353
}
@@ -124,7 +124,7 @@ func GetEditorconfig(ctx *context.APIContext) {
124124
ec, err := ctx.Repo.GetEditorconfig()
125125
if err != nil {
126126
if git.IsErrNotExist(err) {
127-
ctx.Error(404, "GetEditorconfig", err)
127+
ctx.NotFound(err)
128128
} else {
129129
ctx.Error(500, "GetEditorconfig", err)
130130
}
@@ -134,7 +134,7 @@ func GetEditorconfig(ctx *context.APIContext) {
134134
fileName := ctx.Params("filename")
135135
def := ec.GetDefinitionForFilename(fileName)
136136
if def == nil {
137-
ctx.Error(404, "GetDefinitionForFilename", err)
137+
ctx.NotFound(err)
138138
return
139139
}
140140
ctx.JSON(200, def)

routers/api/v1/repo/git_ref.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func getGitRefsInternal(ctx *context.APIContext, filter string) {
8989
}
9090

9191
if len(refs) == 0 {
92-
ctx.Status(404)
92+
ctx.NotFound()
9393
return
9494
}
9595

routers/api/v1/repo/hook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func DeleteHook(ctx *context.APIContext) {
239239
// "$ref": "#/responses/notFound"
240240
if err := models.DeleteWebhookByRepoID(ctx.Repo.Repository.ID, ctx.ParamsInt64(":id")); err != nil {
241241
if models.IsErrWebhookNotExist(err) {
242-
ctx.Status(404)
242+
ctx.NotFound()
243243
} else {
244244
ctx.Error(500, "DeleteWebhookByRepoID", err)
245245
}

routers/api/v1/repo/issue.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ func GetIssue(ctx *context.APIContext) {
146146
issue, err := models.GetIssueWithAttrsByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
147147
if err != nil {
148148
if models.IsErrIssueNotExist(err) {
149-
ctx.Status(404)
149+
ctx.NotFound()
150150
} else {
151151
ctx.Error(500, "GetIssueByIndex", err)
152152
}
@@ -283,7 +283,7 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) {
283283
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
284284
if err != nil {
285285
if models.IsErrIssueNotExist(err) {
286-
ctx.Status(404)
286+
ctx.NotFound()
287287
} else {
288288
ctx.Error(500, "GetIssueByIndex", err)
289289
}
@@ -412,7 +412,7 @@ func UpdateIssueDeadline(ctx *context.APIContext, form api.EditDeadlineOption) {
412412
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
413413
if err != nil {
414414
if models.IsErrIssueNotExist(err) {
415-
ctx.Status(404)
415+
ctx.NotFound()
416416
} else {
417417
ctx.Error(500, "GetIssueByIndex", err)
418418
}
@@ -478,7 +478,7 @@ func StartIssueStopwatch(ctx *context.APIContext) {
478478
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
479479
if err != nil {
480480
if models.IsErrIssueNotExist(err) {
481-
ctx.Status(404)
481+
ctx.NotFound()
482482
} else {
483483
ctx.Error(500, "GetIssueByIndex", err)
484484
}
@@ -547,7 +547,7 @@ func StopIssueStopwatch(ctx *context.APIContext) {
547547
issue, err := models.GetIssueByIndex(ctx.Repo.Repository.ID, ctx.ParamsInt64(":index"))
548548
if err != nil {
549549
if models.IsErrIssueNotExist(err) {
550-
ctx.Status(404)
550+
ctx.NotFound()
551551
} else {
552552
ctx.Error(500, "GetIssueByIndex", err)
553553
}

routers/api/v1/repo/issue_comment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
268268
comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
269269
if err != nil {
270270
if models.IsErrCommentNotExist(err) {
271-
ctx.Error(404, "GetCommentByID", err)
271+
ctx.NotFound(err)
272272
} else {
273273
ctx.Error(500, "GetCommentByID", err)
274274
}
@@ -361,7 +361,7 @@ func deleteIssueComment(ctx *context.APIContext) {
361361
comment, err := models.GetCommentByID(ctx.ParamsInt64(":id"))
362362
if err != nil {
363363
if models.IsErrCommentNotExist(err) {
364-
ctx.Error(404, "GetCommentByID", err)
364+
ctx.NotFound(err)
365365
} else {
366366
ctx.Error(500, "GetCommentByID", err)
367367
}

0 commit comments

Comments
 (0)