Skip to content

Commit 216243e

Browse files
authored
Refactor error system (#33771)
It should not expose `util.SilentWrap` or construct it manually.
1 parent dbed39d commit 216243e

File tree

12 files changed

+41
-58
lines changed

12 files changed

+41
-58
lines changed

models/asymkey/error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func (err ErrKeyUnableVerify) Error() string {
2525
}
2626

2727
// ErrKeyIsPrivate is returned when the provided key is a private key not a public key
28-
var ErrKeyIsPrivate = util.NewSilentWrapErrorf(util.ErrInvalidArgument, "the provided key is a private key")
28+
var ErrKeyIsPrivate = util.ErrorWrap(util.ErrInvalidArgument, "the provided key is a private key")
2929

3030
// ErrKeyNotExist represents a "KeyNotExist" kind of error.
3131
type ErrKeyNotExist struct {

models/db/name.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (err ErrNameCharsNotAllowed) Unwrap() error {
7777
func IsUsableName(reservedNames, reservedPatterns []string, name string) error {
7878
name = strings.TrimSpace(strings.ToLower(name))
7979
if utf8.RuneCountInString(name) == 0 {
80-
return util.SilentWrap{Message: "name is empty", Err: util.ErrInvalidArgument}
80+
return util.NewInvalidArgumentErrorf("name is empty")
8181
}
8282

8383
for i := range reservedNames {

models/repo/archiver.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ func (archiver *RepoArchiver) RelativePath() string {
5050
func repoArchiverForRelativePath(relativePath string) (*RepoArchiver, error) {
5151
parts := strings.SplitN(relativePath, "/", 3)
5252
if len(parts) != 3 {
53-
return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
53+
return nil, util.NewInvalidArgumentErrorf("invalid storage path: must have 3 parts")
5454
}
5555
repoID, err := strconv.ParseInt(parts[0], 10, 64)
5656
if err != nil {
57-
return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
57+
return nil, util.NewInvalidArgumentErrorf("invalid storage path: invalid repo id")
5858
}
5959
commitID, archiveType := git.SplitArchiveNameType(parts[2])
6060
if archiveType == git.ArchiveUnknown {
61-
return nil, util.SilentWrap{Message: fmt.Sprintf("invalid storage path: %s", relativePath), Err: util.ErrInvalidArgument}
61+
return nil, util.NewInvalidArgumentErrorf("invalid storage path: invalid archive type")
6262
}
6363
return &RepoArchiver{RepoID: repoID, CommitID: commitID, Type: archiveType}, nil
6464
}

models/user/must_change_password.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func SetMustChangePassword(ctx context.Context, all, mustChangePassword bool, in
3434
if !all {
3535
include = sliceTrimSpaceDropEmpty(include)
3636
if len(include) == 0 {
37-
return 0, util.NewSilentWrapErrorf(util.ErrInvalidArgument, "no users to include provided")
37+
return 0, util.ErrorWrap(util.ErrInvalidArgument, "no users to include provided")
3838
}
3939

4040
cond = cond.And(builder.In("lower_name", include))

modules/packages/conda/metadata.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ import (
1717
)
1818

1919
var (
20-
ErrInvalidStructure = util.SilentWrap{Message: "package structure is invalid", Err: util.ErrInvalidArgument}
21-
ErrInvalidName = util.SilentWrap{Message: "package name is invalid", Err: util.ErrInvalidArgument}
22-
ErrInvalidVersion = util.SilentWrap{Message: "package version is invalid", Err: util.ErrInvalidArgument}
20+
ErrInvalidStructure = util.NewInvalidArgumentErrorf("package structure is invalid")
21+
ErrInvalidName = util.NewInvalidArgumentErrorf("package name is invalid")
22+
ErrInvalidVersion = util.NewInvalidArgumentErrorf("package version is invalid")
2323
)
2424

2525
const (

modules/translation/i18n/errors.go

Lines changed: 0 additions & 13 deletions
This file was deleted.

modules/translation/i18n/format.go

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

66
import (
7+
"errors"
78
"fmt"
89
"reflect"
910
)
@@ -30,7 +31,7 @@ func Format(format string, args ...any) (msg string, err error) {
3031
fmtArgs = append(fmtArgs, val.Index(i).Interface())
3132
}
3233
} else {
33-
err = ErrUncertainArguments
34+
err = errors.New("arguments to i18n should not contain uncertain slices")
3435
break
3536
}
3637
} else {

modules/translation/i18n/localestore.go

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

66
import (
7+
"errors"
78
"fmt"
89
"html/template"
910
"slices"
@@ -41,7 +42,7 @@ func NewLocaleStore() LocaleStore {
4142
// AddLocaleByIni adds locale by ini into the store
4243
func (store *localeStore) AddLocaleByIni(langName, langDesc string, source, moreSource []byte) error {
4344
if _, ok := store.localeMap[langName]; ok {
44-
return ErrLocaleAlreadyExist
45+
return errors.New("lang has already been added")
4546
}
4647

4748
store.langNames = append(store.langNames, langName)

modules/util/error.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,74 +22,74 @@ var (
2222
ErrUnprocessableContent = errors.New("unprocessable content")
2323
)
2424

25-
// SilentWrap provides a simple wrapper for a wrapped error where the wrapped error message plays no part in the error message
25+
// errorWrapper provides a simple wrapper for a wrapped error where the wrapped error message plays no part in the error message
2626
// Especially useful for "untyped" errors created with "errors.New(…)" that can be classified as 'invalid argument', 'permission denied', 'exists already', or 'does not exist'
27-
type SilentWrap struct {
27+
type errorWrapper struct {
2828
Message string
2929
Err error
3030
}
3131

3232
// Error returns the message
33-
func (w SilentWrap) Error() string {
33+
func (w errorWrapper) Error() string {
3434
return w.Message
3535
}
3636

3737
// Unwrap returns the underlying error
38-
func (w SilentWrap) Unwrap() error {
38+
func (w errorWrapper) Unwrap() error {
3939
return w.Err
4040
}
4141

42-
type LocaleWrap struct {
42+
type LocaleWrapper struct {
4343
err error
4444
TrKey string
4545
TrArgs []any
4646
}
4747

4848
// Error returns the message
49-
func (w LocaleWrap) Error() string {
49+
func (w LocaleWrapper) Error() string {
5050
return w.err.Error()
5151
}
5252

5353
// Unwrap returns the underlying error
54-
func (w LocaleWrap) Unwrap() error {
54+
func (w LocaleWrapper) Unwrap() error {
5555
return w.err
5656
}
5757

58-
// NewSilentWrapErrorf returns an error that formats as the given text but unwraps as the provided error
59-
func NewSilentWrapErrorf(unwrap error, message string, args ...any) error {
58+
// ErrorWrap returns an error that formats as the given text but unwraps as the provided error
59+
func ErrorWrap(unwrap error, message string, args ...any) error {
6060
if len(args) == 0 {
61-
return SilentWrap{Message: message, Err: unwrap}
61+
return errorWrapper{Message: message, Err: unwrap}
6262
}
63-
return SilentWrap{Message: fmt.Sprintf(message, args...), Err: unwrap}
63+
return errorWrapper{Message: fmt.Sprintf(message, args...), Err: unwrap}
6464
}
6565

6666
// NewInvalidArgumentErrorf returns an error that formats as the given text but unwraps as an ErrInvalidArgument
6767
func NewInvalidArgumentErrorf(message string, args ...any) error {
68-
return NewSilentWrapErrorf(ErrInvalidArgument, message, args...)
68+
return ErrorWrap(ErrInvalidArgument, message, args...)
6969
}
7070

7171
// NewPermissionDeniedErrorf returns an error that formats as the given text but unwraps as an ErrPermissionDenied
7272
func NewPermissionDeniedErrorf(message string, args ...any) error {
73-
return NewSilentWrapErrorf(ErrPermissionDenied, message, args...)
73+
return ErrorWrap(ErrPermissionDenied, message, args...)
7474
}
7575

7676
// NewAlreadyExistErrorf returns an error that formats as the given text but unwraps as an ErrAlreadyExist
7777
func NewAlreadyExistErrorf(message string, args ...any) error {
78-
return NewSilentWrapErrorf(ErrAlreadyExist, message, args...)
78+
return ErrorWrap(ErrAlreadyExist, message, args...)
7979
}
8080

8181
// NewNotExistErrorf returns an error that formats as the given text but unwraps as an ErrNotExist
8282
func NewNotExistErrorf(message string, args ...any) error {
83-
return NewSilentWrapErrorf(ErrNotExist, message, args...)
83+
return ErrorWrap(ErrNotExist, message, args...)
8484
}
8585

86-
// ErrWrapLocale wraps an err with a translation key and arguments
87-
func ErrWrapLocale(err error, trKey string, trArgs ...any) error {
88-
return LocaleWrap{err: err, TrKey: trKey, TrArgs: trArgs}
86+
// ErrorWrapLocale wraps an err with a translation key and arguments
87+
func ErrorWrapLocale(err error, trKey string, trArgs ...any) error {
88+
return LocaleWrapper{err: err, TrKey: trKey, TrArgs: trArgs}
8989
}
9090

91-
func ErrAsLocale(err error) *LocaleWrap {
92-
var e LocaleWrap
91+
func ErrorAsLocale(err error) *LocaleWrapper {
92+
var e LocaleWrapper
9393
if errors.As(err, &e) {
9494
return &e
9595
}

routers/web/repo/actions/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ func Run(ctx *context_module.Context) {
789789
return nil
790790
})
791791
if err != nil {
792-
if errLocale := util.ErrAsLocale(err); errLocale != nil {
792+
if errLocale := util.ErrorAsLocale(err); errLocale != nil {
793793
ctx.Flash.Error(ctx.Tr(errLocale.TrKey, errLocale.TrArgs...))
794794
ctx.Redirect(redirectURL)
795795
} else {

services/actions/workflow.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,14 @@ func GetActionWorkflow(ctx *context.APIContext, workflowID string) (*api.ActionW
141141

142142
func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, repo *repo_model.Repository, gitRepo *git.Repository, workflowID, ref string, processInputs func(model *model.WorkflowDispatch, inputs map[string]any) error) error {
143143
if workflowID == "" {
144-
return util.ErrWrapLocale(
144+
return util.ErrorWrapLocale(
145145
util.NewNotExistErrorf("workflowID is empty"),
146146
"actions.workflow.not_found", workflowID,
147147
)
148148
}
149149

150150
if ref == "" {
151-
return util.ErrWrapLocale(
151+
return util.ErrorWrapLocale(
152152
util.NewNotExistErrorf("ref is empty"),
153153
"form.target_ref_not_exist", ref,
154154
)
@@ -158,7 +158,7 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
158158
cfgUnit := repo.MustGetUnit(ctx, unit.TypeActions)
159159
cfg := cfgUnit.ActionsConfig()
160160
if cfg.IsWorkflowDisabled(workflowID) {
161-
return util.ErrWrapLocale(
161+
return util.ErrorWrapLocale(
162162
util.NewPermissionDeniedErrorf("workflow is disabled"),
163163
"actions.workflow.disabled",
164164
)
@@ -177,7 +177,7 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
177177
runTargetCommit, err = gitRepo.GetBranchCommit(ref)
178178
}
179179
if err != nil {
180-
return util.ErrWrapLocale(
180+
return util.ErrorWrapLocale(
181181
util.NewNotExistErrorf("ref %q doesn't exist", ref),
182182
"form.target_ref_not_exist", ref,
183183
)
@@ -208,7 +208,7 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
208208
}
209209

210210
if len(workflows) == 0 {
211-
return util.ErrWrapLocale(
211+
return util.ErrorWrapLocale(
212212
util.NewNotExistErrorf("workflow %q doesn't exist", workflowID),
213213
"actions.workflow.not_found", workflowID,
214214
)

services/release/release.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -296,10 +296,7 @@ func UpdateRelease(ctx context.Context, doer *user_model.User, gitRepo *git.Repo
296296
}
297297
for _, attach := range attachments {
298298
if attach.ReleaseID != rel.ID {
299-
return util.SilentWrap{
300-
Message: "delete attachment of release permission denied",
301-
Err: util.ErrPermissionDenied,
302-
}
299+
return util.NewPermissionDeniedErrorf("delete attachment of release permission denied")
303300
}
304301
deletedUUIDs.Add(attach.UUID)
305302
}
@@ -321,10 +318,7 @@ func UpdateRelease(ctx context.Context, doer *user_model.User, gitRepo *git.Repo
321318
}
322319
for _, attach := range attachments {
323320
if attach.ReleaseID != rel.ID {
324-
return util.SilentWrap{
325-
Message: "update attachment of release permission denied",
326-
Err: util.ErrPermissionDenied,
327-
}
321+
return util.NewPermissionDeniedErrorf("update attachment of release permission denied")
328322
}
329323
}
330324

0 commit comments

Comments
 (0)