Skip to content

Commit 5f200ee

Browse files
committed
user modern golang error system
1 parent e19a4b1 commit 5f200ee

File tree

6 files changed

+14
-12
lines changed

6 files changed

+14
-12
lines changed

modules/util/error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var (
1616
ErrPermissionDenied = errors.New("permission denied") // also implies HTTP 403
1717
ErrNotExist = errors.New("resource does not exist") // also implies HTTP 404
1818
ErrAlreadyExist = errors.New("resource already exists") // also implies HTTP 409
19+
ErrContentTooLarge = errors.New("content exceeds limit") // also implies HTTP 413
1920

2021
// ErrUnprocessableContent implies HTTP 422, the syntax of the request content is correct,
2122
// but the server is unable to process the contained instructions

routers/api/v1/repo/issue_attachment.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
package repo
55

66
import (
7+
"errors"
78
"net/http"
89

910
issues_model "code.gitea.io/gitea/models/issues"
1011
repo_model "code.gitea.io/gitea/models/repo"
1112
"code.gitea.io/gitea/modules/log"
1213
"code.gitea.io/gitea/modules/setting"
1314
api "code.gitea.io/gitea/modules/structs"
15+
"code.gitea.io/gitea/modules/util"
1416
"code.gitea.io/gitea/modules/web"
1517
attachment_service "code.gitea.io/gitea/services/attachment"
1618
"code.gitea.io/gitea/services/context"
@@ -192,7 +194,7 @@ func CreateIssueAttachment(ctx *context.APIContext) {
192194
if err != nil {
193195
if upload.IsErrFileTypeForbidden(err) {
194196
ctx.APIError(http.StatusUnprocessableEntity, err)
195-
} else if attachment_service.IsErrAttachmentSizeExceed(err) {
197+
} else if errors.Is(err, util.ErrContentTooLarge) {
196198
ctx.APIError(http.StatusRequestEntityTooLarge, err)
197199
} else {
198200
ctx.APIErrorInternal(err)

routers/api/v1/repo/issue_comment_attachment.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"code.gitea.io/gitea/modules/log"
1414
"code.gitea.io/gitea/modules/setting"
1515
api "code.gitea.io/gitea/modules/structs"
16+
"code.gitea.io/gitea/modules/util"
1617
"code.gitea.io/gitea/modules/web"
1718
attachment_service "code.gitea.io/gitea/services/attachment"
1819
"code.gitea.io/gitea/services/context"
@@ -201,7 +202,7 @@ func CreateIssueCommentAttachment(ctx *context.APIContext) {
201202
if err != nil {
202203
if upload.IsErrFileTypeForbidden(err) {
203204
ctx.APIError(http.StatusUnprocessableEntity, err)
204-
} else if attachment_service.IsErrAttachmentSizeExceed(err) {
205+
} else if errors.Is(err, util.ErrContentTooLarge) {
205206
ctx.APIError(http.StatusRequestEntityTooLarge, err)
206207
} else {
207208
ctx.APIErrorInternal(err)

routers/api/v1/repo/release_attachment.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package repo
55

66
import (
7+
"errors"
78
"io"
89
"net/http"
910
"strings"
@@ -12,6 +13,7 @@ import (
1213
"code.gitea.io/gitea/modules/log"
1314
"code.gitea.io/gitea/modules/setting"
1415
api "code.gitea.io/gitea/modules/structs"
16+
"code.gitea.io/gitea/modules/util"
1517
"code.gitea.io/gitea/modules/web"
1618
attachment_service "code.gitea.io/gitea/services/attachment"
1719
"code.gitea.io/gitea/services/context"
@@ -248,7 +250,7 @@ func CreateReleaseAttachment(ctx *context.APIContext) {
248250
return
249251
}
250252

251-
if attachment_service.IsErrAttachmentSizeExceed(err) {
253+
if errors.Is(err, util.ErrContentTooLarge) {
252254
ctx.APIError(http.StatusRequestEntityTooLarge, err)
253255
return
254256
}

services/attachment/attachment.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,11 @@ type ErrAttachmentSizeExceed struct {
4444
}
4545

4646
func (e ErrAttachmentSizeExceed) Error() string {
47-
return fmt.Sprintf("attachment size %d exceed limit %d", e.Size, e.MaxSize)
47+
return fmt.Sprintf("attachment size %d exceeds limit %d", e.Size, e.MaxSize)
4848
}
4949

50-
func IsErrAttachmentSizeExceed(err error) bool {
51-
_, ok := err.(ErrAttachmentSizeExceed)
52-
return ok
53-
}
54-
55-
func (err ErrAttachmentSizeExceed) Unwrap() error {
56-
return util.ErrInvalidArgument
50+
func (e ErrAttachmentSizeExceed) Unwrap() error {
51+
return util.ErrContentTooLarge
5752
}
5853

5954
// UploadAttachment upload new attachment into storage and update database

services/mailer/incoming/incoming_handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package incoming
66
import (
77
"bytes"
88
"context"
9+
"errors"
910
"fmt"
1011

1112
issues_model "code.gitea.io/gitea/models/issues"
@@ -95,7 +96,7 @@ func (h *ReplyHandler) Handle(ctx context.Context, content *MailContent, doer *u
9596
log.Info("Skipping disallowed attachment type: %s", attachment.Name)
9697
continue
9798
}
98-
if attachment_service.IsErrAttachmentSizeExceed(err) {
99+
if errors.Is(err, util.ErrContentTooLarge) {
99100
log.Info("Skipping attachment exceeding size limit: %s", attachment.Name)
100101
continue
101102
}

0 commit comments

Comments
 (0)