diff options
author | zeripath <art27@cantab.net> | 2022-10-18 06:50:37 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-18 07:50:37 +0200 |
commit | 716fcfcf72be6af854d800f3e2c885b9806577e6 (patch) | |
tree | b89ef04d79b58472c9247a5ec426f1118c74f360 /models/error.go | |
parent | 6af1a0c8c004aeb4ef1df1a0c74dcb1e1f874796 (diff) | |
download | gitea-716fcfcf72be6af854d800f3e2c885b9806577e6.tar.gz gitea-716fcfcf72be6af854d800f3e2c885b9806577e6.zip |
Make every not exist error unwrappable to a fs.ErrNotExist (#20891)
A lot of our code is repeatedly testing if individual errors are
specific types of Not Exist errors. This is repetitative and unnecesary.
`Unwrap() error` provides a common way of labelling an error as a
NotExist error and we can/should use this.
This PR has chosen to use the common `io/fs` errors e.g.
`fs.ErrNotExist` for our errors. This is in some ways not completely
correct as these are not filesystem errors but it seems like a
reasonable thing to do and would allow us to simplify a lot of our code
to `errors.Is(err, fs.ErrNotExist)` instead of
`package.IsErr...NotExist(err)`
I am open to suggestions to use a different base error - perhaps
`models/db.ErrNotExist` if that would be felt to be better.
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: delvh <dev.lh@web.de>
Diffstat (limited to 'models/error.go')
-rw-r--r-- | models/error.go | 85 |
1 files changed, 83 insertions, 2 deletions
diff --git a/models/error.go b/models/error.go index 873ed0ceaa..f4c4bc8f67 100644 --- a/models/error.go +++ b/models/error.go @@ -10,6 +10,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/util" ) // ErrUserOwnRepos represents a "UserOwnRepos" kind of error. @@ -63,8 +64,8 @@ type ErrNoPendingRepoTransfer struct { RepoID int64 } -func (e ErrNoPendingRepoTransfer) Error() string { - return fmt.Sprintf("repository doesn't have a pending transfer [repo_id: %d]", e.RepoID) +func (err ErrNoPendingRepoTransfer) Error() string { + return fmt.Sprintf("repository doesn't have a pending transfer [repo_id: %d]", err.RepoID) } // IsErrNoPendingTransfer is an error type when a repository has no pending @@ -74,6 +75,10 @@ func IsErrNoPendingTransfer(err error) bool { return ok } +func (err ErrNoPendingRepoTransfer) Unwrap() error { + return util.ErrNotExist +} + // ErrRepoTransferInProgress represents the state of a repository that has an // ongoing transfer type ErrRepoTransferInProgress struct { @@ -91,6 +96,10 @@ func (err ErrRepoTransferInProgress) Error() string { return fmt.Sprintf("repository is already being transferred [uname: %s, name: %s]", err.Uname, err.Name) } +func (err ErrRepoTransferInProgress) Unwrap() error { + return util.ErrAlreadyExist +} + // ErrInvalidCloneAddr represents a "InvalidCloneAddr" kind of error. type ErrInvalidCloneAddr struct { Host string @@ -124,6 +133,10 @@ func (err *ErrInvalidCloneAddr) Error() string { return fmt.Sprintf("migration/cloning from '%s' is not allowed", err.Host) } +func (err *ErrInvalidCloneAddr) Unwrap() error { + return util.ErrInvalidArgument +} + // ErrUpdateTaskNotExist represents a "UpdateTaskNotExist" kind of error. type ErrUpdateTaskNotExist struct { UUID string @@ -139,6 +152,10 @@ func (err ErrUpdateTaskNotExist) Error() string { return fmt.Sprintf("update task does not exist [uuid: %s]", err.UUID) } +func (err ErrUpdateTaskNotExist) Unwrap() error { + return util.ErrNotExist +} + // ErrInvalidTagName represents a "InvalidTagName" kind of error. type ErrInvalidTagName struct { TagName string @@ -154,6 +171,10 @@ func (err ErrInvalidTagName) Error() string { return fmt.Sprintf("release tag name is not valid [tag_name: %s]", err.TagName) } +func (err ErrInvalidTagName) Unwrap() error { + return util.ErrInvalidArgument +} + // ErrProtectedTagName represents a "ProtectedTagName" kind of error. type ErrProtectedTagName struct { TagName string @@ -169,6 +190,10 @@ func (err ErrProtectedTagName) Error() string { return fmt.Sprintf("release tag name is protected [tag_name: %s]", err.TagName) } +func (err ErrProtectedTagName) Unwrap() error { + return util.ErrPermissionDenied +} + // ErrRepoFileAlreadyExists represents a "RepoFileAlreadyExist" kind of error. type ErrRepoFileAlreadyExists struct { Path string @@ -184,6 +209,10 @@ func (err ErrRepoFileAlreadyExists) Error() string { return fmt.Sprintf("repository file already exists [path: %s]", err.Path) } +func (err ErrRepoFileAlreadyExists) Unwrap() error { + return util.ErrAlreadyExist +} + // ErrRepoFileDoesNotExist represents a "RepoFileDoesNotExist" kind of error. type ErrRepoFileDoesNotExist struct { Path string @@ -200,6 +229,10 @@ func (err ErrRepoFileDoesNotExist) Error() string { return fmt.Sprintf("repository file does not exist [path: %s]", err.Path) } +func (err ErrRepoFileDoesNotExist) Unwrap() error { + return util.ErrNotExist +} + // ErrFilenameInvalid represents a "FilenameInvalid" kind of error. type ErrFilenameInvalid struct { Path string @@ -215,6 +248,10 @@ func (err ErrFilenameInvalid) Error() string { return fmt.Sprintf("path contains a malformed path component [path: %s]", err.Path) } +func (err ErrFilenameInvalid) Unwrap() error { + return util.ErrInvalidArgument +} + // ErrUserCannotCommit represents "UserCannotCommit" kind of error. type ErrUserCannotCommit struct { UserName string @@ -230,6 +267,10 @@ func (err ErrUserCannotCommit) Error() string { return fmt.Sprintf("user cannot commit to repo [user: %s]", err.UserName) } +func (err ErrUserCannotCommit) Unwrap() error { + return util.ErrPermissionDenied +} + // ErrFilePathInvalid represents a "FilePathInvalid" kind of error. type ErrFilePathInvalid struct { Message string @@ -251,6 +292,10 @@ func (err ErrFilePathInvalid) Error() string { return fmt.Sprintf("path is invalid [path: %s]", err.Path) } +func (err ErrFilePathInvalid) Unwrap() error { + return util.ErrInvalidArgument +} + // ErrFilePathProtected represents a "FilePathProtected" kind of error. type ErrFilePathProtected struct { Message string @@ -270,6 +315,10 @@ func (err ErrFilePathProtected) Error() string { return fmt.Sprintf("path is protected and can not be changed [path: %s]", err.Path) } +func (err ErrFilePathProtected) Unwrap() error { + return util.ErrPermissionDenied +} + // __________ .__ // \______ \____________ ____ ____ | |__ // | | _/\_ __ \__ \ / \_/ ___\| | \ @@ -292,6 +341,10 @@ func (err ErrBranchDoesNotExist) Error() string { return fmt.Sprintf("branch does not exist [name: %s]", err.BranchName) } +func (err ErrBranchDoesNotExist) Unwrap() error { + return util.ErrNotExist +} + // ErrBranchAlreadyExists represents an error that branch with such name already exists. type ErrBranchAlreadyExists struct { BranchName string @@ -307,6 +360,10 @@ func (err ErrBranchAlreadyExists) Error() string { return fmt.Sprintf("branch already exists [name: %s]", err.BranchName) } +func (err ErrBranchAlreadyExists) Unwrap() error { + return util.ErrAlreadyExist +} + // ErrBranchNameConflict represents an error that branch name conflicts with other branch. type ErrBranchNameConflict struct { BranchName string @@ -322,6 +379,10 @@ func (err ErrBranchNameConflict) Error() string { return fmt.Sprintf("branch conflicts with existing branch [name: %s]", err.BranchName) } +func (err ErrBranchNameConflict) Unwrap() error { + return util.ErrAlreadyExist +} + // ErrBranchesEqual represents an error that branch name conflicts with other branch. type ErrBranchesEqual struct { BaseBranchName string @@ -338,6 +399,10 @@ func (err ErrBranchesEqual) Error() string { return fmt.Sprintf("branches are equal [head: %sm base: %s]", err.HeadBranchName, err.BaseBranchName) } +func (err ErrBranchesEqual) Unwrap() error { + return util.ErrInvalidArgument +} + // ErrDisallowedToMerge represents an error that a branch is protected and the current user is not allowed to modify it. type ErrDisallowedToMerge struct { Reason string @@ -353,6 +418,10 @@ func (err ErrDisallowedToMerge) Error() string { return fmt.Sprintf("not allowed to merge [reason: %s]", err.Reason) } +func (err ErrDisallowedToMerge) Unwrap() error { + return util.ErrPermissionDenied +} + // ErrTagAlreadyExists represents an error that tag with such name already exists. type ErrTagAlreadyExists struct { TagName string @@ -368,6 +437,10 @@ func (err ErrTagAlreadyExists) Error() string { return fmt.Sprintf("tag already exists [name: %s]", err.TagName) } +func (err ErrTagAlreadyExists) Unwrap() error { + return util.ErrAlreadyExist +} + // ErrSHADoesNotMatch represents a "SHADoesNotMatch" kind of error. type ErrSHADoesNotMatch struct { Path string @@ -400,6 +473,10 @@ func (err ErrSHANotFound) Error() string { return fmt.Sprintf("sha not found [%s]", err.SHA) } +func (err ErrSHANotFound) Unwrap() error { + return util.ErrNotExist +} + // ErrCommitIDDoesNotMatch represents a "CommitIDDoesNotMatch" kind of error. type ErrCommitIDDoesNotMatch struct { GivenCommitID string @@ -446,6 +523,10 @@ func (err ErrInvalidMergeStyle) Error() string { err.ID, err.Style) } +func (err ErrInvalidMergeStyle) Unwrap() error { + return util.ErrInvalidArgument +} + // ErrMergeConflicts represents an error if merging fails with a conflict type ErrMergeConflicts struct { Style repo_model.MergeStyle |