1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-22 10:18:38 +00:00

Refactor error system (#33610)

This commit is contained in:
wxiaoguang
2025-02-17 14:13:17 +08:00
committed by GitHub
parent 69de5a65c2
commit f35850f48e
184 changed files with 2100 additions and 2106 deletions

View File

@@ -23,14 +23,14 @@ func checkReleaseMatchRepo(ctx *context.APIContext, releaseID int64) bool {
release, err := repo_model.GetReleaseByID(ctx, releaseID)
if err != nil {
if repo_model.IsErrReleaseNotExist(err) {
ctx.NotFound()
ctx.APIErrorNotFound()
return false
}
ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err)
ctx.APIError(http.StatusInternalServerError, err)
return false
}
if release.RepoID != ctx.Repo.Repository.ID {
ctx.NotFound()
ctx.APIErrorNotFound()
return false
}
return true
@@ -81,15 +81,15 @@ func GetReleaseAttachment(ctx *context.APIContext) {
attach, err := repo_model.GetAttachmentByID(ctx, attachID)
if err != nil {
if repo_model.IsErrAttachmentNotExist(err) {
ctx.NotFound()
ctx.APIErrorNotFound()
return
}
ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err)
ctx.APIError(http.StatusInternalServerError, err)
return
}
if attach.ReleaseID != releaseID {
log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
ctx.NotFound()
ctx.APIErrorNotFound()
return
}
// FIXME Should prove the existence of the given repo, but results in unnecessary database requests
@@ -130,18 +130,18 @@ func ListReleaseAttachments(ctx *context.APIContext) {
release, err := repo_model.GetReleaseByID(ctx, releaseID)
if err != nil {
if repo_model.IsErrReleaseNotExist(err) {
ctx.NotFound()
ctx.APIErrorNotFound()
return
}
ctx.Error(http.StatusInternalServerError, "GetReleaseByID", err)
ctx.APIError(http.StatusInternalServerError, err)
return
}
if release.RepoID != ctx.Repo.Repository.ID {
ctx.NotFound()
ctx.APIErrorNotFound()
return
}
if err := release.LoadAttributes(ctx); err != nil {
ctx.Error(http.StatusInternalServerError, "LoadAttributes", err)
ctx.APIError(http.StatusInternalServerError, err)
return
}
ctx.JSON(http.StatusOK, convert.ToAPIRelease(ctx, ctx.Repo.Repository, release).Attachments)
@@ -194,7 +194,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
// Check if attachments are enabled
if !setting.Attachment.Enabled {
ctx.NotFound("Attachment is not enabled")
ctx.APIErrorNotFound("Attachment is not enabled")
return
}
@@ -212,7 +212,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
if strings.HasPrefix(strings.ToLower(ctx.Req.Header.Get("Content-Type")), "multipart/form-data") {
file, header, err := ctx.Req.FormFile("attachment")
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetFile", err)
ctx.APIError(http.StatusInternalServerError, err)
return
}
defer file.Close()
@@ -229,7 +229,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
}
if filename == "" {
ctx.Error(http.StatusBadRequest, "CreateReleaseAttachment", "Could not determine name of attachment.")
ctx.APIError(http.StatusBadRequest, "Could not determine name of attachment.")
return
}
@@ -242,10 +242,10 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
})
if err != nil {
if upload.IsErrFileTypeForbidden(err) {
ctx.Error(http.StatusBadRequest, "DetectContentType", err)
ctx.APIError(http.StatusBadRequest, err)
return
}
ctx.Error(http.StatusInternalServerError, "NewAttachment", err)
ctx.APIError(http.StatusInternalServerError, err)
return
}
@@ -308,15 +308,15 @@ func EditReleaseAttachment(ctx *context.APIContext) {
attach, err := repo_model.GetAttachmentByID(ctx, attachID)
if err != nil {
if repo_model.IsErrAttachmentNotExist(err) {
ctx.NotFound()
ctx.APIErrorNotFound()
return
}
ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err)
ctx.APIError(http.StatusInternalServerError, err)
return
}
if attach.ReleaseID != releaseID {
log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
ctx.NotFound()
ctx.APIErrorNotFound()
return
}
// FIXME Should prove the existence of the given repo, but results in unnecessary database requests
@@ -326,10 +326,10 @@ func EditReleaseAttachment(ctx *context.APIContext) {
if err := attachment_service.UpdateAttachment(ctx, setting.Repository.Release.AllowedTypes, attach); err != nil {
if upload.IsErrFileTypeForbidden(err) {
ctx.Error(http.StatusUnprocessableEntity, "", err)
ctx.APIError(http.StatusUnprocessableEntity, err)
return
}
ctx.Error(http.StatusInternalServerError, "UpdateAttachment", attach)
ctx.APIError(http.StatusInternalServerError, attach)
return
}
ctx.JSON(http.StatusCreated, convert.ToAPIAttachment(ctx.Repo.Repository, attach))
@@ -381,21 +381,21 @@ func DeleteReleaseAttachment(ctx *context.APIContext) {
attach, err := repo_model.GetAttachmentByID(ctx, attachID)
if err != nil {
if repo_model.IsErrAttachmentNotExist(err) {
ctx.NotFound()
ctx.APIErrorNotFound()
return
}
ctx.Error(http.StatusInternalServerError, "GetAttachmentByID", err)
ctx.APIError(http.StatusInternalServerError, err)
return
}
if attach.ReleaseID != releaseID {
log.Info("User requested attachment is not in release, release_id %v, attachment_id: %v", releaseID, attachID)
ctx.NotFound()
ctx.APIErrorNotFound()
return
}
// FIXME Should prove the existence of the given repo, but results in unnecessary database requests
if err := repo_model.DeleteAttachment(ctx, attach, true); err != nil {
ctx.Error(http.StatusInternalServerError, "DeleteAttachment", err)
ctx.APIError(http.StatusInternalServerError, err)
return
}
ctx.Status(http.StatusNoContent)