Skip to content

Commit a4f4b78

Browse files
committed
Return 404 NotFound if requested attachment does not exist
Add code to test if GetAttachmentByID returns an ErrAttachmentNotExist error and return NotFound instead of InternalServerError Fix go-gitea#20884 Signed-off-by: Andrew Thornton <[email protected]>
1 parent 6784a70 commit a4f4b78

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

routers/api/v1/repo/release_attachment.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ func GetReleaseAttachment(ctx *context.APIContext) {
5757
attachID := ctx.ParamsInt64(":asset")
5858
attach, err := repo_model.GetAttachmentByID(ctx, attachID)
5959
if err != nil {
60+
if repo_model.IsErrAttachmentNotExist(err) {
61+
ctx.NotFound()
62+
return
63+
}
6064
ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err)
6165
return
6266
}
@@ -100,6 +104,10 @@ func ListReleaseAttachments(ctx *context.APIContext) {
100104
releaseID := ctx.ParamsInt64(":id")
101105
release, err := models.GetReleaseByID(ctx, releaseID)
102106
if err != nil {
107+
if repo_model.IsErrAttachmentNotExist(err) {
108+
ctx.NotFound()
109+
return
110+
}
103111
ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err)
104112
return
105113
}
@@ -166,6 +174,10 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
166174
releaseID := ctx.ParamsInt64(":id")
167175
release, err := models.GetReleaseByID(ctx, releaseID)
168176
if err != nil {
177+
if repo_model.IsErrAttachmentNotExist(err) {
178+
ctx.NotFound()
179+
return
180+
}
169181
ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err)
170182
return
171183
}
@@ -244,6 +256,10 @@ func EditReleaseAttachment(ctx *context.APIContext) {
244256
attachID := ctx.ParamsInt64(":asset")
245257
attach, err := repo_model.GetAttachmentByID(ctx, attachID)
246258
if err != nil {
259+
if repo_model.IsErrAttachmentNotExist(err) {
260+
ctx.NotFound()
261+
return
262+
}
247263
ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err)
248264
return
249265
}
@@ -302,6 +318,10 @@ func DeleteReleaseAttachment(ctx *context.APIContext) {
302318
attachID := ctx.ParamsInt64(":asset")
303319
attach, err := repo_model.GetAttachmentByID(ctx, attachID)
304320
if err != nil {
321+
if repo_model.IsErrAttachmentNotExist(err) {
322+
ctx.NotFound()
323+
return
324+
}
305325
ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err)
306326
return
307327
}

0 commit comments

Comments
 (0)