user modern golang error system

This commit is contained in:
wxiaoguang 2025-09-24 23:21:57 +08:00
parent e19a4b17a5
commit 5f200eea5b
6 changed files with 14 additions and 12 deletions

View File

@ -16,6 +16,7 @@ var (
ErrPermissionDenied = errors.New("permission denied") // also implies HTTP 403
ErrNotExist = errors.New("resource does not exist") // also implies HTTP 404
ErrAlreadyExist = errors.New("resource already exists") // also implies HTTP 409
ErrContentTooLarge = errors.New("content exceeds limit") // also implies HTTP 413
// ErrUnprocessableContent implies HTTP 422, the syntax of the request content is correct,
// but the server is unable to process the contained instructions

View File

@ -4,6 +4,7 @@
package repo
import (
"errors"
"net/http"
issues_model "code.gitea.io/gitea/models/issues"
@ -11,6 +12,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
attachment_service "code.gitea.io/gitea/services/attachment"
"code.gitea.io/gitea/services/context"
@ -192,7 +194,7 @@ func CreateIssueAttachment(ctx *context.APIContext) {
if err != nil {
if upload.IsErrFileTypeForbidden(err) {
ctx.APIError(http.StatusUnprocessableEntity, err)
} else if attachment_service.IsErrAttachmentSizeExceed(err) {
} else if errors.Is(err, util.ErrContentTooLarge) {
ctx.APIError(http.StatusRequestEntityTooLarge, err)
} else {
ctx.APIErrorInternal(err)

View File

@ -13,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
attachment_service "code.gitea.io/gitea/services/attachment"
"code.gitea.io/gitea/services/context"
@ -201,7 +202,7 @@ func CreateIssueCommentAttachment(ctx *context.APIContext) {
if err != nil {
if upload.IsErrFileTypeForbidden(err) {
ctx.APIError(http.StatusUnprocessableEntity, err)
} else if attachment_service.IsErrAttachmentSizeExceed(err) {
} else if errors.Is(err, util.ErrContentTooLarge) {
ctx.APIError(http.StatusRequestEntityTooLarge, err)
} else {
ctx.APIErrorInternal(err)

View File

@ -4,6 +4,7 @@
package repo
import (
"errors"
"io"
"net/http"
"strings"
@ -12,6 +13,7 @@ import (
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/util"
"code.gitea.io/gitea/modules/web"
attachment_service "code.gitea.io/gitea/services/attachment"
"code.gitea.io/gitea/services/context"
@ -248,7 +250,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
return
}
if attachment_service.IsErrAttachmentSizeExceed(err) {
if errors.Is(err, util.ErrContentTooLarge) {
ctx.APIError(http.StatusRequestEntityTooLarge, err)
return
}

View File

@ -44,16 +44,11 @@ type ErrAttachmentSizeExceed struct {
}
func (e ErrAttachmentSizeExceed) Error() string {
return fmt.Sprintf("attachment size %d exceed limit %d", e.Size, e.MaxSize)
return fmt.Sprintf("attachment size %d exceeds limit %d", e.Size, e.MaxSize)
}
func IsErrAttachmentSizeExceed(err error) bool {
_, ok := err.(ErrAttachmentSizeExceed)
return ok
}
func (err ErrAttachmentSizeExceed) Unwrap() error {
return util.ErrInvalidArgument
func (e ErrAttachmentSizeExceed) Unwrap() error {
return util.ErrContentTooLarge
}
// UploadAttachment upload new attachment into storage and update database

View File

@ -6,6 +6,7 @@ package incoming
import (
"bytes"
"context"
"errors"
"fmt"
issues_model "code.gitea.io/gitea/models/issues"
@ -95,7 +96,7 @@ func (h *ReplyHandler) Handle(ctx context.Context, content *MailContent, doer *u
log.Info("Skipping disallowed attachment type: %s", attachment.Name)
continue
}
if attachment_service.IsErrAttachmentSizeExceed(err) {
if errors.Is(err, util.ErrContentTooLarge) {
log.Info("Skipping attachment exceeding size limit: %s", attachment.Name)
continue
}