From a4f4b789958b34bcc9e80c6e279f89d78213f365 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 21 Aug 2022 09:30:46 +0100 Subject: [PATCH 1/4] 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 #20884 Signed-off-by: Andrew Thornton --- routers/api/v1/repo/release_attachment.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/routers/api/v1/repo/release_attachment.go b/routers/api/v1/repo/release_attachment.go index 8694653c06059..bf552417674c3 100644 --- a/routers/api/v1/repo/release_attachment.go +++ b/routers/api/v1/repo/release_attachment.go @@ -57,6 +57,10 @@ func GetReleaseAttachment(ctx *context.APIContext) { attachID := ctx.ParamsInt64(":asset") attach, err := repo_model.GetAttachmentByID(ctx, attachID) if err != nil { + if repo_model.IsErrAttachmentNotExist(err) { + ctx.NotFound() + return + } ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err) return } @@ -100,6 +104,10 @@ func ListReleaseAttachments(ctx *context.APIContext) { releaseID := ctx.ParamsInt64(":id") release, err := models.GetReleaseByID(ctx, releaseID) if err != nil { + if repo_model.IsErrAttachmentNotExist(err) { + ctx.NotFound() + return + } ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err) return } @@ -166,6 +174,10 @@ func CreateReleaseAttachment(ctx *context.APIContext) { releaseID := ctx.ParamsInt64(":id") release, err := models.GetReleaseByID(ctx, releaseID) if err != nil { + if repo_model.IsErrAttachmentNotExist(err) { + ctx.NotFound() + return + } ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err) return } @@ -244,6 +256,10 @@ func EditReleaseAttachment(ctx *context.APIContext) { attachID := ctx.ParamsInt64(":asset") attach, err := repo_model.GetAttachmentByID(ctx, attachID) if err != nil { + if repo_model.IsErrAttachmentNotExist(err) { + ctx.NotFound() + return + } ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err) return } @@ -302,6 +318,10 @@ func DeleteReleaseAttachment(ctx *context.APIContext) { attachID := ctx.ParamsInt64(":asset") attach, err := repo_model.GetAttachmentByID(ctx, attachID) if err != nil { + if repo_model.IsErrAttachmentNotExist(err) { + ctx.NotFound() + return + } ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err) return } From 90babdc88128eeeafcc215f94abc077a83ecf7e2 Mon Sep 17 00:00:00 2001 From: zeripath Date: Mon, 22 Aug 2022 14:41:02 +0100 Subject: [PATCH 2/4] Update routers/api/v1/repo/release_attachment.go --- routers/api/v1/repo/release_attachment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/repo/release_attachment.go b/routers/api/v1/repo/release_attachment.go index bf552417674c3..21950354ab5da 100644 --- a/routers/api/v1/repo/release_attachment.go +++ b/routers/api/v1/repo/release_attachment.go @@ -104,7 +104,7 @@ func ListReleaseAttachments(ctx *context.APIContext) { releaseID := ctx.ParamsInt64(":id") release, err := models.GetReleaseByID(ctx, releaseID) if err != nil { - if repo_model.IsErrAttachmentNotExist(err) { + if models.IsErrReleaseNotExist(err) { ctx.NotFound() return } From fa8bd016a573fc28e5da6bbec072b21234f7c60c Mon Sep 17 00:00:00 2001 From: zeripath Date: Mon, 22 Aug 2022 14:41:32 +0100 Subject: [PATCH 3/4] Update routers/api/v1/repo/release_attachment.go --- routers/api/v1/repo/release_attachment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/repo/release_attachment.go b/routers/api/v1/repo/release_attachment.go index 21950354ab5da..2bd4941693591 100644 --- a/routers/api/v1/repo/release_attachment.go +++ b/routers/api/v1/repo/release_attachment.go @@ -174,7 +174,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) { releaseID := ctx.ParamsInt64(":id") release, err := models.GetReleaseByID(ctx, releaseID) if err != nil { - if repo_model.IsErrAttachmentNotExist(err) { + if models.IsErrReleaseNotExist(err) { ctx.NotFound() return } From f864cd4491910d10e34ad10fc04cc6b12b66e321 Mon Sep 17 00:00:00 2001 From: zeripath Date: Mon, 22 Aug 2022 14:41:44 +0100 Subject: [PATCH 4/4] Update routers/api/v1/repo/release_attachment.go --- routers/api/v1/repo/release_attachment.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/api/v1/repo/release_attachment.go b/routers/api/v1/repo/release_attachment.go index 2bd4941693591..7b63af34c8687 100644 --- a/routers/api/v1/repo/release_attachment.go +++ b/routers/api/v1/repo/release_attachment.go @@ -174,7 +174,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) { releaseID := ctx.ParamsInt64(":id") release, err := models.GetReleaseByID(ctx, releaseID) if err != nil { - if models.IsErrReleaseNotExist(err) { + if models.IsErrReleaseNotExist(err) { ctx.NotFound() return }