From a86d0704120429016e02aa7909483dd8f5ae630c Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sun, 21 Aug 2022 10:35:48 +0100 Subject: [PATCH 1/6] Make every not exist error unwrappable to a fs.ErrNotExist 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 --- models/asymkey/error.go | 53 +++++++++++- models/auth/oauth2.go | 11 +++ models/auth/source.go | 11 +++ models/auth/twofactor.go | 6 ++ models/auth/webauthn.go | 6 ++ models/db/engine.go | 2 +- models/db/error.go | 19 ++++- models/db/name.go | 31 ++++++- models/error.go | 129 ++++++++++++++++++++++++++++- models/foreignreference/error.go | 9 ++ models/git/lfs.go | 20 ++++- models/issues/comment.go | 5 ++ models/issues/content_history.go | 5 ++ models/issues/dependency.go | 13 +++ models/issues/issue.go | 5 ++ models/issues/label.go | 13 +++ models/issues/milestone.go | 5 ++ models/issues/pull.go | 9 ++ models/issues/reaction.go | 9 ++ models/issues/review.go | 9 ++ models/issues/stopwatch.go | 9 ++ models/issues/tracked_time.go | 6 +- models/notification.go | 2 +- models/organization/org.go | 9 ++ models/organization/team.go | 9 ++ models/project/project.go | 9 ++ models/pull/automerge.go | 2 +- models/repo/attachment.go | 5 ++ models/repo/redirect.go | 5 ++ models/repo/repo.go | 10 +++ models/repo/repo_unit.go | 5 ++ models/repo/topic.go | 5 ++ models/repo/update.go | 13 +++ models/task.go | 5 ++ models/user/email_address.go | 21 +++++ models/user/error.go | 21 +++++ models/user/external_login_user.go | 9 ++ models/user/openid.go | 5 ++ models/user/redirect.go | 5 ++ models/webhook/webhook.go | 9 ++ modules/git/error.go | 9 ++ 41 files changed, 527 insertions(+), 16 deletions(-) diff --git a/models/asymkey/error.go b/models/asymkey/error.go index 5d2be1f289590..5fe0e7f1e3af6 100644 --- a/models/asymkey/error.go +++ b/models/asymkey/error.go @@ -4,7 +4,10 @@ package asymkey -import "fmt" +import ( + "fmt" + "io/fs" +) // ErrKeyUnableVerify represents a "KeyUnableVerify" kind of error. type ErrKeyUnableVerify struct { @@ -36,6 +39,10 @@ func (err ErrKeyNotExist) Error() string { return fmt.Sprintf("public key does not exist [id: %d]", err.ID) } +func (err ErrKeyNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrKeyAlreadyExist represents a "KeyAlreadyExist" kind of error. type ErrKeyAlreadyExist struct { OwnerID int64 @@ -54,6 +61,10 @@ func (err ErrKeyAlreadyExist) Error() string { err.OwnerID, err.Fingerprint, err.Content) } +func (err ErrKeyAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // ErrKeyNameAlreadyUsed represents a "KeyNameAlreadyUsed" kind of error. type ErrKeyNameAlreadyUsed struct { OwnerID int64 @@ -70,6 +81,10 @@ func (err ErrKeyNameAlreadyUsed) Error() string { return fmt.Sprintf("public key already exists [owner_id: %d, name: %s]", err.OwnerID, err.Name) } +func (err ErrKeyNameAlreadyUsed) Unwrap() error { + return fs.ErrExist +} + // ErrGPGNoEmailFound represents a "ErrGPGNoEmailFound" kind of error. type ErrGPGNoEmailFound struct { FailedEmails []string @@ -132,6 +147,10 @@ func (err ErrGPGKeyNotExist) Error() string { return fmt.Sprintf("public gpg key does not exist [id: %d]", err.ID) } +func (err ErrGPGKeyNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrGPGKeyImportNotExist represents a "GPGKeyImportNotExist" kind of error. type ErrGPGKeyImportNotExist struct { ID string @@ -147,6 +166,10 @@ func (err ErrGPGKeyImportNotExist) Error() string { return fmt.Sprintf("public gpg key import does not exist [id: %s]", err.ID) } +func (err ErrGPGKeyImportNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrGPGKeyIDAlreadyUsed represents a "GPGKeyIDAlreadyUsed" kind of error. type ErrGPGKeyIDAlreadyUsed struct { KeyID string @@ -162,6 +185,10 @@ func (err ErrGPGKeyIDAlreadyUsed) Error() string { return fmt.Sprintf("public key already exists [key_id: %s]", err.KeyID) } +func (err ErrGPGKeyIDAlreadyUsed) Unwrap() error { + return fs.ErrExist +} + // ErrGPGKeyAccessDenied represents a "GPGKeyAccessDenied" kind of Error. type ErrGPGKeyAccessDenied struct { UserID int64 @@ -180,6 +207,10 @@ func (err ErrGPGKeyAccessDenied) Error() string { err.UserID, err.KeyID) } +func (err ErrGPGKeyAccessDenied) Unwrap() error { + return fs.ErrPermission +} + // ErrKeyAccessDenied represents a "KeyAccessDenied" kind of error. type ErrKeyAccessDenied struct { UserID int64 @@ -198,6 +229,10 @@ func (err ErrKeyAccessDenied) Error() string { err.UserID, err.KeyID, err.Note) } +func (err ErrKeyAccessDenied) Unwrap() error { + return fs.ErrPermission +} + // ErrDeployKeyNotExist represents a "DeployKeyNotExist" kind of error. type ErrDeployKeyNotExist struct { ID int64 @@ -215,6 +250,10 @@ func (err ErrDeployKeyNotExist) Error() string { return fmt.Sprintf("Deploy key does not exist [id: %d, key_id: %d, repo_id: %d]", err.ID, err.KeyID, err.RepoID) } +func (err ErrDeployKeyNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrDeployKeyAlreadyExist represents a "DeployKeyAlreadyExist" kind of error. type ErrDeployKeyAlreadyExist struct { KeyID int64 @@ -231,6 +270,10 @@ func (err ErrDeployKeyAlreadyExist) Error() string { return fmt.Sprintf("public key already exists [key_id: %d, repo_id: %d]", err.KeyID, err.RepoID) } +func (err ErrDeployKeyAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // ErrDeployKeyNameAlreadyUsed represents a "DeployKeyNameAlreadyUsed" kind of error. type ErrDeployKeyNameAlreadyUsed struct { RepoID int64 @@ -247,6 +290,10 @@ func (err ErrDeployKeyNameAlreadyUsed) Error() string { return fmt.Sprintf("public key with name already exists [repo_id: %d, name: %s]", err.RepoID, err.Name) } +func (err ErrDeployKeyNameAlreadyUsed) Unwrap() error { + return fs.ErrNotExist +} + // ErrSSHInvalidTokenSignature represents a "ErrSSHInvalidTokenSignature" kind of error. type ErrSSHInvalidTokenSignature struct { Wrapped error @@ -262,3 +309,7 @@ func IsErrSSHInvalidTokenSignature(err error) bool { func (err ErrSSHInvalidTokenSignature) Error() string { return "the provided signature does not sign the token with the provided key" } + +func (err ErrSSHInvalidTokenSignature) Unwrap() error { + return fs.ErrInvalid +} diff --git a/models/auth/oauth2.go b/models/auth/oauth2.go index ad1d80e25a85b..6803844572fe2 100644 --- a/models/auth/oauth2.go +++ b/models/auth/oauth2.go @@ -10,6 +10,7 @@ import ( "encoding/base32" "encoding/base64" "fmt" + "io/fs" "net/url" "strings" @@ -483,6 +484,11 @@ func (err ErrOAuthClientIDInvalid) Error() string { return fmt.Sprintf("Client ID invalid [Client ID: %s]", err.ClientID) } +// Unwrap unwraps this as a ErrNotExist err +func (err ErrOAuthClientIDInvalid) Unwrap() error { + return fs.ErrNotExist +} + // ErrOAuthApplicationNotFound will be thrown if id cannot be found type ErrOAuthApplicationNotFound struct { ID int64 @@ -499,6 +505,11 @@ func (err ErrOAuthApplicationNotFound) Error() string { return fmt.Sprintf("OAuth application not found [ID: %d]", err.ID) } +// Unwrap unwraps this as a ErrNotExist err +func (err ErrOAuthApplicationNotFound) Unwrap() error { + return fs.ErrNotExist +} + // GetActiveOAuth2ProviderSources returns all actived LoginOAuth2 sources func GetActiveOAuth2ProviderSources() ([]*Source, error) { sources := make([]*Source, 0, 1) diff --git a/models/auth/source.go b/models/auth/source.go index 6f4f5addcb9c6..708bcdf3231b5 100644 --- a/models/auth/source.go +++ b/models/auth/source.go @@ -7,6 +7,7 @@ package auth import ( "fmt" + "io/fs" "reflect" "code.gitea.io/gitea/models/db" @@ -366,6 +367,11 @@ func (err ErrSourceNotExist) Error() string { return fmt.Sprintf("login source does not exist [id: %d]", err.ID) } +// Unwrap unwraps this as a ErrNotExist err +func (err ErrSourceNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrSourceAlreadyExist represents a "SourceAlreadyExist" kind of error. type ErrSourceAlreadyExist struct { Name string @@ -381,6 +387,11 @@ func (err ErrSourceAlreadyExist) Error() string { return fmt.Sprintf("login source already exists [name: %s]", err.Name) } +// Unwrap unwraps this as a ErrExist err +func (err ErrSourceAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // ErrSourceInUse represents a "SourceInUse" kind of error. type ErrSourceInUse struct { ID int64 diff --git a/models/auth/twofactor.go b/models/auth/twofactor.go index c5bd972f91641..a487434e5b749 100644 --- a/models/auth/twofactor.go +++ b/models/auth/twofactor.go @@ -11,6 +11,7 @@ import ( "encoding/base32" "encoding/base64" "fmt" + "io/fs" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/secret" @@ -41,6 +42,11 @@ func (err ErrTwoFactorNotEnrolled) Error() string { return fmt.Sprintf("user not enrolled in 2FA [uid: %d]", err.UID) } +// Unwrap unwraps this as a ErrNotExist err +func (err ErrTwoFactorNotEnrolled) Unwrap() error { + return fs.ErrNotExist +} + // TwoFactor represents a two-factor authentication token. type TwoFactor struct { ID int64 `xorm:"pk autoincr"` diff --git a/models/auth/webauthn.go b/models/auth/webauthn.go index d3062342f545b..d7f38a033e083 100644 --- a/models/auth/webauthn.go +++ b/models/auth/webauthn.go @@ -7,6 +7,7 @@ package auth import ( "context" "fmt" + "io/fs" "strings" "code.gitea.io/gitea/models/db" @@ -29,6 +30,11 @@ func (err ErrWebAuthnCredentialNotExist) Error() string { return fmt.Sprintf("WebAuthn credential does not exist [credential_id: %x]", err.CredentialID) } +// Unwrap unwraps this as a ErrNotExist err +func (err ErrWebAuthnCredentialNotExist) Unwrap() error { + return fs.ErrNotExist +} + // IsErrWebAuthnCredentialNotExist checks if an error is a ErrWebAuthnCredentialNotExist. func IsErrWebAuthnCredentialNotExist(err error) bool { _, ok := err.(ErrWebAuthnCredentialNotExist) diff --git a/models/db/engine.go b/models/db/engine.go index 2c329300e3af2..f0c9ec46e9b08 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -225,7 +225,7 @@ func NamesToBean(names ...string) ([]interface{}, error) { for _, name := range names { bean, ok := beanMap[strings.ToLower(strings.TrimSpace(name))] if !ok { - return nil, fmt.Errorf("No table found that matches: %s", name) + return nil, fmt.Errorf("no table found that matches: %s", name) } if !gotBean[bean] { beans = append(beans, bean) diff --git a/models/db/error.go b/models/db/error.go index 65572299436a6..47c3a33cd44d2 100644 --- a/models/db/error.go +++ b/models/db/error.go @@ -6,6 +6,7 @@ package db import ( "fmt" + "io/fs" ) // ErrCancelled represents an error due to context cancellation @@ -45,7 +46,8 @@ func (err ErrSSHDisabled) Error() string { // ErrNotExist represents a non-exist error. type ErrNotExist struct { - ID int64 + Resource string + ID int64 } // IsErrNotExist checks if an error is an ErrNotExist @@ -55,5 +57,18 @@ func IsErrNotExist(err error) bool { } func (err ErrNotExist) Error() string { - return fmt.Sprintf("record does not exist [id: %d]", err.ID) + name := "record" + if err.Resource != "" { + name = err.Resource + } + + if err.ID != 0 { + return fmt.Sprintf("%s does not exist [id: %d]", name, err.ID) + } + return fmt.Sprintf("%s does not exist", name) +} + +// Unwrap unwraps this as a ErrNotExist err +func (err ErrNotExist) Unwrap() error { + return fs.ErrNotExist } diff --git a/models/db/name.go b/models/db/name.go index 9c9d18f184748..215d6692791e8 100644 --- a/models/db/name.go +++ b/models/db/name.go @@ -5,8 +5,8 @@ package db import ( - "errors" "fmt" + "io/fs" "regexp" "strings" "unicode/utf8" @@ -14,12 +14,22 @@ import ( var ( // ErrNameEmpty name is empty error - ErrNameEmpty = errors.New("Name is empty") + ErrNameEmpty = errNameEmpty{} // AlphaDashDotPattern characters prohibited in a user name (anything except A-Za-z0-9_.-) AlphaDashDotPattern = regexp.MustCompile(`[^\w-\.]`) ) +type errNameEmpty struct{} + +func (err errNameEmpty) Error() string { + return "name is empty" +} + +func (err errNameEmpty) Unwrap() error { + return fs.ErrInvalid +} + // ErrNameReserved represents a "reserved name" error. type ErrNameReserved struct { Name string @@ -35,6 +45,11 @@ func (err ErrNameReserved) Error() string { return fmt.Sprintf("name is reserved [name: %s]", err.Name) } +// Unwrap unwraps this as a ErrInvalid err +func (err ErrNameReserved) Unwrap() error { + return fs.ErrInvalid +} + // ErrNamePatternNotAllowed represents a "pattern not allowed" error. type ErrNamePatternNotAllowed struct { Pattern string @@ -50,6 +65,11 @@ func (err ErrNamePatternNotAllowed) Error() string { return fmt.Sprintf("name pattern is not allowed [pattern: %s]", err.Pattern) } +// Unwrap unwraps this as a ErrInvalid err +func (err ErrNamePatternNotAllowed) Unwrap() error { + return fs.ErrInvalid +} + // ErrNameCharsNotAllowed represents a "character not allowed in name" error. type ErrNameCharsNotAllowed struct { Name string @@ -62,7 +82,12 @@ func IsErrNameCharsNotAllowed(err error) bool { } func (err ErrNameCharsNotAllowed) Error() string { - return fmt.Sprintf("User name is invalid [%s]: must be valid alpha or numeric or dash(-_) or dot characters", err.Name) + return fmt.Sprintf("name is invalid [%s]: must be valid alpha or numeric or dash(-_) or dot characters", err.Name) +} + +// Unwrap unwraps this as a ErrInvalid err +func (err ErrNameCharsNotAllowed) Unwrap() error { + return fs.ErrInvalid } // IsUsableName checks if name is reserved or pattern of name is not allowed diff --git a/models/error.go b/models/error.go index 3c617904f8dd8..43ac804b3eafa 100644 --- a/models/error.go +++ b/models/error.go @@ -7,6 +7,7 @@ package models import ( "fmt" + "io/fs" repo_model "code.gitea.io/gitea/models/repo" "code.gitea.io/gitea/modules/git" @@ -79,6 +80,10 @@ func (err ErrWikiAlreadyExist) Error() string { return fmt.Sprintf("wiki page already exists [title: %s]", err.Title) } +func (err ErrWikiAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // ErrWikiReservedName represents a reserved name error. type ErrWikiReservedName struct { Title string @@ -94,6 +99,10 @@ func (err ErrWikiReservedName) Error() string { return fmt.Sprintf("wiki title is reserved: %s", err.Title) } +func (err ErrWikiReservedName) Unwrap() error { + return fs.ErrInvalid +} + // ErrWikiInvalidFileName represents an invalid wiki file name. type ErrWikiInvalidFileName struct { FileName string @@ -109,6 +118,10 @@ func (err ErrWikiInvalidFileName) Error() string { return fmt.Sprintf("Invalid wiki filename: %s", err.FileName) } +func (err ErrWikiInvalidFileName) Unwrap() error { + return fs.ErrInvalid +} + // _____ ___________ __ // / _ \ ____ ____ ____ ______ _____\__ ___/___ | | __ ____ ____ // / /_\ \_/ ___\/ ___\/ __ \ / ___// ___/ | | / _ \| |/ // __ \ / \ @@ -131,6 +144,10 @@ func (err ErrAccessTokenNotExist) Error() string { return fmt.Sprintf("access token does not exist [sha: %s]", err.Token) } +func (err ErrAccessTokenNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrAccessTokenEmpty represents a "AccessTokenEmpty" kind of error. type ErrAccessTokenEmpty struct{} @@ -144,14 +161,18 @@ func (err ErrAccessTokenEmpty) Error() string { return "access token is empty" } +func (err ErrAccessTokenEmpty) Unwrap() error { + return fs.ErrInvalid +} + // ErrNoPendingRepoTransfer is an error type for repositories without a pending // transfer request 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 @@ -161,6 +182,10 @@ func IsErrNoPendingTransfer(err error) bool { return ok } +func (err ErrNoPendingRepoTransfer) Unwrap() error { + return fs.ErrNotExist +} + // ErrRepoTransferInProgress represents the state of a repository that has an // ongoing transfer type ErrRepoTransferInProgress struct { @@ -178,6 +203,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 fs.ErrExist +} + // ErrForkAlreadyExist represents a "ForkAlreadyExist" kind of error. type ErrForkAlreadyExist struct { Uname string @@ -195,6 +224,10 @@ func (err ErrForkAlreadyExist) Error() string { return fmt.Sprintf("repository is already forked by user [uname: %s, repo path: %s, fork path: %s]", err.Uname, err.RepoName, err.ForkName) } +func (err ErrForkAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // ErrInvalidCloneAddr represents a "InvalidCloneAddr" kind of error. type ErrInvalidCloneAddr struct { Host string @@ -228,6 +261,10 @@ func (err *ErrInvalidCloneAddr) Error() string { return fmt.Sprintf("migration/cloning from '%s' is not allowed", err.Host) } +func (err *ErrInvalidCloneAddr) Unwrap() error { + return fs.ErrInvalid +} + // ErrUpdateTaskNotExist represents a "UpdateTaskNotExist" kind of error. type ErrUpdateTaskNotExist struct { UUID string @@ -243,6 +280,10 @@ func (err ErrUpdateTaskNotExist) Error() string { return fmt.Sprintf("update task does not exist [uuid: %s]", err.UUID) } +func (err ErrUpdateTaskNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrReleaseAlreadyExist represents a "ReleaseAlreadyExist" kind of error. type ErrReleaseAlreadyExist struct { TagName string @@ -258,6 +299,10 @@ func (err ErrReleaseAlreadyExist) Error() string { return fmt.Sprintf("release tag already exist [tag_name: %s]", err.TagName) } +func (err ErrReleaseAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // ErrReleaseNotExist represents a "ReleaseNotExist" kind of error. type ErrReleaseNotExist struct { ID int64 @@ -274,6 +319,10 @@ func (err ErrReleaseNotExist) Error() string { return fmt.Sprintf("release tag does not exist [id: %d, tag_name: %s]", err.ID, err.TagName) } +func (err ErrReleaseNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrInvalidTagName represents a "InvalidTagName" kind of error. type ErrInvalidTagName struct { TagName string @@ -289,6 +338,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 fs.ErrInvalid +} + // ErrProtectedTagName represents a "ProtectedTagName" kind of error. type ErrProtectedTagName struct { TagName string @@ -304,6 +357,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 fs.ErrPermission +} + // ErrRepoFileAlreadyExists represents a "RepoFileAlreadyExist" kind of error. type ErrRepoFileAlreadyExists struct { Path string @@ -319,6 +376,10 @@ func (err ErrRepoFileAlreadyExists) Error() string { return fmt.Sprintf("repository file already exists [path: %s]", err.Path) } +func (err ErrRepoFileAlreadyExists) Unwrap() error { + return fs.ErrExist +} + // ErrRepoFileDoesNotExist represents a "RepoFileDoesNotExist" kind of error. type ErrRepoFileDoesNotExist struct { Path string @@ -335,6 +396,10 @@ func (err ErrRepoFileDoesNotExist) Error() string { return fmt.Sprintf("repository file does not exist [path: %s]", err.Path) } +func (err ErrRepoFileDoesNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrFilenameInvalid represents a "FilenameInvalid" kind of error. type ErrFilenameInvalid struct { Path string @@ -350,6 +415,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 fs.ErrInvalid +} + // ErrUserCannotCommit represents "UserCannotCommit" kind of error. type ErrUserCannotCommit struct { UserName string @@ -365,6 +434,10 @@ func (err ErrUserCannotCommit) Error() string { return fmt.Sprintf("user cannot commit to repo [user: %s]", err.UserName) } +func (err ErrUserCannotCommit) Unwrap() error { + return fs.ErrPermission +} + // ErrFilePathInvalid represents a "FilePathInvalid" kind of error. type ErrFilePathInvalid struct { Message string @@ -386,6 +459,10 @@ func (err ErrFilePathInvalid) Error() string { return fmt.Sprintf("path is invalid [path: %s]", err.Path) } +func (err ErrFilePathInvalid) Unwrap() error { + return fs.ErrInvalid +} + // ErrFilePathProtected represents a "FilePathProtected" kind of error. type ErrFilePathProtected struct { Message string @@ -405,6 +482,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 fs.ErrPermission +} + // __________ .__ // \______ \____________ ____ ____ | |__ // | | _/\_ __ \__ \ / \_/ ___\| | \ @@ -427,6 +508,10 @@ func (err ErrBranchDoesNotExist) Error() string { return fmt.Sprintf("branch does not exist [name: %s]", err.BranchName) } +func (err ErrBranchDoesNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrBranchAlreadyExists represents an error that branch with such name already exists. type ErrBranchAlreadyExists struct { BranchName string @@ -442,6 +527,10 @@ func (err ErrBranchAlreadyExists) Error() string { return fmt.Sprintf("branch already exists [name: %s]", err.BranchName) } +func (err ErrBranchAlreadyExists) Unwrap() error { + return fs.ErrExist +} + // ErrBranchNameConflict represents an error that branch name conflicts with other branch. type ErrBranchNameConflict struct { BranchName string @@ -457,6 +546,10 @@ func (err ErrBranchNameConflict) Error() string { return fmt.Sprintf("branch conflicts with existing branch [name: %s]", err.BranchName) } +func (err ErrBranchNameConflict) Unwrap() error { + return fs.ErrExist +} + // ErrBranchesEqual represents an error that branch name conflicts with other branch. type ErrBranchesEqual struct { BaseBranchName string @@ -473,6 +566,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 fs.ErrInvalid +} + // ErrDisallowedToMerge represents an error that a branch is protected and the current user is not allowed to modify it. type ErrDisallowedToMerge struct { Reason string @@ -488,6 +585,10 @@ func (err ErrDisallowedToMerge) Error() string { return fmt.Sprintf("not allowed to merge [reason: %s]", err.Reason) } +func (err ErrDisallowedToMerge) Unwrap() error { + return fs.ErrPermission +} + // ErrTagAlreadyExists represents an error that tag with such name already exists. type ErrTagAlreadyExists struct { TagName string @@ -503,6 +604,10 @@ func (err ErrTagAlreadyExists) Error() string { return fmt.Sprintf("tag already exists [name: %s]", err.TagName) } +func (err ErrTagAlreadyExists) Unwrap() error { + return fs.ErrExist +} + // ErrSHADoesNotMatch represents a "SHADoesNotMatch" kind of error. type ErrSHADoesNotMatch struct { Path string @@ -535,6 +640,10 @@ func (err ErrSHANotFound) Error() string { return fmt.Sprintf("sha not found [%s]", err.SHA) } +func (err ErrSHANotFound) Unwrap() error { + return fs.ErrNotExist +} + // ErrCommitIDDoesNotMatch represents a "CommitIDDoesNotMatch" kind of error. type ErrCommitIDDoesNotMatch struct { GivenCommitID string @@ -581,6 +690,10 @@ func (err ErrInvalidMergeStyle) Error() string { err.ID, err.Style) } +func (err ErrInvalidMergeStyle) Unwrap() error { + return fs.ErrInvalid +} + // ErrMergeConflicts represents an error if merging fails with a conflict type ErrMergeConflicts struct { Style repo_model.MergeStyle @@ -680,6 +793,10 @@ func (err ErrStopwatchNotExist) Error() string { return fmt.Sprintf("stopwatch does not exist [id: %d]", err.ID) } +func (err ErrStopwatchNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ___________ __ .______________.__ // \__ ___/___________ ____ | | __ ____ __| _/\__ ___/|__| _____ ____ // | | \_ __ \__ \ _/ ___\| |/ // __ \ / __ | | | | |/ \_/ __ \ @@ -702,6 +819,10 @@ func (err ErrTrackedTimeNotExist) Error() string { return fmt.Sprintf("tracked time does not exist [id: %d]", err.ID) } +func (err ErrTrackedTimeNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ____ ___ .__ .___ // | | \______ | | _________ __| _/ // | | /\____ \| | / _ \__ \ / __ | @@ -725,3 +846,7 @@ func IsErrUploadNotExist(err error) bool { func (err ErrUploadNotExist) Error() string { return fmt.Sprintf("attachment does not exist [id: %d, uuid: %s]", err.ID, err.UUID) } + +func (err ErrUploadNotExist) Unwrap() error { + return fs.ErrNotExist +} diff --git a/models/foreignreference/error.go b/models/foreignreference/error.go index d783a087301ea..899855bf2f701 100644 --- a/models/foreignreference/error.go +++ b/models/foreignreference/error.go @@ -6,6 +6,7 @@ package foreignreference import ( "fmt" + "io/fs" ) // ErrLocalIndexNotExist represents a "LocalIndexNotExist" kind of error. @@ -25,6 +26,10 @@ func (err ErrLocalIndexNotExist) Error() string { return fmt.Sprintf("repository %d has no LocalIndex for ForeignIndex %d of type %s", err.RepoID, err.ForeignIndex, err.Type) } +func (err ErrLocalIndexNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrForeignIndexNotExist represents a "ForeignIndexNotExist" kind of error. type ErrForeignIndexNotExist struct { RepoID int64 @@ -41,3 +46,7 @@ func IsErrForeignIndexNotExist(err error) bool { func (err ErrForeignIndexNotExist) Error() string { return fmt.Sprintf("repository %d has no ForeignIndex for LocalIndex %d of type %s", err.RepoID, err.LocalIndex, err.Type) } + +func (err ErrForeignIndexNotExist) Unwrap() error { + return fs.ErrNotExist +} diff --git a/models/git/lfs.go b/models/git/lfs.go index 179da3120ae1c..c270bd352bb5b 100644 --- a/models/git/lfs.go +++ b/models/git/lfs.go @@ -6,8 +6,8 @@ package git import ( "context" - "errors" "fmt" + "io/fs" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/perm" @@ -38,6 +38,10 @@ func (err ErrLFSLockNotExist) Error() string { return fmt.Sprintf("lfs lock does not exist [id: %d, rid: %d, path: %s]", err.ID, err.RepoID, err.Path) } +func (err ErrLFSLockNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrLFSUnauthorizedAction represents a "LFSUnauthorizedAction" kind of error. type ErrLFSUnauthorizedAction struct { RepoID int64 @@ -58,6 +62,10 @@ func (err ErrLFSUnauthorizedAction) Error() string { return fmt.Sprintf("User %s doesn't have read access for lfs lock [rid: %d]", err.UserName, err.RepoID) } +func (err ErrLFSUnauthorizedAction) Unwrap() error { + return fs.ErrPermission +} + // ErrLFSLockAlreadyExist represents a "LFSLockAlreadyExist" kind of error. type ErrLFSLockAlreadyExist struct { RepoID int64 @@ -74,6 +82,10 @@ func (err ErrLFSLockAlreadyExist) Error() string { return fmt.Sprintf("lfs lock already exists [rid: %d, path: %s]", err.RepoID, err.Path) } +func (err ErrLFSLockAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // ErrLFSFileLocked represents a "LFSFileLocked" kind of error. type ErrLFSFileLocked struct { RepoID int64 @@ -91,6 +103,10 @@ func (err ErrLFSFileLocked) Error() string { return fmt.Sprintf("File is lfs locked [repo: %d, locked by: %s, path: %s]", err.RepoID, err.UserName, err.Path) } +func (err ErrLFSFileLocked) Unwrap() error { + return fs.ErrPermission +} + // LFSMetaObject stores metadata for LFS tracked files. type LFSMetaObject struct { ID int64 `xorm:"pk autoincr"` @@ -114,7 +130,7 @@ type LFSTokenResponse struct { // ErrLFSObjectNotExist is returned from lfs models functions in order // to differentiate between database and missing object errors. -var ErrLFSObjectNotExist = errors.New("LFS Meta object does not exist") +var ErrLFSObjectNotExist = db.ErrNotExist{Resource: "LFS Meta object"} // NewLFSMetaObject stores a given populated LFSMetaObject structure in the database // if it is not already present. diff --git a/models/issues/comment.go b/models/issues/comment.go index a71afda9e0e8f..788a15a4004ad 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -9,6 +9,7 @@ package issues import ( "context" "fmt" + "io/fs" "regexp" "strconv" "strings" @@ -49,6 +50,10 @@ func (err ErrCommentNotExist) Error() string { return fmt.Sprintf("comment does not exist [id: %d, issue_id: %d]", err.ID, err.IssueID) } +func (err ErrCommentNotExist) Unwrap() error { + return fs.ErrNotExist +} + // CommentType defines whether a comment is just a simple comment, an action (like close) or a reference. type CommentType int diff --git a/models/issues/content_history.go b/models/issues/content_history.go index 3e321784bd0a8..7a683c423cdfc 100644 --- a/models/issues/content_history.go +++ b/models/issues/content_history.go @@ -7,6 +7,7 @@ package issues import ( "context" "fmt" + "io/fs" "code.gitea.io/gitea/models/avatars" "code.gitea.io/gitea/models/db" @@ -201,6 +202,10 @@ func (err ErrIssueContentHistoryNotExist) Error() string { return fmt.Sprintf("issue content history does not exist [id: %d]", err.ID) } +func (err ErrIssueContentHistoryNotExist) Unwrap() error { + return fs.ErrPermission +} + // GetIssueContentHistoryByID get issue content history func GetIssueContentHistoryByID(dbCtx context.Context, id int64) (*ContentHistory, error) { h := &ContentHistory{} diff --git a/models/issues/dependency.go b/models/issues/dependency.go index d664c0758e77e..dbdbb5a64054d 100644 --- a/models/issues/dependency.go +++ b/models/issues/dependency.go @@ -7,6 +7,7 @@ package issues import ( "context" "fmt" + "io/fs" "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" @@ -29,6 +30,10 @@ func (err ErrDependencyExists) Error() string { return fmt.Sprintf("issue dependency does already exist [issue id: %d, dependency id: %d]", err.IssueID, err.DependencyID) } +func (err ErrDependencyExists) Unwrap() error { + return fs.ErrExist +} + // ErrDependencyNotExists represents a "DependencyAlreadyExists" kind of error. type ErrDependencyNotExists struct { IssueID int64 @@ -45,6 +50,10 @@ func (err ErrDependencyNotExists) Error() string { return fmt.Sprintf("issue dependency does not exist [issue id: %d, dependency id: %d]", err.IssueID, err.DependencyID) } +func (err ErrDependencyNotExists) Unwrap() error { + return fs.ErrNotExist +} + // ErrCircularDependency represents a "DependencyCircular" kind of error. type ErrCircularDependency struct { IssueID int64 @@ -91,6 +100,10 @@ func (err ErrUnknownDependencyType) Error() string { return fmt.Sprintf("unknown dependency type [type: %d]", err.Type) } +func (err ErrUnknownDependencyType) Unwrap() error { + return fs.ErrInvalid +} + // IssueDependency represents an issue dependency type IssueDependency struct { ID int64 `xorm:"pk autoincr"` diff --git a/models/issues/issue.go b/models/issues/issue.go index 5bdb60f7c08c5..71f1376d5b8f4 100644 --- a/models/issues/issue.go +++ b/models/issues/issue.go @@ -8,6 +8,7 @@ package issues import ( "context" "fmt" + "io/fs" "regexp" "sort" "strconv" @@ -52,6 +53,10 @@ func (err ErrIssueNotExist) Error() string { return fmt.Sprintf("issue does not exist [id: %d, repo_id: %d, index: %d]", err.ID, err.RepoID, err.Index) } +func (err ErrIssueNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrIssueIsClosed represents a "IssueIsClosed" kind of error. type ErrIssueIsClosed struct { ID int64 diff --git a/models/issues/label.go b/models/issues/label.go index 667a608687436..0d206ee55df61 100644 --- a/models/issues/label.go +++ b/models/issues/label.go @@ -9,6 +9,7 @@ import ( "context" "fmt" "html/template" + "io/fs" "math" "regexp" "strconv" @@ -38,6 +39,10 @@ func (err ErrRepoLabelNotExist) Error() string { return fmt.Sprintf("label does not exist [label_id: %d, repo_id: %d]", err.LabelID, err.RepoID) } +func (err ErrRepoLabelNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrOrgLabelNotExist represents a "OrgLabelNotExist" kind of error. type ErrOrgLabelNotExist struct { LabelID int64 @@ -54,6 +59,10 @@ func (err ErrOrgLabelNotExist) Error() string { return fmt.Sprintf("label does not exist [label_id: %d, org_id: %d]", err.LabelID, err.OrgID) } +func (err ErrOrgLabelNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrLabelNotExist represents a "LabelNotExist" kind of error. type ErrLabelNotExist struct { LabelID int64 @@ -69,6 +78,10 @@ func (err ErrLabelNotExist) Error() string { return fmt.Sprintf("label does not exist [label_id: %d]", err.LabelID) } +func (err ErrLabelNotExist) Unwrap() error { + return fs.ErrNotExist +} + // LabelColorPattern is a regexp witch can validate LabelColor var LabelColorPattern = regexp.MustCompile("^#?(?:[0-9a-fA-F]{6}|[0-9a-fA-F]{3})$") diff --git a/models/issues/milestone.go b/models/issues/milestone.go index 1021938b205ae..ed5e460b15c9f 100644 --- a/models/issues/milestone.go +++ b/models/issues/milestone.go @@ -7,6 +7,7 @@ package issues import ( "context" "fmt" + "io/fs" "strings" "time" @@ -39,6 +40,10 @@ func (err ErrMilestoneNotExist) Error() string { return fmt.Sprintf("milestone does not exist [id: %d, repo_id: %d]", err.ID, err.RepoID) } +func (err ErrMilestoneNotExist) Unwrap() error { + return fs.ErrNotExist +} + // Milestone represents a milestone of repository. type Milestone struct { ID int64 `xorm:"pk autoincr"` diff --git a/models/issues/pull.go b/models/issues/pull.go index f96b03445e91f..afe24ba9df89d 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -9,6 +9,7 @@ import ( "context" "fmt" "io" + "io/fs" "strings" "code.gitea.io/gitea/models/db" @@ -46,6 +47,10 @@ func (err ErrPullRequestNotExist) Error() string { err.ID, err.IssueID, err.HeadRepoID, err.BaseRepoID, err.HeadBranch, err.BaseBranch) } +func (err ErrPullRequestNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrPullRequestAlreadyExists represents a "PullRequestAlreadyExists"-error type ErrPullRequestAlreadyExists struct { ID int64 @@ -68,6 +73,10 @@ func (err ErrPullRequestAlreadyExists) Error() string { err.ID, err.IssueID, err.HeadRepoID, err.BaseRepoID, err.HeadBranch, err.BaseBranch) } +func (err ErrPullRequestAlreadyExists) Unwrap() error { + return fs.ErrExist +} + // ErrPullRequestHeadRepoMissing represents a "ErrPullRequestHeadRepoMissing" error type ErrPullRequestHeadRepoMissing struct { ID int64 diff --git a/models/issues/reaction.go b/models/issues/reaction.go index 87d6ff431054a..3a00e2dda7e86 100644 --- a/models/issues/reaction.go +++ b/models/issues/reaction.go @@ -8,6 +8,7 @@ import ( "bytes" "context" "fmt" + "io/fs" "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" @@ -34,6 +35,10 @@ func (err ErrForbiddenIssueReaction) Error() string { return fmt.Sprintf("'%s' is not an allowed reaction", err.Reaction) } +func (err ErrForbiddenIssueReaction) Unwrap() error { + return fs.ErrPermission +} + // ErrReactionAlreadyExist is used when a existing reaction was try to created type ErrReactionAlreadyExist struct { Reaction string @@ -49,6 +54,10 @@ func (err ErrReactionAlreadyExist) Error() string { return fmt.Sprintf("reaction '%s' already exists", err.Reaction) } +func (err ErrReactionAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // Reaction represents a reactions on issues and comments. type Reaction struct { ID int64 `xorm:"pk autoincr"` diff --git a/models/issues/review.go b/models/issues/review.go index 5835900801778..2136ab26f828f 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -7,6 +7,7 @@ package issues import ( "context" "fmt" + "io/fs" "strings" "code.gitea.io/gitea/models/db" @@ -39,6 +40,10 @@ func (err ErrReviewNotExist) Error() string { return fmt.Sprintf("review does not exist [id: %d]", err.ID) } +func (err ErrReviewNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrNotValidReviewRequest an not allowed review request modify type ErrNotValidReviewRequest struct { Reason string @@ -59,6 +64,10 @@ func (err ErrNotValidReviewRequest) Error() string { err.RepoID) } +func (err ErrNotValidReviewRequest) Unwrap() error { + return fs.ErrInvalid +} + // ReviewType defines the sort of feedback a review gives type ReviewType int diff --git a/models/issues/stopwatch.go b/models/issues/stopwatch.go index 0a7ad41f9ced5..854a6d889e759 100644 --- a/models/issues/stopwatch.go +++ b/models/issues/stopwatch.go @@ -7,6 +7,7 @@ package issues import ( "context" "fmt" + "io/fs" "time" "code.gitea.io/gitea/models/db" @@ -25,6 +26,10 @@ func (err ErrIssueStopwatchNotExist) Error() string { return fmt.Sprintf("issue stopwatch doesn't exist[uid: %d, issue_id: %d", err.UserID, err.IssueID) } +func (err ErrIssueStopwatchNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrIssueStopwatchAlreadyExist represents an error that stopwatch is already exist type ErrIssueStopwatchAlreadyExist struct { UserID int64 @@ -35,6 +40,10 @@ func (err ErrIssueStopwatchAlreadyExist) Error() string { return fmt.Sprintf("issue stopwatch already exists[uid: %d, issue_id: %d", err.UserID, err.IssueID) } +func (err ErrIssueStopwatchAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // Stopwatch represents a stopwatch for time tracking. type Stopwatch struct { ID int64 `xorm:"pk autoincr"` diff --git a/models/issues/tracked_time.go b/models/issues/tracked_time.go index 9f8767362fab3..ca21eb5149626 100644 --- a/models/issues/tracked_time.go +++ b/models/issues/tracked_time.go @@ -236,7 +236,7 @@ func DeleteIssueUserTimes(issue *Issue, user *user_model.User) error { return err } if removedTime == 0 { - return db.ErrNotExist{} + return db.ErrNotExist{Resource: "tracked_time"} } if err := issue.LoadRepo(ctx); err != nil { @@ -296,7 +296,7 @@ func deleteTimes(ctx context.Context, opts FindTrackedTimesOptions) (removedTime func deleteTime(ctx context.Context, t *TrackedTime) error { if t.Deleted { - return db.ErrNotExist{ID: t.ID} + return db.ErrNotExist{Resource: "tracked_time", ID: t.ID} } t.Deleted = true _, err := db.GetEngine(ctx).ID(t.ID).Cols("deleted").Update(t) @@ -310,7 +310,7 @@ func GetTrackedTimeByID(id int64) (*TrackedTime, error) { if err != nil { return nil, err } else if !has { - return nil, db.ErrNotExist{ID: id} + return nil, db.ErrNotExist{Resource: "tracked_time", ID: id} } return time, nil } diff --git a/models/notification.go b/models/notification.go index fdc4ffad1326f..f24eebbdc24a1 100644 --- a/models/notification.go +++ b/models/notification.go @@ -817,7 +817,7 @@ func getNotificationByID(ctx context.Context, notificationID int64) (*Notificati } if !ok { - return nil, db.ErrNotExist{ID: notificationID} + return nil, db.ErrNotExist{Resource: "notification", ID: notificationID} } return notification, nil diff --git a/models/organization/org.go b/models/organization/org.go index 044ea065637c5..bb5684e5e6639 100644 --- a/models/organization/org.go +++ b/models/organization/org.go @@ -8,6 +8,7 @@ package organization import ( "context" "fmt" + "io/fs" "strings" "code.gitea.io/gitea/models/db" @@ -45,6 +46,10 @@ func (err ErrOrgNotExist) Error() string { return fmt.Sprintf("org does not exist [id: %d, name: %s]", err.ID, err.Name) } +func (err ErrOrgNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrLastOrgOwner represents a "LastOrgOwner" kind of error. type ErrLastOrgOwner struct { UID int64 @@ -73,6 +78,10 @@ func (err ErrUserNotAllowedCreateOrg) Error() string { return "user is not allowed to create organizations" } +func (err ErrUserNotAllowedCreateOrg) Unwrap() error { + return fs.ErrPermission +} + // Organization represents an organization type Organization user_model.User diff --git a/models/organization/team.go b/models/organization/team.go index 0b53c84d67036..1ad21c3c3a74a 100644 --- a/models/organization/team.go +++ b/models/organization/team.go @@ -8,6 +8,7 @@ package organization import ( "context" "fmt" + "io/fs" "strings" "code.gitea.io/gitea/models/db" @@ -43,6 +44,10 @@ func (err ErrTeamAlreadyExist) Error() string { return fmt.Sprintf("team already exists [org_id: %d, name: %s]", err.OrgID, err.Name) } +func (err ErrTeamAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // ErrTeamNotExist represents a "TeamNotExist" error type ErrTeamNotExist struct { OrgID int64 @@ -60,6 +65,10 @@ func (err ErrTeamNotExist) Error() string { return fmt.Sprintf("team does not exist [org_id %d, team_id %d, name: %s]", err.OrgID, err.TeamID, err.Name) } +func (err ErrTeamNotExist) Unwrap() error { + return fs.ErrNotExist +} + // OwnerTeamName return the owner team name const OwnerTeamName = "Owners" diff --git a/models/project/project.go b/models/project/project.go index 86a77947d8838..1447c70fb07cb 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -8,6 +8,7 @@ import ( "context" "errors" "fmt" + "io/fs" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/setting" @@ -55,6 +56,10 @@ func (err ErrProjectNotExist) Error() string { return fmt.Sprintf("projects does not exist [id: %d]", err.ID) } +func (err ErrProjectNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrProjectBoardNotExist represents a "ProjectBoardNotExist" kind of error. type ErrProjectBoardNotExist struct { BoardID int64 @@ -70,6 +75,10 @@ func (err ErrProjectBoardNotExist) Error() string { return fmt.Sprintf("project board does not exist [id: %d]", err.BoardID) } +func (err ErrProjectBoardNotExist) Unwrap() error { + return fs.ErrNotExist +} + // Project represents a project board type Project struct { ID int64 `xorm:"pk autoincr"` diff --git a/models/pull/automerge.go b/models/pull/automerge.go index d0aca2e85f976..16ab5af09391b 100644 --- a/models/pull/automerge.go +++ b/models/pull/automerge.go @@ -90,7 +90,7 @@ func DeleteScheduledAutoMerge(ctx context.Context, pullID int64) error { if err != nil { return err } else if !exist { - return db.ErrNotExist{ID: pullID} + return db.ErrNotExist{Resource: "auto_merge", ID: pullID} } _, err = db.GetEngine(ctx).ID(scheduledPRM.ID).Delete(&AutoMerge{}) diff --git a/models/repo/attachment.go b/models/repo/attachment.go index afec78a4254b7..d054aeca62270 100644 --- a/models/repo/attachment.go +++ b/models/repo/attachment.go @@ -7,6 +7,7 @@ package repo import ( "context" "fmt" + "io/fs" "net/url" "path" @@ -83,6 +84,10 @@ func (err ErrAttachmentNotExist) Error() string { return fmt.Sprintf("attachment does not exist [id: %d, uuid: %s]", err.ID, err.UUID) } +func (err ErrAttachmentNotExist) Unwrap() error { + return fs.ErrNotExist +} + // GetAttachmentByID returns attachment by given id func GetAttachmentByID(ctx context.Context, id int64) (*Attachment, error) { attach := &Attachment{} diff --git a/models/repo/redirect.go b/models/repo/redirect.go index 88fad6f3e34ed..e9aa8b180601b 100644 --- a/models/repo/redirect.go +++ b/models/repo/redirect.go @@ -7,6 +7,7 @@ package repo import ( "context" "fmt" + "io/fs" "strings" "code.gitea.io/gitea/models/db" @@ -28,6 +29,10 @@ func (err ErrRedirectNotExist) Error() string { return fmt.Sprintf("repository redirect does not exist [uid: %d, name: %s]", err.OwnerID, err.RepoName) } +func (err ErrRedirectNotExist) Unwrap() error { + return fs.ErrNotExist +} + // Redirect represents that a repo name should be redirected to another type Redirect struct { ID int64 `xorm:"pk autoincr"` diff --git a/models/repo/repo.go b/models/repo/repo.go index bb2a7468ff42a..da131ab66a9c1 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -8,6 +8,7 @@ import ( "context" "fmt" "html/template" + "io/fs" "net" "net/url" "path/filepath" @@ -41,6 +42,10 @@ func (err ErrUserDoesNotHaveAccessToRepo) Error() string { return fmt.Sprintf("user doesn't have access to repo [user_id: %d, repo_name: %s]", err.UserID, err.RepoName) } +func (err ErrUserDoesNotHaveAccessToRepo) Unwrap() error { + return fs.ErrPermission +} + var ( reservedRepoNames = []string{".", "..", "-"} reservedRepoPatterns = []string{"*.git", "*.wiki", "*.rss", "*.atom"} @@ -641,6 +646,11 @@ func (err ErrRepoNotExist) Error() string { err.ID, err.UID, err.OwnerName, err.Name) } +// Unwrap unwraps this error as a ErrNotExist error +func (err ErrRepoNotExist) Unwrap() error { + return fs.ErrNotExist +} + // GetRepositoryByOwnerAndNameCtx returns the repository by given owner name and repo name func GetRepositoryByOwnerAndNameCtx(ctx context.Context, ownerName, repoName string) (*Repository, error) { var repo Repository diff --git a/models/repo/repo_unit.go b/models/repo/repo_unit.go index da3e19decea82..3b9161f7c09c2 100644 --- a/models/repo/repo_unit.go +++ b/models/repo/repo_unit.go @@ -7,6 +7,7 @@ package repo import ( "context" "fmt" + "io/fs" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unit" @@ -33,6 +34,10 @@ func (err ErrUnitTypeNotExist) Error() string { return fmt.Sprintf("Unit type does not exist: %s", err.UT.String()) } +func (err ErrUnitTypeNotExist) Unwrap() error { + return fs.ErrNotExist +} + // RepoUnit describes all units of a repository type RepoUnit struct { //revive:disable-line:exported ID int64 diff --git a/models/repo/topic.go b/models/repo/topic.go index 2a16467215d3a..c89cd700068fd 100644 --- a/models/repo/topic.go +++ b/models/repo/topic.go @@ -7,6 +7,7 @@ package repo import ( "context" "fmt" + "io/fs" "regexp" "strings" @@ -54,6 +55,10 @@ func (err ErrTopicNotExist) Error() string { return fmt.Sprintf("topic is not exist [name: %s]", err.Name) } +func (err ErrTopicNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ValidateTopic checks a topic by length and match pattern rules func ValidateTopic(topic string) bool { return len(topic) <= 35 && topicPattern.MatchString(topic) diff --git a/models/repo/update.go b/models/repo/update.go index 07776ebc01941..f489bdbf368f4 100644 --- a/models/repo/update.go +++ b/models/repo/update.go @@ -7,6 +7,7 @@ package repo import ( "context" "fmt" + "io/fs" "strings" "time" @@ -63,6 +64,10 @@ func (err ErrReachLimitOfRepo) Error() string { return fmt.Sprintf("user has reached maximum limit of repositories [limit: %d]", err.Limit) } +func (err ErrReachLimitOfRepo) Unwrap() error { + return fs.ErrPermission +} + // ErrRepoAlreadyExist represents a "RepoAlreadyExist" kind of error. type ErrRepoAlreadyExist struct { Uname string @@ -79,6 +84,10 @@ func (err ErrRepoAlreadyExist) Error() string { return fmt.Sprintf("repository already exists [uname: %s, name: %s]", err.Uname, err.Name) } +func (err ErrRepoAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // ErrRepoFilesAlreadyExist represents a "RepoFilesAlreadyExist" kind of error. type ErrRepoFilesAlreadyExist struct { Uname string @@ -95,6 +104,10 @@ func (err ErrRepoFilesAlreadyExist) Error() string { return fmt.Sprintf("repository files already exist [uname: %s, name: %s]", err.Uname, err.Name) } +func (err ErrRepoFilesAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // CheckCreateRepository check if could created a repository func CheckCreateRepository(doer, u *user_model.User, name string, overwriteOrAdopt bool) error { if !doer.CanCreateRepo() { diff --git a/models/task.go b/models/task.go index 67f04d9562f42..d903139b8a9de 100644 --- a/models/task.go +++ b/models/task.go @@ -7,6 +7,7 @@ package models import ( "context" "fmt" + "io/fs" "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" @@ -167,6 +168,10 @@ func (err ErrTaskDoesNotExist) Error() string { err.ID, err.RepoID, err.Type) } +func (err ErrTaskDoesNotExist) Unwrap() error { + return fs.ErrNotExist +} + // GetMigratingTask returns the migrating task by repo's id func GetMigratingTask(repoID int64) (*Task, error) { task := Task{ diff --git a/models/user/email_address.go b/models/user/email_address.go index c931db9c16720..576a4b1eb1bb8 100644 --- a/models/user/email_address.go +++ b/models/user/email_address.go @@ -9,6 +9,7 @@ import ( "context" "errors" "fmt" + "io/fs" "net/mail" "regexp" "strings" @@ -40,6 +41,10 @@ func (err ErrEmailCharIsNotSupported) Error() string { return fmt.Sprintf("e-mail address contains unsupported character [email: %s]", err.Email) } +func (err ErrEmailCharIsNotSupported) Unwrap() error { + return fs.ErrInvalid +} + // ErrEmailInvalid represents an error where the email address does not comply with RFC 5322 type ErrEmailInvalid struct { Email string @@ -55,6 +60,10 @@ func (err ErrEmailInvalid) Error() string { return fmt.Sprintf("e-mail invalid [email: %s]", err.Email) } +func (err ErrEmailInvalid) Unwrap() error { + return fs.ErrInvalid +} + // ErrEmailAlreadyUsed represents a "EmailAlreadyUsed" kind of error. type ErrEmailAlreadyUsed struct { Email string @@ -70,6 +79,10 @@ func (err ErrEmailAlreadyUsed) Error() string { return fmt.Sprintf("e-mail already in use [email: %s]", err.Email) } +func (err ErrEmailAlreadyUsed) Unwrap() error { + return fs.ErrExist +} + // ErrEmailAddressNotExist email address not exist type ErrEmailAddressNotExist struct { Email string @@ -85,6 +98,10 @@ func (err ErrEmailAddressNotExist) Error() string { return fmt.Sprintf("Email address does not exist [email: %s]", err.Email) } +func (err ErrEmailAddressNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrPrimaryEmailCannotDelete primary email address cannot be deleted type ErrPrimaryEmailCannotDelete struct { Email string @@ -100,6 +117,10 @@ func (err ErrPrimaryEmailCannotDelete) Error() string { return fmt.Sprintf("Primary email address cannot be deleted [email: %s]", err.Email) } +func (err ErrPrimaryEmailCannotDelete) Unwrap() error { + return fs.ErrInvalid +} + // EmailAddress is the list of all email addresses of a user. It also contains the // primary email address which is saved in user table. type EmailAddress struct { diff --git a/models/user/error.go b/models/user/error.go index 25e0d8ea8a428..dc69bde515d29 100644 --- a/models/user/error.go +++ b/models/user/error.go @@ -6,6 +6,7 @@ package user import ( "fmt" + "io/fs" ) // ____ ___ @@ -30,6 +31,11 @@ func (err ErrUserAlreadyExist) Error() string { return fmt.Sprintf("user already exists [name: %s]", err.Name) } +// Unwrap unwraps this error as a ErrExist error +func (err ErrUserAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // ErrUserNotExist represents a "UserNotExist" kind of error. type ErrUserNotExist struct { UID int64 @@ -47,6 +53,11 @@ func (err ErrUserNotExist) Error() string { return fmt.Sprintf("user does not exist [uid: %d, name: %s, keyid: %d]", err.UID, err.Name, err.KeyID) } +// Unwrap unwraps this error as a ErrNotExist error +func (err ErrUserNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrUserProhibitLogin represents a "ErrUserProhibitLogin" kind of error. type ErrUserProhibitLogin struct { UID int64 @@ -63,6 +74,11 @@ func (err ErrUserProhibitLogin) Error() string { return fmt.Sprintf("user is not allowed login [uid: %d, name: %s]", err.UID, err.Name) } +// Unwrap unwraps this error as a ErrPermission error +func (err ErrUserProhibitLogin) Unwrap() error { + return fs.ErrPermission +} + // ErrUserInactive represents a "ErrUserInactive" kind of error. type ErrUserInactive struct { UID int64 @@ -78,3 +94,8 @@ func IsErrUserInactive(err error) bool { func (err ErrUserInactive) Error() string { return fmt.Sprintf("user is inactive [uid: %d, name: %s]", err.UID, err.Name) } + +// Unwrap unwraps this error as a ErrPermission error +func (err ErrUserInactive) Unwrap() error { + return fs.ErrPermission +} diff --git a/models/user/external_login_user.go b/models/user/external_login_user.go index 422823b89c3ff..e25addc4f0cd3 100644 --- a/models/user/external_login_user.go +++ b/models/user/external_login_user.go @@ -7,6 +7,7 @@ package user import ( "context" "fmt" + "io/fs" "time" "code.gitea.io/gitea/models/db" @@ -31,6 +32,10 @@ func (err ErrExternalLoginUserAlreadyExist) Error() string { return fmt.Sprintf("external login user already exists [externalID: %s, userID: %d, loginSourceID: %d]", err.ExternalID, err.UserID, err.LoginSourceID) } +func (err ErrExternalLoginUserAlreadyExist) Unwrap() error { + return fs.ErrExist +} + // ErrExternalLoginUserNotExist represents a "ExternalLoginUserNotExist" kind of error. type ErrExternalLoginUserNotExist struct { UserID int64 @@ -47,6 +52,10 @@ func (err ErrExternalLoginUserNotExist) Error() string { return fmt.Sprintf("external login user link does not exists [userID: %d, loginSourceID: %d]", err.UserID, err.LoginSourceID) } +func (err ErrExternalLoginUserNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ExternalLoginUser makes the connecting between some existing user and additional external login sources type ExternalLoginUser struct { ExternalID string `xorm:"pk NOT NULL"` diff --git a/models/user/openid.go b/models/user/openid.go index 8ef0ce5ed7ee3..e1da5ec266c28 100644 --- a/models/user/openid.go +++ b/models/user/openid.go @@ -8,6 +8,7 @@ import ( "context" "errors" "fmt" + "io/fs" "code.gitea.io/gitea/models/db" ) @@ -65,6 +66,10 @@ func (err ErrOpenIDAlreadyUsed) Error() string { return fmt.Sprintf("OpenID already in use [oid: %s]", err.OpenID) } +func (err ErrOpenIDAlreadyUsed) Unwrap() error { + return fs.ErrExist +} + // AddUserOpenID adds an pre-verified/normalized OpenID URI to given user. // NOTE: make sure openid.URI is normalized already func AddUserOpenID(ctx context.Context, openid *UserOpenID) error { diff --git a/models/user/redirect.go b/models/user/redirect.go index 49370218dbd16..055b815bbd29b 100644 --- a/models/user/redirect.go +++ b/models/user/redirect.go @@ -7,6 +7,7 @@ package user import ( "context" "fmt" + "io/fs" "strings" "code.gitea.io/gitea/models/db" @@ -27,6 +28,10 @@ func (err ErrUserRedirectNotExist) Error() string { return fmt.Sprintf("user redirect does not exist [name: %s]", err.Name) } +func (err ErrUserRedirectNotExist) Unwrap() error { + return fs.ErrNotExist +} + // Redirect represents that a user name should be redirected to another type Redirect struct { ID int64 `xorm:"pk autoincr"` diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index 478a6a29ff236..51a9a9d9bd318 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -8,6 +8,7 @@ package webhook import ( "context" "fmt" + "io/fs" "strings" "code.gitea.io/gitea/models/db" @@ -41,6 +42,10 @@ func (err ErrWebhookNotExist) Error() string { return fmt.Sprintf("webhook does not exist [id: %d]", err.ID) } +func (err ErrWebhookNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrHookTaskNotExist represents a "HookTaskNotExist" kind of error. type ErrHookTaskNotExist struct { HookID int64 @@ -57,6 +62,10 @@ func (err ErrHookTaskNotExist) Error() string { return fmt.Sprintf("hook task does not exist [hook: %d, uuid: %s]", err.HookID, err.UUID) } +func (err ErrHookTaskNotExist) Unwrap() error { + return fs.ErrNotExist +} + // HookContentType is the content type of a web hook type HookContentType int diff --git a/modules/git/error.go b/modules/git/error.go index 387dd724e5852..1f1ec0c748113 100644 --- a/modules/git/error.go +++ b/modules/git/error.go @@ -6,6 +6,7 @@ package git import ( "fmt" + "io/fs" "strings" "time" ) @@ -41,6 +42,10 @@ func (err ErrNotExist) Error() string { return fmt.Sprintf("object does not exist [id: %s, rel_path: %s]", err.ID, err.RelPath) } +func (err ErrNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrBadLink entry.FollowLink error type ErrBadLink struct { Name string @@ -87,6 +92,10 @@ func (err ErrBranchNotExist) Error() string { return fmt.Sprintf("branch does not exist [name: %s]", err.Name) } +func (err ErrBranchNotExist) Unwrap() error { + return fs.ErrNotExist +} + // ErrPushOutOfDate represents an error if merging fails due to unrelated histories type ErrPushOutOfDate struct { StdOut string From 6b1291006f0c47584003f0b6b793a458c4a9cd4b Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 27 Aug 2022 17:30:04 +0100 Subject: [PATCH 2/6] move to use util.ErrNotExist etc. Signed-off-by: Andrew Thornton --- models/admin/task.go | 3 +-- models/asymkey/error.go | 27 ++++++++++--------- models/auth/oauth2.go | 7 +++-- models/auth/source.go | 6 ++--- models/auth/token.go | 5 ++-- models/auth/twofactor.go | 3 +-- models/auth/webauthn.go | 4 +-- models/db/error.go | 5 ++-- models/db/name.go | 11 ++++---- models/error.go | 42 +++++++++++++++--------------- models/foreignreference/error.go | 7 ++--- models/git/lfs.go | 10 +++---- models/issues/comment.go | 4 +-- models/issues/content_history.go | 4 +-- models/issues/dependency.go | 8 +++--- models/issues/issue.go | 3 +-- models/issues/label.go | 7 +++-- models/issues/milestone.go | 4 +-- models/issues/pull.go | 5 ++-- models/issues/reaction.go | 6 ++--- models/issues/review.go | 5 ++-- models/issues/stopwatch.go | 5 ++-- models/organization/org.go | 6 ++--- models/organization/team.go | 6 ++--- models/project/project.go | 5 ++-- models/repo/attachment.go | 4 +-- models/repo/redirect.go | 4 +-- models/repo/release.go | 5 ++-- models/repo/repo.go | 5 ++-- models/repo/repo_unit.go | 4 +-- models/repo/topic.go | 4 +-- models/repo/update.go | 7 +++-- models/repo/upload.go | 3 +-- models/repo/wiki.go | 7 +++-- models/user/email_address.go | 11 ++++---- models/user/error.go | 11 ++++---- models/user/external_login_user.go | 6 ++--- models/user/openid.go | 4 +-- models/user/redirect.go | 4 +-- models/webhook/webhook.go | 5 ++-- modules/git/error.go | 7 ++--- modules/util/error.go | 20 ++++++++++++++ services/repository/fork.go | 3 +-- 43 files changed, 160 insertions(+), 152 deletions(-) create mode 100644 modules/util/error.go diff --git a/models/admin/task.go b/models/admin/task.go index 779c48e0c9948..4fa0f10394e67 100644 --- a/models/admin/task.go +++ b/models/admin/task.go @@ -7,7 +7,6 @@ package admin import ( "context" "fmt" - "io/fs" "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" @@ -169,7 +168,7 @@ func (err ErrTaskDoesNotExist) Error() string { } func (err ErrTaskDoesNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // GetMigratingTask returns the migrating task by repo's id diff --git a/models/asymkey/error.go b/models/asymkey/error.go index 5fe0e7f1e3af6..b733b3a586e4e 100644 --- a/models/asymkey/error.go +++ b/models/asymkey/error.go @@ -6,7 +6,8 @@ package asymkey import ( "fmt" - "io/fs" + + "code.gitea.io/gitea/modules/util" ) // ErrKeyUnableVerify represents a "KeyUnableVerify" kind of error. @@ -40,7 +41,7 @@ func (err ErrKeyNotExist) Error() string { } func (err ErrKeyNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrKeyAlreadyExist represents a "KeyAlreadyExist" kind of error. @@ -62,7 +63,7 @@ func (err ErrKeyAlreadyExist) Error() string { } func (err ErrKeyAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrKeyNameAlreadyUsed represents a "KeyNameAlreadyUsed" kind of error. @@ -82,7 +83,7 @@ func (err ErrKeyNameAlreadyUsed) Error() string { } func (err ErrKeyNameAlreadyUsed) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrGPGNoEmailFound represents a "ErrGPGNoEmailFound" kind of error. @@ -148,7 +149,7 @@ func (err ErrGPGKeyNotExist) Error() string { } func (err ErrGPGKeyNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrGPGKeyImportNotExist represents a "GPGKeyImportNotExist" kind of error. @@ -167,7 +168,7 @@ func (err ErrGPGKeyImportNotExist) Error() string { } func (err ErrGPGKeyImportNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrGPGKeyIDAlreadyUsed represents a "GPGKeyIDAlreadyUsed" kind of error. @@ -186,7 +187,7 @@ func (err ErrGPGKeyIDAlreadyUsed) Error() string { } func (err ErrGPGKeyIDAlreadyUsed) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrGPGKeyAccessDenied represents a "GPGKeyAccessDenied" kind of Error. @@ -208,7 +209,7 @@ func (err ErrGPGKeyAccessDenied) Error() string { } func (err ErrGPGKeyAccessDenied) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } // ErrKeyAccessDenied represents a "KeyAccessDenied" kind of error. @@ -230,7 +231,7 @@ func (err ErrKeyAccessDenied) Error() string { } func (err ErrKeyAccessDenied) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } // ErrDeployKeyNotExist represents a "DeployKeyNotExist" kind of error. @@ -251,7 +252,7 @@ func (err ErrDeployKeyNotExist) Error() string { } func (err ErrDeployKeyNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrDeployKeyAlreadyExist represents a "DeployKeyAlreadyExist" kind of error. @@ -271,7 +272,7 @@ func (err ErrDeployKeyAlreadyExist) Error() string { } func (err ErrDeployKeyAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrDeployKeyNameAlreadyUsed represents a "DeployKeyNameAlreadyUsed" kind of error. @@ -291,7 +292,7 @@ func (err ErrDeployKeyNameAlreadyUsed) Error() string { } func (err ErrDeployKeyNameAlreadyUsed) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrSSHInvalidTokenSignature represents a "ErrSSHInvalidTokenSignature" kind of error. @@ -311,5 +312,5 @@ func (err ErrSSHInvalidTokenSignature) Error() string { } func (err ErrSSHInvalidTokenSignature) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } diff --git a/models/auth/oauth2.go b/models/auth/oauth2.go index 6803844572fe2..9d7485d8e7816 100644 --- a/models/auth/oauth2.go +++ b/models/auth/oauth2.go @@ -10,7 +10,6 @@ import ( "encoding/base32" "encoding/base64" "fmt" - "io/fs" "net/url" "strings" @@ -473,7 +472,7 @@ type ErrOAuthClientIDInvalid struct { ClientID string } -// IsErrOauthClientIDInvalid checks if an error is a ErrReviewNotExist. +// IsErrOauthClientIDInvalid checks if an error is a ErrOAuthClientIDInvalid. func IsErrOauthClientIDInvalid(err error) bool { _, ok := err.(ErrOAuthClientIDInvalid) return ok @@ -486,7 +485,7 @@ func (err ErrOAuthClientIDInvalid) Error() string { // Unwrap unwraps this as a ErrNotExist err func (err ErrOAuthClientIDInvalid) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrOAuthApplicationNotFound will be thrown if id cannot be found @@ -507,7 +506,7 @@ func (err ErrOAuthApplicationNotFound) Error() string { // Unwrap unwraps this as a ErrNotExist err func (err ErrOAuthApplicationNotFound) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // GetActiveOAuth2ProviderSources returns all actived LoginOAuth2 sources diff --git a/models/auth/source.go b/models/auth/source.go index 708bcdf3231b5..88c5c29b780c9 100644 --- a/models/auth/source.go +++ b/models/auth/source.go @@ -7,12 +7,12 @@ package auth import ( "fmt" - "io/fs" "reflect" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "xorm.io/xorm" "xorm.io/xorm/convert" @@ -369,7 +369,7 @@ func (err ErrSourceNotExist) Error() string { // Unwrap unwraps this as a ErrNotExist err func (err ErrSourceNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrSourceAlreadyExist represents a "SourceAlreadyExist" kind of error. @@ -389,7 +389,7 @@ func (err ErrSourceAlreadyExist) Error() string { // Unwrap unwraps this as a ErrExist err func (err ErrSourceAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrSourceInUse represents a "SourceInUse" kind of error. diff --git a/models/auth/token.go b/models/auth/token.go index 32bc713eb78fd..44b7af0a11521 100644 --- a/models/auth/token.go +++ b/models/auth/token.go @@ -8,7 +8,6 @@ package auth import ( "crypto/subtle" "fmt" - "io/fs" "time" "code.gitea.io/gitea/models/db" @@ -37,7 +36,7 @@ func (err ErrAccessTokenNotExist) Error() string { } func (err ErrAccessTokenNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrAccessTokenEmpty represents a "AccessTokenEmpty" kind of error. @@ -54,7 +53,7 @@ func (err ErrAccessTokenEmpty) Error() string { } func (err ErrAccessTokenEmpty) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } var successfulAccessTokenCache *lru.Cache diff --git a/models/auth/twofactor.go b/models/auth/twofactor.go index a487434e5b749..736d4c340c404 100644 --- a/models/auth/twofactor.go +++ b/models/auth/twofactor.go @@ -11,7 +11,6 @@ import ( "encoding/base32" "encoding/base64" "fmt" - "io/fs" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/secret" @@ -44,7 +43,7 @@ func (err ErrTwoFactorNotEnrolled) Error() string { // Unwrap unwraps this as a ErrNotExist err func (err ErrTwoFactorNotEnrolled) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // TwoFactor represents a two-factor authentication token. diff --git a/models/auth/webauthn.go b/models/auth/webauthn.go index d7f38a033e083..1575b6cbab678 100644 --- a/models/auth/webauthn.go +++ b/models/auth/webauthn.go @@ -7,11 +7,11 @@ package auth import ( "context" "fmt" - "io/fs" "strings" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "github.com/duo-labs/webauthn/webauthn" "xorm.io/xorm" @@ -32,7 +32,7 @@ func (err ErrWebAuthnCredentialNotExist) Error() string { // Unwrap unwraps this as a ErrNotExist err func (err ErrWebAuthnCredentialNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // IsErrWebAuthnCredentialNotExist checks if an error is a ErrWebAuthnCredentialNotExist. diff --git a/models/db/error.go b/models/db/error.go index 47c3a33cd44d2..9577fa55dbb38 100644 --- a/models/db/error.go +++ b/models/db/error.go @@ -6,7 +6,8 @@ package db import ( "fmt" - "io/fs" + + "code.gitea.io/gitea/modules/util" ) // ErrCancelled represents an error due to context cancellation @@ -70,5 +71,5 @@ func (err ErrNotExist) Error() string { // Unwrap unwraps this as a ErrNotExist err func (err ErrNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } diff --git a/models/db/name.go b/models/db/name.go index 215d6692791e8..42c77db678c00 100644 --- a/models/db/name.go +++ b/models/db/name.go @@ -6,10 +6,11 @@ package db import ( "fmt" - "io/fs" "regexp" "strings" "unicode/utf8" + + "code.gitea.io/gitea/modules/util" ) var ( @@ -27,7 +28,7 @@ func (err errNameEmpty) Error() string { } func (err errNameEmpty) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // ErrNameReserved represents a "reserved name" error. @@ -47,7 +48,7 @@ func (err ErrNameReserved) Error() string { // Unwrap unwraps this as a ErrInvalid err func (err ErrNameReserved) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // ErrNamePatternNotAllowed represents a "pattern not allowed" error. @@ -67,7 +68,7 @@ func (err ErrNamePatternNotAllowed) Error() string { // Unwrap unwraps this as a ErrInvalid err func (err ErrNamePatternNotAllowed) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // ErrNameCharsNotAllowed represents a "character not allowed in name" error. @@ -87,7 +88,7 @@ func (err ErrNameCharsNotAllowed) Error() string { // Unwrap unwraps this as a ErrInvalid err func (err ErrNameCharsNotAllowed) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // IsUsableName checks if name is reserved or pattern of name is not allowed diff --git a/models/error.go b/models/error.go index 14631461e3d3a..b935946e8094d 100644 --- a/models/error.go +++ b/models/error.go @@ -7,10 +7,10 @@ package models import ( "fmt" - "io/fs" 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. @@ -76,7 +76,7 @@ func IsErrNoPendingTransfer(err error) bool { } func (err ErrNoPendingRepoTransfer) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrRepoTransferInProgress represents the state of a repository that has an @@ -97,7 +97,7 @@ func (err ErrRepoTransferInProgress) Error() string { } func (err ErrRepoTransferInProgress) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrInvalidCloneAddr represents a "InvalidCloneAddr" kind of error. @@ -134,7 +134,7 @@ func (err *ErrInvalidCloneAddr) Error() string { } func (err *ErrInvalidCloneAddr) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // ErrUpdateTaskNotExist represents a "UpdateTaskNotExist" kind of error. @@ -153,7 +153,7 @@ func (err ErrUpdateTaskNotExist) Error() string { } func (err ErrUpdateTaskNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrInvalidTagName represents a "InvalidTagName" kind of error. @@ -172,7 +172,7 @@ func (err ErrInvalidTagName) Error() string { } func (err ErrInvalidTagName) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // ErrProtectedTagName represents a "ProtectedTagName" kind of error. @@ -191,7 +191,7 @@ func (err ErrProtectedTagName) Error() string { } func (err ErrProtectedTagName) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } // ErrRepoFileAlreadyExists represents a "RepoFileAlreadyExist" kind of error. @@ -210,7 +210,7 @@ func (err ErrRepoFileAlreadyExists) Error() string { } func (err ErrRepoFileAlreadyExists) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrRepoFileDoesNotExist represents a "RepoFileDoesNotExist" kind of error. @@ -230,7 +230,7 @@ func (err ErrRepoFileDoesNotExist) Error() string { } func (err ErrRepoFileDoesNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrFilenameInvalid represents a "FilenameInvalid" kind of error. @@ -249,7 +249,7 @@ func (err ErrFilenameInvalid) Error() string { } func (err ErrFilenameInvalid) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // ErrUserCannotCommit represents "UserCannotCommit" kind of error. @@ -268,7 +268,7 @@ func (err ErrUserCannotCommit) Error() string { } func (err ErrUserCannotCommit) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } // ErrFilePathInvalid represents a "FilePathInvalid" kind of error. @@ -293,7 +293,7 @@ func (err ErrFilePathInvalid) Error() string { } func (err ErrFilePathInvalid) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // ErrFilePathProtected represents a "FilePathProtected" kind of error. @@ -316,7 +316,7 @@ func (err ErrFilePathProtected) Error() string { } func (err ErrFilePathProtected) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } // __________ .__ @@ -342,7 +342,7 @@ func (err ErrBranchDoesNotExist) Error() string { } func (err ErrBranchDoesNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrBranchAlreadyExists represents an error that branch with such name already exists. @@ -361,7 +361,7 @@ func (err ErrBranchAlreadyExists) Error() string { } func (err ErrBranchAlreadyExists) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrBranchNameConflict represents an error that branch name conflicts with other branch. @@ -380,7 +380,7 @@ func (err ErrBranchNameConflict) Error() string { } func (err ErrBranchNameConflict) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrBranchesEqual represents an error that branch name conflicts with other branch. @@ -400,7 +400,7 @@ func (err ErrBranchesEqual) Error() string { } func (err ErrBranchesEqual) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // ErrDisallowedToMerge represents an error that a branch is protected and the current user is not allowed to modify it. @@ -419,7 +419,7 @@ func (err ErrDisallowedToMerge) Error() string { } func (err ErrDisallowedToMerge) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } // ErrTagAlreadyExists represents an error that tag with such name already exists. @@ -438,7 +438,7 @@ func (err ErrTagAlreadyExists) Error() string { } func (err ErrTagAlreadyExists) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrSHADoesNotMatch represents a "SHADoesNotMatch" kind of error. @@ -474,7 +474,7 @@ func (err ErrSHANotFound) Error() string { } func (err ErrSHANotFound) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrCommitIDDoesNotMatch represents a "CommitIDDoesNotMatch" kind of error. @@ -524,7 +524,7 @@ func (err ErrInvalidMergeStyle) Error() string { } func (err ErrInvalidMergeStyle) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // ErrMergeConflicts represents an error if merging fails with a conflict diff --git a/models/foreignreference/error.go b/models/foreignreference/error.go index 899855bf2f701..a1db773cd29cb 100644 --- a/models/foreignreference/error.go +++ b/models/foreignreference/error.go @@ -6,7 +6,8 @@ package foreignreference import ( "fmt" - "io/fs" + + "code.gitea.io/gitea/modules/util" ) // ErrLocalIndexNotExist represents a "LocalIndexNotExist" kind of error. @@ -27,7 +28,7 @@ func (err ErrLocalIndexNotExist) Error() string { } func (err ErrLocalIndexNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrForeignIndexNotExist represents a "ForeignIndexNotExist" kind of error. @@ -48,5 +49,5 @@ func (err ErrForeignIndexNotExist) Error() string { } func (err ErrForeignIndexNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } diff --git a/models/git/lfs.go b/models/git/lfs.go index c270bd352bb5b..2e7a2990d87df 100644 --- a/models/git/lfs.go +++ b/models/git/lfs.go @@ -7,7 +7,6 @@ package git import ( "context" "fmt" - "io/fs" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/perm" @@ -17,6 +16,7 @@ import ( "code.gitea.io/gitea/modules/lfs" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "xorm.io/builder" ) @@ -39,7 +39,7 @@ func (err ErrLFSLockNotExist) Error() string { } func (err ErrLFSLockNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrLFSUnauthorizedAction represents a "LFSUnauthorizedAction" kind of error. @@ -63,7 +63,7 @@ func (err ErrLFSUnauthorizedAction) Error() string { } func (err ErrLFSUnauthorizedAction) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } // ErrLFSLockAlreadyExist represents a "LFSLockAlreadyExist" kind of error. @@ -83,7 +83,7 @@ func (err ErrLFSLockAlreadyExist) Error() string { } func (err ErrLFSLockAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrLFSFileLocked represents a "LFSFileLocked" kind of error. @@ -104,7 +104,7 @@ func (err ErrLFSFileLocked) Error() string { } func (err ErrLFSFileLocked) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } // LFSMetaObject stores metadata for LFS tracked files. diff --git a/models/issues/comment.go b/models/issues/comment.go index 788a15a4004ad..9ab6cab7d0812 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -9,7 +9,6 @@ package issues import ( "context" "fmt" - "io/fs" "regexp" "strconv" "strings" @@ -29,6 +28,7 @@ import ( "code.gitea.io/gitea/modules/references" "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "xorm.io/builder" "xorm.io/xorm" @@ -51,7 +51,7 @@ func (err ErrCommentNotExist) Error() string { } func (err ErrCommentNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // CommentType defines whether a comment is just a simple comment, an action (like close) or a reference. diff --git a/models/issues/content_history.go b/models/issues/content_history.go index 1cd99f8b1a11f..f5cfa65b8ff4f 100644 --- a/models/issues/content_history.go +++ b/models/issues/content_history.go @@ -7,12 +7,12 @@ package issues import ( "context" "fmt" - "io/fs" "code.gitea.io/gitea/models/avatars" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "xorm.io/builder" ) @@ -203,7 +203,7 @@ func (err ErrIssueContentHistoryNotExist) Error() string { } func (err ErrIssueContentHistoryNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // GetIssueContentHistoryByID get issue content history diff --git a/models/issues/dependency.go b/models/issues/dependency.go index dbdbb5a64054d..64a3f1d6059e9 100644 --- a/models/issues/dependency.go +++ b/models/issues/dependency.go @@ -7,11 +7,11 @@ package issues import ( "context" "fmt" - "io/fs" "code.gitea.io/gitea/models/db" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" ) // ErrDependencyExists represents a "DependencyAlreadyExists" kind of error. @@ -31,7 +31,7 @@ func (err ErrDependencyExists) Error() string { } func (err ErrDependencyExists) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrDependencyNotExists represents a "DependencyAlreadyExists" kind of error. @@ -51,7 +51,7 @@ func (err ErrDependencyNotExists) Error() string { } func (err ErrDependencyNotExists) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrCircularDependency represents a "DependencyCircular" kind of error. @@ -101,7 +101,7 @@ func (err ErrUnknownDependencyType) Error() string { } func (err ErrUnknownDependencyType) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // IssueDependency represents an issue dependency diff --git a/models/issues/issue.go b/models/issues/issue.go index 71f1376d5b8f4..6706c83b333b6 100644 --- a/models/issues/issue.go +++ b/models/issues/issue.go @@ -8,7 +8,6 @@ package issues import ( "context" "fmt" - "io/fs" "regexp" "sort" "strconv" @@ -54,7 +53,7 @@ func (err ErrIssueNotExist) Error() string { } func (err ErrIssueNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrIssueIsClosed represents a "IssueIsClosed" kind of error. diff --git a/models/issues/label.go b/models/issues/label.go index 0d206ee55df61..be97454e26fd8 100644 --- a/models/issues/label.go +++ b/models/issues/label.go @@ -9,7 +9,6 @@ import ( "context" "fmt" "html/template" - "io/fs" "math" "regexp" "strconv" @@ -40,7 +39,7 @@ func (err ErrRepoLabelNotExist) Error() string { } func (err ErrRepoLabelNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrOrgLabelNotExist represents a "OrgLabelNotExist" kind of error. @@ -60,7 +59,7 @@ func (err ErrOrgLabelNotExist) Error() string { } func (err ErrOrgLabelNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrLabelNotExist represents a "LabelNotExist" kind of error. @@ -79,7 +78,7 @@ func (err ErrLabelNotExist) Error() string { } func (err ErrLabelNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // LabelColorPattern is a regexp witch can validate LabelColor diff --git a/models/issues/milestone.go b/models/issues/milestone.go index ed5e460b15c9f..3ccade7411e5f 100644 --- a/models/issues/milestone.go +++ b/models/issues/milestone.go @@ -7,7 +7,6 @@ package issues import ( "context" "fmt" - "io/fs" "strings" "time" @@ -16,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "xorm.io/builder" ) @@ -41,7 +41,7 @@ func (err ErrMilestoneNotExist) Error() string { } func (err ErrMilestoneNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // Milestone represents a milestone of repository. diff --git a/models/issues/pull.go b/models/issues/pull.go index afe24ba9df89d..cc61a342c43b7 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -9,7 +9,6 @@ import ( "context" "fmt" "io" - "io/fs" "strings" "code.gitea.io/gitea/models/db" @@ -48,7 +47,7 @@ func (err ErrPullRequestNotExist) Error() string { } func (err ErrPullRequestNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrPullRequestAlreadyExists represents a "PullRequestAlreadyExists"-error @@ -74,7 +73,7 @@ func (err ErrPullRequestAlreadyExists) Error() string { } func (err ErrPullRequestAlreadyExists) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrPullRequestHeadRepoMissing represents a "ErrPullRequestHeadRepoMissing" error diff --git a/models/issues/reaction.go b/models/issues/reaction.go index 3a00e2dda7e86..cdfc5b36b7bb6 100644 --- a/models/issues/reaction.go +++ b/models/issues/reaction.go @@ -8,7 +8,6 @@ import ( "bytes" "context" "fmt" - "io/fs" "code.gitea.io/gitea/models/db" repo_model "code.gitea.io/gitea/models/repo" @@ -16,6 +15,7 @@ import ( "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "xorm.io/builder" ) @@ -36,7 +36,7 @@ func (err ErrForbiddenIssueReaction) Error() string { } func (err ErrForbiddenIssueReaction) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } // ErrReactionAlreadyExist is used when a existing reaction was try to created @@ -55,7 +55,7 @@ func (err ErrReactionAlreadyExist) Error() string { } func (err ErrReactionAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // Reaction represents a reactions on issues and comments. diff --git a/models/issues/review.go b/models/issues/review.go index 2136ab26f828f..b2079b27e47bd 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -7,7 +7,6 @@ package issues import ( "context" "fmt" - "io/fs" "strings" "code.gitea.io/gitea/models/db" @@ -41,7 +40,7 @@ func (err ErrReviewNotExist) Error() string { } func (err ErrReviewNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrNotValidReviewRequest an not allowed review request modify @@ -65,7 +64,7 @@ func (err ErrNotValidReviewRequest) Error() string { } func (err ErrNotValidReviewRequest) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // ReviewType defines the sort of feedback a review gives diff --git a/models/issues/stopwatch.go b/models/issues/stopwatch.go index 854a6d889e759..0046727f599ca 100644 --- a/models/issues/stopwatch.go +++ b/models/issues/stopwatch.go @@ -7,7 +7,6 @@ package issues import ( "context" "fmt" - "io/fs" "time" "code.gitea.io/gitea/models/db" @@ -27,7 +26,7 @@ func (err ErrIssueStopwatchNotExist) Error() string { } func (err ErrIssueStopwatchNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrIssueStopwatchAlreadyExist represents an error that stopwatch is already exist @@ -41,7 +40,7 @@ func (err ErrIssueStopwatchAlreadyExist) Error() string { } func (err ErrIssueStopwatchAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // Stopwatch represents a stopwatch for time tracking. diff --git a/models/organization/org.go b/models/organization/org.go index bb5684e5e6639..98eeb96e83f9e 100644 --- a/models/organization/org.go +++ b/models/organization/org.go @@ -8,7 +8,6 @@ package organization import ( "context" "fmt" - "io/fs" "strings" "code.gitea.io/gitea/models/db" @@ -19,6 +18,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/util" "xorm.io/builder" ) @@ -47,7 +47,7 @@ func (err ErrOrgNotExist) Error() string { } func (err ErrOrgNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrLastOrgOwner represents a "LastOrgOwner" kind of error. @@ -79,7 +79,7 @@ func (err ErrUserNotAllowedCreateOrg) Error() string { } func (err ErrUserNotAllowedCreateOrg) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } // Organization represents an organization diff --git a/models/organization/team.go b/models/organization/team.go index db10a03ed1090..3d0a584ef4d23 100644 --- a/models/organization/team.go +++ b/models/organization/team.go @@ -8,7 +8,6 @@ package organization import ( "context" "fmt" - "io/fs" "strings" "code.gitea.io/gitea/models/db" @@ -17,6 +16,7 @@ import ( "code.gitea.io/gitea/models/unit" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/log" + "code.gitea.io/gitea/modules/util" "xorm.io/builder" ) @@ -45,7 +45,7 @@ func (err ErrTeamAlreadyExist) Error() string { } func (err ErrTeamAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrTeamNotExist represents a "TeamNotExist" error @@ -66,7 +66,7 @@ func (err ErrTeamNotExist) Error() string { } func (err ErrTeamNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // OwnerTeamName return the owner team name diff --git a/models/project/project.go b/models/project/project.go index 1447c70fb07cb..af2c8ac2afe04 100644 --- a/models/project/project.go +++ b/models/project/project.go @@ -8,7 +8,6 @@ import ( "context" "errors" "fmt" - "io/fs" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/setting" @@ -57,7 +56,7 @@ func (err ErrProjectNotExist) Error() string { } func (err ErrProjectNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrProjectBoardNotExist represents a "ProjectBoardNotExist" kind of error. @@ -76,7 +75,7 @@ func (err ErrProjectBoardNotExist) Error() string { } func (err ErrProjectBoardNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // Project represents a project board diff --git a/models/repo/attachment.go b/models/repo/attachment.go index d054aeca62270..5d4e11ae72a5b 100644 --- a/models/repo/attachment.go +++ b/models/repo/attachment.go @@ -7,7 +7,6 @@ package repo import ( "context" "fmt" - "io/fs" "net/url" "path" @@ -15,6 +14,7 @@ import ( "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/storage" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" ) // Attachment represent a attachment of issue/comment/release. @@ -85,7 +85,7 @@ func (err ErrAttachmentNotExist) Error() string { } func (err ErrAttachmentNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // GetAttachmentByID returns attachment by given id diff --git a/models/repo/redirect.go b/models/repo/redirect.go index e9aa8b180601b..f28220c2afea2 100644 --- a/models/repo/redirect.go +++ b/models/repo/redirect.go @@ -7,10 +7,10 @@ package repo import ( "context" "fmt" - "io/fs" "strings" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/util" ) // ErrRedirectNotExist represents a "RedirectNotExist" kind of error. @@ -30,7 +30,7 @@ func (err ErrRedirectNotExist) Error() string { } func (err ErrRedirectNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // Redirect represents that a repo name should be redirected to another diff --git a/models/repo/release.go b/models/repo/release.go index e7520045cc73c..473b24db3d880 100644 --- a/models/repo/release.go +++ b/models/repo/release.go @@ -9,7 +9,6 @@ import ( "context" "errors" "fmt" - "io/fs" "sort" "strconv" "strings" @@ -39,7 +38,7 @@ func (err ErrReleaseAlreadyExist) Error() string { } func (err ErrReleaseAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrReleaseNotExist represents a "ReleaseNotExist" kind of error. @@ -59,7 +58,7 @@ func (err ErrReleaseNotExist) Error() string { } func (err ErrReleaseNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // Release represents a release of repository. diff --git a/models/repo/repo.go b/models/repo/repo.go index 880f84ab59864..9c33d4f10dc03 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -8,7 +8,6 @@ import ( "context" "fmt" "html/template" - "io/fs" "net" "net/url" "path/filepath" @@ -45,7 +44,7 @@ func (err ErrUserDoesNotHaveAccessToRepo) Error() string { } func (err ErrUserDoesNotHaveAccessToRepo) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } var ( @@ -650,7 +649,7 @@ func (err ErrRepoNotExist) Error() string { // Unwrap unwraps this error as a ErrNotExist error func (err ErrRepoNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // GetRepositoryByOwnerAndNameCtx returns the repository by given owner name and repo name diff --git a/models/repo/repo_unit.go b/models/repo/repo_unit.go index 3b9161f7c09c2..dd85ca9186fcf 100644 --- a/models/repo/repo_unit.go +++ b/models/repo/repo_unit.go @@ -7,13 +7,13 @@ package repo import ( "context" "fmt" - "io/fs" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/unit" "code.gitea.io/gitea/modules/json" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "xorm.io/xorm" "xorm.io/xorm/convert" @@ -35,7 +35,7 @@ func (err ErrUnitTypeNotExist) Error() string { } func (err ErrUnitTypeNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // RepoUnit describes all units of a repository diff --git a/models/repo/topic.go b/models/repo/topic.go index c89cd700068fd..ea744015900b0 100644 --- a/models/repo/topic.go +++ b/models/repo/topic.go @@ -7,12 +7,12 @@ package repo import ( "context" "fmt" - "io/fs" "regexp" "strings" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" "xorm.io/builder" ) @@ -56,7 +56,7 @@ func (err ErrTopicNotExist) Error() string { } func (err ErrTopicNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ValidateTopic checks a topic by length and match pattern rules diff --git a/models/repo/update.go b/models/repo/update.go index f489bdbf368f4..3cf6cbfde0e32 100644 --- a/models/repo/update.go +++ b/models/repo/update.go @@ -7,7 +7,6 @@ package repo import ( "context" "fmt" - "io/fs" "strings" "time" @@ -65,7 +64,7 @@ func (err ErrReachLimitOfRepo) Error() string { } func (err ErrReachLimitOfRepo) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } // ErrRepoAlreadyExist represents a "RepoAlreadyExist" kind of error. @@ -85,7 +84,7 @@ func (err ErrRepoAlreadyExist) Error() string { } func (err ErrRepoAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrRepoFilesAlreadyExist represents a "RepoFilesAlreadyExist" kind of error. @@ -105,7 +104,7 @@ func (err ErrRepoFilesAlreadyExist) Error() string { } func (err ErrRepoFilesAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // CheckCreateRepository check if could created a repository diff --git a/models/repo/upload.go b/models/repo/upload.go index 007eaefaeded4..e3ce7e458fa60 100644 --- a/models/repo/upload.go +++ b/models/repo/upload.go @@ -8,7 +8,6 @@ package repo import ( "fmt" "io" - "io/fs" "mime/multipart" "os" "path" @@ -38,7 +37,7 @@ func (err ErrUploadNotExist) Error() string { } func (err ErrUploadNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // Upload represent a uploaded file to a repo to be deleted when moved diff --git a/models/repo/wiki.go b/models/repo/wiki.go index 94df0f367f714..f62b3028c5c65 100644 --- a/models/repo/wiki.go +++ b/models/repo/wiki.go @@ -7,7 +7,6 @@ package repo import ( "fmt" - "io/fs" "path/filepath" "strings" @@ -32,7 +31,7 @@ func (err ErrWikiAlreadyExist) Error() string { } func (err ErrWikiAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrWikiReservedName represents a reserved name error. @@ -51,7 +50,7 @@ func (err ErrWikiReservedName) Error() string { } func (err ErrWikiReservedName) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // ErrWikiInvalidFileName represents an invalid wiki file name. @@ -70,7 +69,7 @@ func (err ErrWikiInvalidFileName) Error() string { } func (err ErrWikiInvalidFileName) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // WikiCloneLink returns clone URLs of repository wiki. diff --git a/models/user/email_address.go b/models/user/email_address.go index 576a4b1eb1bb8..4a68dac6cdf02 100644 --- a/models/user/email_address.go +++ b/models/user/email_address.go @@ -9,7 +9,6 @@ import ( "context" "errors" "fmt" - "io/fs" "net/mail" "regexp" "strings" @@ -42,7 +41,7 @@ func (err ErrEmailCharIsNotSupported) Error() string { } func (err ErrEmailCharIsNotSupported) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // ErrEmailInvalid represents an error where the email address does not comply with RFC 5322 @@ -61,7 +60,7 @@ func (err ErrEmailInvalid) Error() string { } func (err ErrEmailInvalid) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // ErrEmailAlreadyUsed represents a "EmailAlreadyUsed" kind of error. @@ -80,7 +79,7 @@ func (err ErrEmailAlreadyUsed) Error() string { } func (err ErrEmailAlreadyUsed) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrEmailAddressNotExist email address not exist @@ -99,7 +98,7 @@ func (err ErrEmailAddressNotExist) Error() string { } func (err ErrEmailAddressNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrPrimaryEmailCannotDelete primary email address cannot be deleted @@ -118,7 +117,7 @@ func (err ErrPrimaryEmailCannotDelete) Error() string { } func (err ErrPrimaryEmailCannotDelete) Unwrap() error { - return fs.ErrInvalid + return util.ErrInvalid } // EmailAddress is the list of all email addresses of a user. It also contains the diff --git a/models/user/error.go b/models/user/error.go index dc69bde515d29..f3b53b5255ab9 100644 --- a/models/user/error.go +++ b/models/user/error.go @@ -6,7 +6,8 @@ package user import ( "fmt" - "io/fs" + + "code.gitea.io/gitea/modules/util" ) // ____ ___ @@ -33,7 +34,7 @@ func (err ErrUserAlreadyExist) Error() string { // Unwrap unwraps this error as a ErrExist error func (err ErrUserAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrUserNotExist represents a "UserNotExist" kind of error. @@ -55,7 +56,7 @@ func (err ErrUserNotExist) Error() string { // Unwrap unwraps this error as a ErrNotExist error func (err ErrUserNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrUserProhibitLogin represents a "ErrUserProhibitLogin" kind of error. @@ -76,7 +77,7 @@ func (err ErrUserProhibitLogin) Error() string { // Unwrap unwraps this error as a ErrPermission error func (err ErrUserProhibitLogin) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } // ErrUserInactive represents a "ErrUserInactive" kind of error. @@ -97,5 +98,5 @@ func (err ErrUserInactive) Error() string { // Unwrap unwraps this error as a ErrPermission error func (err ErrUserInactive) Unwrap() error { - return fs.ErrPermission + return util.ErrPermission } diff --git a/models/user/external_login_user.go b/models/user/external_login_user.go index e25addc4f0cd3..489e3e829f488 100644 --- a/models/user/external_login_user.go +++ b/models/user/external_login_user.go @@ -7,10 +7,10 @@ package user import ( "context" "fmt" - "io/fs" "time" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/util" "xorm.io/builder" ) @@ -33,7 +33,7 @@ func (err ErrExternalLoginUserAlreadyExist) Error() string { } func (err ErrExternalLoginUserAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ErrExternalLoginUserNotExist represents a "ExternalLoginUserNotExist" kind of error. @@ -53,7 +53,7 @@ func (err ErrExternalLoginUserNotExist) Error() string { } func (err ErrExternalLoginUserNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ExternalLoginUser makes the connecting between some existing user and additional external login sources diff --git a/models/user/openid.go b/models/user/openid.go index e1da5ec266c28..5855b95d7dfe3 100644 --- a/models/user/openid.go +++ b/models/user/openid.go @@ -8,9 +8,9 @@ import ( "context" "errors" "fmt" - "io/fs" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/util" ) // ErrOpenIDNotExist openid is not known @@ -67,7 +67,7 @@ func (err ErrOpenIDAlreadyUsed) Error() string { } func (err ErrOpenIDAlreadyUsed) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // AddUserOpenID adds an pre-verified/normalized OpenID URI to given user. diff --git a/models/user/redirect.go b/models/user/redirect.go index 055b815bbd29b..af8d6439ad995 100644 --- a/models/user/redirect.go +++ b/models/user/redirect.go @@ -7,10 +7,10 @@ package user import ( "context" "fmt" - "io/fs" "strings" "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/modules/util" ) // ErrUserRedirectNotExist represents a "UserRedirectNotExist" kind of error. @@ -29,7 +29,7 @@ func (err ErrUserRedirectNotExist) Error() string { } func (err ErrUserRedirectNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // Redirect represents that a user name should be redirected to another diff --git a/models/webhook/webhook.go b/models/webhook/webhook.go index 51a9a9d9bd318..016cbcd712ef9 100644 --- a/models/webhook/webhook.go +++ b/models/webhook/webhook.go @@ -8,7 +8,6 @@ package webhook import ( "context" "fmt" - "io/fs" "strings" "code.gitea.io/gitea/models/db" @@ -43,7 +42,7 @@ func (err ErrWebhookNotExist) Error() string { } func (err ErrWebhookNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrHookTaskNotExist represents a "HookTaskNotExist" kind of error. @@ -63,7 +62,7 @@ func (err ErrHookTaskNotExist) Error() string { } func (err ErrHookTaskNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // HookContentType is the content type of a web hook diff --git a/modules/git/error.go b/modules/git/error.go index 1f1ec0c748113..40c41064141be 100644 --- a/modules/git/error.go +++ b/modules/git/error.go @@ -6,9 +6,10 @@ package git import ( "fmt" - "io/fs" "strings" "time" + + "code.gitea.io/gitea/modules/util" ) // ErrExecTimeout error when exec timed out @@ -43,7 +44,7 @@ func (err ErrNotExist) Error() string { } func (err ErrNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrBadLink entry.FollowLink error @@ -93,7 +94,7 @@ func (err ErrBranchNotExist) Error() string { } func (err ErrBranchNotExist) Unwrap() error { - return fs.ErrNotExist + return util.ErrNotExist } // ErrPushOutOfDate represents an error if merging fails due to unrelated histories diff --git a/modules/util/error.go b/modules/util/error.go new file mode 100644 index 0000000000000..23717be77850b --- /dev/null +++ b/modules/util/error.go @@ -0,0 +1,20 @@ +// Copyright 2022 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package util + +import ( + "io/fs" +) + +// Common Errors forming the base of our error system +// +// Many Errors returned by Gitea can be tested against these errors +// using errors.Is. +var ( + ErrInvalid = fs.ErrInvalid // "invalid argument" + ErrPermission = fs.ErrPermission // "permission denied" + ErrExist = fs.ErrExist // "file already exists" + ErrNotExist = fs.ErrNotExist // "file does not exist" +) diff --git a/services/repository/fork.go b/services/repository/fork.go index 1847f5385ca15..7053a588de0c2 100644 --- a/services/repository/fork.go +++ b/services/repository/fork.go @@ -7,7 +7,6 @@ package repository import ( "context" "fmt" - "io/fs" "strings" "time" @@ -41,7 +40,7 @@ func (err ErrForkAlreadyExist) Error() string { } func (err ErrForkAlreadyExist) Unwrap() error { - return fs.ErrExist + return util.ErrExist } // ForkRepoOptions contains the fork repository options From fde62c6bf4903f788ed14b2c9ba00912caf7638f Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Sat, 3 Sep 2022 10:22:48 +0100 Subject: [PATCH 3/6] add silent wrapper Signed-off-by: Andrew Thornton --- models/db/name.go | 12 +----------- modules/translation/i18n/errors.go | 8 +++++--- modules/util/error.go | 16 ++++++++++++++++ 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/models/db/name.go b/models/db/name.go index 42c77db678c00..8c74b0e88369a 100644 --- a/models/db/name.go +++ b/models/db/name.go @@ -15,22 +15,12 @@ import ( var ( // ErrNameEmpty name is empty error - ErrNameEmpty = errNameEmpty{} + ErrNameEmpty = util.SilentWrap{Message: "name is empty", Err: util.ErrInvalid} // AlphaDashDotPattern characters prohibited in a user name (anything except A-Za-z0-9_.-) AlphaDashDotPattern = regexp.MustCompile(`[^\w-\.]`) ) -type errNameEmpty struct{} - -func (err errNameEmpty) Error() string { - return "name is empty" -} - -func (err errNameEmpty) Unwrap() error { - return util.ErrInvalid -} - // ErrNameReserved represents a "reserved name" error. type ErrNameReserved struct { Name string diff --git a/modules/translation/i18n/errors.go b/modules/translation/i18n/errors.go index b485badd1d2b9..79477f1b79c67 100644 --- a/modules/translation/i18n/errors.go +++ b/modules/translation/i18n/errors.go @@ -4,9 +4,11 @@ package i18n -import "errors" +import ( + "code.gitea.io/gitea/modules/util" +) var ( - ErrLocaleAlreadyExist = errors.New("lang already exists") - ErrUncertainArguments = errors.New("arguments to i18n should not contain uncertain slices") + ErrLocaleAlreadyExist = util.SilentWrap{Message: "lang already exists", Err: util.ErrExist} + ErrUncertainArguments = util.SilentWrap{Message: "arguments to i18n should not contain uncertain slices", Err: util.ErrInvalid} ) diff --git a/modules/util/error.go b/modules/util/error.go index 23717be77850b..fa18e4e7e4b87 100644 --- a/modules/util/error.go +++ b/modules/util/error.go @@ -18,3 +18,19 @@ var ( ErrExist = fs.ErrExist // "file already exists" ErrNotExist = fs.ErrNotExist // "file does not exist" ) + +// SilentWrap provides a simple wrapper for a wrapped error where the wrapped error message plays no part in the error message +type SilentWrap struct { + Message string + Err error +} + +// Error returns the message +func (w SilentWrap) Error() string { + return w.Message +} + +// Unwrap returns the underlying error +func (w SilentWrap) Unwrap() error { + return w.Err +} From 27b6b0a2a8f6b56441d29cb12b609ec7d7e1dbad Mon Sep 17 00:00:00 2001 From: zeripath Date: Tue, 6 Sep 2022 21:45:18 +0100 Subject: [PATCH 4/6] Update modules/util/error.go Co-authored-by: delvh --- modules/util/error.go | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/util/error.go b/modules/util/error.go index fa18e4e7e4b87..0c32485c09cbc 100644 --- a/modules/util/error.go +++ b/modules/util/error.go @@ -20,6 +20,7 @@ var ( ) // SilentWrap provides a simple wrapper for a wrapped error where the wrapped error message plays no part in the error message +// Especially useful for "untyped" errors created with "errors.New(…)" that can be classified as 'invalid argument', 'permission denied', 'exists already', or 'does not exist' type SilentWrap struct { Message string Err error From 770ff60df709aee7869c7eb5bfc2255ac142cb28 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 17 Oct 2022 20:06:14 +0100 Subject: [PATCH 5/6] as per wxiaoguang Signed-off-by: Andrew Thornton --- modules/util/error.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/modules/util/error.go b/modules/util/error.go index 0c32485c09cbc..812103bca0847 100644 --- a/modules/util/error.go +++ b/modules/util/error.go @@ -5,7 +5,7 @@ package util import ( - "io/fs" + "errors" ) // Common Errors forming the base of our error system @@ -13,10 +13,10 @@ import ( // Many Errors returned by Gitea can be tested against these errors // using errors.Is. var ( - ErrInvalid = fs.ErrInvalid // "invalid argument" - ErrPermission = fs.ErrPermission // "permission denied" - ErrExist = fs.ErrExist // "file already exists" - ErrNotExist = fs.ErrNotExist // "file does not exist" + ErrInvalid = errors.New("invalid argument") + ErrPermission = errors.New("permission denied") + ErrExist = errors.New("resource already exists") + ErrNotExist = errors.New("resource does not exist") ) // SilentWrap provides a simple wrapper for a wrapped error where the wrapped error message plays no part in the error message From 19dd77f08c006d0508a644a5dace7329cc2beb72 Mon Sep 17 00:00:00 2001 From: Andrew Thornton Date: Mon, 17 Oct 2022 22:10:19 +0100 Subject: [PATCH 6/6] as per delvh Signed-off-by: Andrew Thornton --- models/asymkey/error.go | 14 +++++++------- models/auth/source.go | 2 +- models/auth/token.go | 2 +- models/db/name.go | 8 ++++---- models/error.go | 30 +++++++++++++++--------------- models/git/lfs.go | 6 +++--- models/issues/dependency.go | 4 ++-- models/issues/pull.go | 2 +- models/issues/reaction.go | 4 ++-- models/issues/review.go | 2 +- models/issues/stopwatch.go | 2 +- models/organization/org.go | 2 +- models/organization/team.go | 2 +- models/repo/release.go | 2 +- models/repo/repo.go | 2 +- models/repo/update.go | 6 +++--- models/repo/wiki.go | 6 +++--- models/user/email_address.go | 8 ++++---- models/user/error.go | 6 +++--- models/user/external_login_user.go | 2 +- models/user/openid.go | 2 +- modules/translation/i18n/errors.go | 4 ++-- modules/util/error.go | 8 ++++---- services/repository/fork.go | 2 +- 24 files changed, 64 insertions(+), 64 deletions(-) diff --git a/models/asymkey/error.go b/models/asymkey/error.go index b733b3a586e4e..3ddeb0498a2d5 100644 --- a/models/asymkey/error.go +++ b/models/asymkey/error.go @@ -63,7 +63,7 @@ func (err ErrKeyAlreadyExist) Error() string { } func (err ErrKeyAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrKeyNameAlreadyUsed represents a "KeyNameAlreadyUsed" kind of error. @@ -83,7 +83,7 @@ func (err ErrKeyNameAlreadyUsed) Error() string { } func (err ErrKeyNameAlreadyUsed) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrGPGNoEmailFound represents a "ErrGPGNoEmailFound" kind of error. @@ -187,7 +187,7 @@ func (err ErrGPGKeyIDAlreadyUsed) Error() string { } func (err ErrGPGKeyIDAlreadyUsed) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrGPGKeyAccessDenied represents a "GPGKeyAccessDenied" kind of Error. @@ -209,7 +209,7 @@ func (err ErrGPGKeyAccessDenied) Error() string { } func (err ErrGPGKeyAccessDenied) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } // ErrKeyAccessDenied represents a "KeyAccessDenied" kind of error. @@ -231,7 +231,7 @@ func (err ErrKeyAccessDenied) Error() string { } func (err ErrKeyAccessDenied) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } // ErrDeployKeyNotExist represents a "DeployKeyNotExist" kind of error. @@ -272,7 +272,7 @@ func (err ErrDeployKeyAlreadyExist) Error() string { } func (err ErrDeployKeyAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrDeployKeyNameAlreadyUsed represents a "DeployKeyNameAlreadyUsed" kind of error. @@ -312,5 +312,5 @@ func (err ErrSSHInvalidTokenSignature) Error() string { } func (err ErrSSHInvalidTokenSignature) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } diff --git a/models/auth/source.go b/models/auth/source.go index 88c5c29b780c9..f8be5398aef67 100644 --- a/models/auth/source.go +++ b/models/auth/source.go @@ -389,7 +389,7 @@ func (err ErrSourceAlreadyExist) Error() string { // Unwrap unwraps this as a ErrExist err func (err ErrSourceAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrSourceInUse represents a "SourceInUse" kind of error. diff --git a/models/auth/token.go b/models/auth/token.go index 44b7af0a11521..3afef832da8a0 100644 --- a/models/auth/token.go +++ b/models/auth/token.go @@ -53,7 +53,7 @@ func (err ErrAccessTokenEmpty) Error() string { } func (err ErrAccessTokenEmpty) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } var successfulAccessTokenCache *lru.Cache diff --git a/models/db/name.go b/models/db/name.go index 8c74b0e88369a..a05d1a789be1a 100644 --- a/models/db/name.go +++ b/models/db/name.go @@ -15,7 +15,7 @@ import ( var ( // ErrNameEmpty name is empty error - ErrNameEmpty = util.SilentWrap{Message: "name is empty", Err: util.ErrInvalid} + ErrNameEmpty = util.SilentWrap{Message: "name is empty", Err: util.ErrInvalidArgument} // AlphaDashDotPattern characters prohibited in a user name (anything except A-Za-z0-9_.-) AlphaDashDotPattern = regexp.MustCompile(`[^\w-\.]`) @@ -38,7 +38,7 @@ func (err ErrNameReserved) Error() string { // Unwrap unwraps this as a ErrInvalid err func (err ErrNameReserved) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // ErrNamePatternNotAllowed represents a "pattern not allowed" error. @@ -58,7 +58,7 @@ func (err ErrNamePatternNotAllowed) Error() string { // Unwrap unwraps this as a ErrInvalid err func (err ErrNamePatternNotAllowed) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // ErrNameCharsNotAllowed represents a "character not allowed in name" error. @@ -78,7 +78,7 @@ func (err ErrNameCharsNotAllowed) Error() string { // Unwrap unwraps this as a ErrInvalid err func (err ErrNameCharsNotAllowed) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // IsUsableName checks if name is reserved or pattern of name is not allowed diff --git a/models/error.go b/models/error.go index b935946e8094d..f4c4bc8f67cb8 100644 --- a/models/error.go +++ b/models/error.go @@ -97,7 +97,7 @@ func (err ErrRepoTransferInProgress) Error() string { } func (err ErrRepoTransferInProgress) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrInvalidCloneAddr represents a "InvalidCloneAddr" kind of error. @@ -134,7 +134,7 @@ func (err *ErrInvalidCloneAddr) Error() string { } func (err *ErrInvalidCloneAddr) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // ErrUpdateTaskNotExist represents a "UpdateTaskNotExist" kind of error. @@ -172,7 +172,7 @@ func (err ErrInvalidTagName) Error() string { } func (err ErrInvalidTagName) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // ErrProtectedTagName represents a "ProtectedTagName" kind of error. @@ -191,7 +191,7 @@ func (err ErrProtectedTagName) Error() string { } func (err ErrProtectedTagName) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } // ErrRepoFileAlreadyExists represents a "RepoFileAlreadyExist" kind of error. @@ -210,7 +210,7 @@ func (err ErrRepoFileAlreadyExists) Error() string { } func (err ErrRepoFileAlreadyExists) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrRepoFileDoesNotExist represents a "RepoFileDoesNotExist" kind of error. @@ -249,7 +249,7 @@ func (err ErrFilenameInvalid) Error() string { } func (err ErrFilenameInvalid) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // ErrUserCannotCommit represents "UserCannotCommit" kind of error. @@ -268,7 +268,7 @@ func (err ErrUserCannotCommit) Error() string { } func (err ErrUserCannotCommit) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } // ErrFilePathInvalid represents a "FilePathInvalid" kind of error. @@ -293,7 +293,7 @@ func (err ErrFilePathInvalid) Error() string { } func (err ErrFilePathInvalid) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // ErrFilePathProtected represents a "FilePathProtected" kind of error. @@ -316,7 +316,7 @@ func (err ErrFilePathProtected) Error() string { } func (err ErrFilePathProtected) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } // __________ .__ @@ -361,7 +361,7 @@ func (err ErrBranchAlreadyExists) Error() string { } func (err ErrBranchAlreadyExists) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrBranchNameConflict represents an error that branch name conflicts with other branch. @@ -380,7 +380,7 @@ func (err ErrBranchNameConflict) Error() string { } func (err ErrBranchNameConflict) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrBranchesEqual represents an error that branch name conflicts with other branch. @@ -400,7 +400,7 @@ func (err ErrBranchesEqual) Error() string { } func (err ErrBranchesEqual) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // ErrDisallowedToMerge represents an error that a branch is protected and the current user is not allowed to modify it. @@ -419,7 +419,7 @@ func (err ErrDisallowedToMerge) Error() string { } func (err ErrDisallowedToMerge) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } // ErrTagAlreadyExists represents an error that tag with such name already exists. @@ -438,7 +438,7 @@ func (err ErrTagAlreadyExists) Error() string { } func (err ErrTagAlreadyExists) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrSHADoesNotMatch represents a "SHADoesNotMatch" kind of error. @@ -524,7 +524,7 @@ func (err ErrInvalidMergeStyle) Error() string { } func (err ErrInvalidMergeStyle) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // ErrMergeConflicts represents an error if merging fails with a conflict diff --git a/models/git/lfs.go b/models/git/lfs.go index 2e7a2990d87df..1dab31d5f98fd 100644 --- a/models/git/lfs.go +++ b/models/git/lfs.go @@ -63,7 +63,7 @@ func (err ErrLFSUnauthorizedAction) Error() string { } func (err ErrLFSUnauthorizedAction) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } // ErrLFSLockAlreadyExist represents a "LFSLockAlreadyExist" kind of error. @@ -83,7 +83,7 @@ func (err ErrLFSLockAlreadyExist) Error() string { } func (err ErrLFSLockAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrLFSFileLocked represents a "LFSFileLocked" kind of error. @@ -104,7 +104,7 @@ func (err ErrLFSFileLocked) Error() string { } func (err ErrLFSFileLocked) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } // LFSMetaObject stores metadata for LFS tracked files. diff --git a/models/issues/dependency.go b/models/issues/dependency.go index 64a3f1d6059e9..4754ed0f5f95e 100644 --- a/models/issues/dependency.go +++ b/models/issues/dependency.go @@ -31,7 +31,7 @@ func (err ErrDependencyExists) Error() string { } func (err ErrDependencyExists) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrDependencyNotExists represents a "DependencyAlreadyExists" kind of error. @@ -101,7 +101,7 @@ func (err ErrUnknownDependencyType) Error() string { } func (err ErrUnknownDependencyType) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // IssueDependency represents an issue dependency diff --git a/models/issues/pull.go b/models/issues/pull.go index 97181ba8e5f67..18b67eb3058b8 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -73,7 +73,7 @@ func (err ErrPullRequestAlreadyExists) Error() string { } func (err ErrPullRequestAlreadyExists) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrPullRequestHeadRepoMissing represents a "ErrPullRequestHeadRepoMissing" error diff --git a/models/issues/reaction.go b/models/issues/reaction.go index 42be7b84fc9a5..02cffad3ba49c 100644 --- a/models/issues/reaction.go +++ b/models/issues/reaction.go @@ -36,7 +36,7 @@ func (err ErrForbiddenIssueReaction) Error() string { } func (err ErrForbiddenIssueReaction) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } // ErrReactionAlreadyExist is used when a existing reaction was try to created @@ -55,7 +55,7 @@ func (err ErrReactionAlreadyExist) Error() string { } func (err ErrReactionAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // Reaction represents a reactions on issues and comments. diff --git a/models/issues/review.go b/models/issues/review.go index b2079b27e47bd..26fcea9eef84e 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -64,7 +64,7 @@ func (err ErrNotValidReviewRequest) Error() string { } func (err ErrNotValidReviewRequest) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // ReviewType defines the sort of feedback a review gives diff --git a/models/issues/stopwatch.go b/models/issues/stopwatch.go index 0046727f599ca..a87fbfafa2d21 100644 --- a/models/issues/stopwatch.go +++ b/models/issues/stopwatch.go @@ -40,7 +40,7 @@ func (err ErrIssueStopwatchAlreadyExist) Error() string { } func (err ErrIssueStopwatchAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // Stopwatch represents a stopwatch for time tracking. diff --git a/models/organization/org.go b/models/organization/org.go index 98eeb96e83f9e..fbbf6d04fac6e 100644 --- a/models/organization/org.go +++ b/models/organization/org.go @@ -79,7 +79,7 @@ func (err ErrUserNotAllowedCreateOrg) Error() string { } func (err ErrUserNotAllowedCreateOrg) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } // Organization represents an organization diff --git a/models/organization/team.go b/models/organization/team.go index 5cdfa8b73a901..83e5bd6fe1e14 100644 --- a/models/organization/team.go +++ b/models/organization/team.go @@ -45,7 +45,7 @@ func (err ErrTeamAlreadyExist) Error() string { } func (err ErrTeamAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrTeamNotExist represents a "TeamNotExist" error diff --git a/models/repo/release.go b/models/repo/release.go index d59459a3af576..2e7bc6d3229b2 100644 --- a/models/repo/release.go +++ b/models/repo/release.go @@ -38,7 +38,7 @@ func (err ErrReleaseAlreadyExist) Error() string { } func (err ErrReleaseAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrReleaseNotExist represents a "ReleaseNotExist" kind of error. diff --git a/models/repo/repo.go b/models/repo/repo.go index 3a8c2a857ce78..ce698baaefb9a 100644 --- a/models/repo/repo.go +++ b/models/repo/repo.go @@ -44,7 +44,7 @@ func (err ErrUserDoesNotHaveAccessToRepo) Error() string { } func (err ErrUserDoesNotHaveAccessToRepo) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } var ( diff --git a/models/repo/update.go b/models/repo/update.go index 3cf6cbfde0e32..64a225d2f596e 100644 --- a/models/repo/update.go +++ b/models/repo/update.go @@ -64,7 +64,7 @@ func (err ErrReachLimitOfRepo) Error() string { } func (err ErrReachLimitOfRepo) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } // ErrRepoAlreadyExist represents a "RepoAlreadyExist" kind of error. @@ -84,7 +84,7 @@ func (err ErrRepoAlreadyExist) Error() string { } func (err ErrRepoAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrRepoFilesAlreadyExist represents a "RepoFilesAlreadyExist" kind of error. @@ -104,7 +104,7 @@ func (err ErrRepoFilesAlreadyExist) Error() string { } func (err ErrRepoFilesAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // CheckCreateRepository check if could created a repository diff --git a/models/repo/wiki.go b/models/repo/wiki.go index f62b3028c5c65..c8886eaa3454e 100644 --- a/models/repo/wiki.go +++ b/models/repo/wiki.go @@ -31,7 +31,7 @@ func (err ErrWikiAlreadyExist) Error() string { } func (err ErrWikiAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrWikiReservedName represents a reserved name error. @@ -50,7 +50,7 @@ func (err ErrWikiReservedName) Error() string { } func (err ErrWikiReservedName) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // ErrWikiInvalidFileName represents an invalid wiki file name. @@ -69,7 +69,7 @@ func (err ErrWikiInvalidFileName) Error() string { } func (err ErrWikiInvalidFileName) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // WikiCloneLink returns clone URLs of repository wiki. diff --git a/models/user/email_address.go b/models/user/email_address.go index 05fdc1802424b..964e7ae08ccfc 100644 --- a/models/user/email_address.go +++ b/models/user/email_address.go @@ -41,7 +41,7 @@ func (err ErrEmailCharIsNotSupported) Error() string { } func (err ErrEmailCharIsNotSupported) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // ErrEmailInvalid represents an error where the email address does not comply with RFC 5322 @@ -61,7 +61,7 @@ func (err ErrEmailInvalid) Error() string { } func (err ErrEmailInvalid) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // ErrEmailAlreadyUsed represents a "EmailAlreadyUsed" kind of error. @@ -80,7 +80,7 @@ func (err ErrEmailAlreadyUsed) Error() string { } func (err ErrEmailAlreadyUsed) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrEmailAddressNotExist email address not exist @@ -118,7 +118,7 @@ func (err ErrPrimaryEmailCannotDelete) Error() string { } func (err ErrPrimaryEmailCannotDelete) Unwrap() error { - return util.ErrInvalid + return util.ErrInvalidArgument } // EmailAddress is the list of all email addresses of a user. It also contains the diff --git a/models/user/error.go b/models/user/error.go index f3b53b5255ab9..3fe4ee6657ecf 100644 --- a/models/user/error.go +++ b/models/user/error.go @@ -34,7 +34,7 @@ func (err ErrUserAlreadyExist) Error() string { // Unwrap unwraps this error as a ErrExist error func (err ErrUserAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrUserNotExist represents a "UserNotExist" kind of error. @@ -77,7 +77,7 @@ func (err ErrUserProhibitLogin) Error() string { // Unwrap unwraps this error as a ErrPermission error func (err ErrUserProhibitLogin) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } // ErrUserInactive represents a "ErrUserInactive" kind of error. @@ -98,5 +98,5 @@ func (err ErrUserInactive) Error() string { // Unwrap unwraps this error as a ErrPermission error func (err ErrUserInactive) Unwrap() error { - return util.ErrPermission + return util.ErrPermissionDenied } diff --git a/models/user/external_login_user.go b/models/user/external_login_user.go index 489e3e829f488..496717c57bcf6 100644 --- a/models/user/external_login_user.go +++ b/models/user/external_login_user.go @@ -33,7 +33,7 @@ func (err ErrExternalLoginUserAlreadyExist) Error() string { } func (err ErrExternalLoginUserAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ErrExternalLoginUserNotExist represents a "ExternalLoginUserNotExist" kind of error. diff --git a/models/user/openid.go b/models/user/openid.go index 5855b95d7dfe3..f8e8a787e6eea 100644 --- a/models/user/openid.go +++ b/models/user/openid.go @@ -67,7 +67,7 @@ func (err ErrOpenIDAlreadyUsed) Error() string { } func (err ErrOpenIDAlreadyUsed) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // AddUserOpenID adds an pre-verified/normalized OpenID URI to given user. diff --git a/modules/translation/i18n/errors.go b/modules/translation/i18n/errors.go index 79477f1b79c67..a81b0bc1acd59 100644 --- a/modules/translation/i18n/errors.go +++ b/modules/translation/i18n/errors.go @@ -9,6 +9,6 @@ import ( ) var ( - ErrLocaleAlreadyExist = util.SilentWrap{Message: "lang already exists", Err: util.ErrExist} - ErrUncertainArguments = util.SilentWrap{Message: "arguments to i18n should not contain uncertain slices", Err: util.ErrInvalid} + ErrLocaleAlreadyExist = util.SilentWrap{Message: "lang already exists", Err: util.ErrAlreadyExist} + ErrUncertainArguments = util.SilentWrap{Message: "arguments to i18n should not contain uncertain slices", Err: util.ErrInvalidArgument} ) diff --git a/modules/util/error.go b/modules/util/error.go index 812103bca0847..08e491dbaf229 100644 --- a/modules/util/error.go +++ b/modules/util/error.go @@ -13,10 +13,10 @@ import ( // Many Errors returned by Gitea can be tested against these errors // using errors.Is. var ( - ErrInvalid = errors.New("invalid argument") - ErrPermission = errors.New("permission denied") - ErrExist = errors.New("resource already exists") - ErrNotExist = errors.New("resource does not exist") + ErrInvalidArgument = errors.New("invalid argument") + ErrPermissionDenied = errors.New("permission denied") + ErrAlreadyExist = errors.New("resource already exists") + ErrNotExist = errors.New("resource does not exist") ) // SilentWrap provides a simple wrapper for a wrapped error where the wrapped error message plays no part in the error message diff --git a/services/repository/fork.go b/services/repository/fork.go index 7053a588de0c2..32a516b79f992 100644 --- a/services/repository/fork.go +++ b/services/repository/fork.go @@ -40,7 +40,7 @@ func (err ErrForkAlreadyExist) Error() string { } func (err ErrForkAlreadyExist) Unwrap() error { - return util.ErrExist + return util.ErrAlreadyExist } // ForkRepoOptions contains the fork repository options