diff options
author | zeripath <art27@cantab.net> | 2022-10-18 06:50:37 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-18 07:50:37 +0200 |
commit | 716fcfcf72be6af854d800f3e2c885b9806577e6 (patch) | |
tree | b89ef04d79b58472c9247a5ec426f1118c74f360 /models/issues | |
parent | 6af1a0c8c004aeb4ef1df1a0c74dcb1e1f874796 (diff) | |
download | gitea-716fcfcf72be6af854d800f3e2c885b9806577e6.tar.gz gitea-716fcfcf72be6af854d800f3e2c885b9806577e6.zip |
Make every not exist error unwrappable to a fs.ErrNotExist (#20891)
A lot of our code is repeatedly testing if individual errors are
specific types of Not Exist errors. This is repetitative and unnecesary.
`Unwrap() error` provides a common way of labelling an error as a
NotExist error and we can/should use this.
This PR has chosen to use the common `io/fs` errors e.g.
`fs.ErrNotExist` for our errors. This is in some ways not completely
correct as these are not filesystem errors but it seems like a
reasonable thing to do and would allow us to simplify a lot of our code
to `errors.Is(err, fs.ErrNotExist)` instead of
`package.IsErr...NotExist(err)`
I am open to suggestions to use a different base error - perhaps
`models/db.ErrNotExist` if that would be felt to be better.
Signed-off-by: Andrew Thornton <art27@cantab.net>
Co-authored-by: delvh <dev.lh@web.de>
Diffstat (limited to 'models/issues')
-rw-r--r-- | models/issues/comment.go | 5 | ||||
-rw-r--r-- | models/issues/content_history.go | 5 | ||||
-rw-r--r-- | models/issues/dependency.go | 13 | ||||
-rw-r--r-- | models/issues/issue.go | 4 | ||||
-rw-r--r-- | models/issues/label.go | 12 | ||||
-rw-r--r-- | models/issues/milestone.go | 5 | ||||
-rw-r--r-- | models/issues/pull.go | 8 | ||||
-rw-r--r-- | models/issues/reaction.go | 9 | ||||
-rw-r--r-- | models/issues/review.go | 8 | ||||
-rw-r--r-- | models/issues/stopwatch.go | 8 | ||||
-rw-r--r-- | models/issues/tracked_time.go | 6 |
11 files changed, 80 insertions, 3 deletions
diff --git a/models/issues/comment.go b/models/issues/comment.go index a71afda9e0..9ab6cab7d0 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -28,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" @@ -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 util.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 3e321784bd..f5cfa65b8f 100644 --- a/models/issues/content_history.go +++ b/models/issues/content_history.go @@ -12,6 +12,7 @@ import ( "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" ) @@ -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 util.ErrNotExist +} + // 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 d664c0758e..4754ed0f5f 100644 --- a/models/issues/dependency.go +++ b/models/issues/dependency.go @@ -11,6 +11,7 @@ import ( "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. @@ -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 util.ErrAlreadyExist +} + // 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 util.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 util.ErrInvalidArgument +} + // 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 786c969522..e56e43bd1a 100644 --- a/models/issues/issue.go +++ b/models/issues/issue.go @@ -52,6 +52,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 util.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 667a608687..be97454e26 100644 --- a/models/issues/label.go +++ b/models/issues/label.go @@ -38,6 +38,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 util.ErrNotExist +} + // ErrOrgLabelNotExist represents a "OrgLabelNotExist" kind of error. type ErrOrgLabelNotExist struct { LabelID int64 @@ -54,6 +58,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 util.ErrNotExist +} + // ErrLabelNotExist represents a "LabelNotExist" kind of error. type ErrLabelNotExist struct { LabelID int64 @@ -69,6 +77,10 @@ func (err ErrLabelNotExist) Error() string { return fmt.Sprintf("label does not exist [label_id: %d]", err.LabelID) } +func (err ErrLabelNotExist) Unwrap() error { + return util.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 1021938b20..3ccade7411 100644 --- a/models/issues/milestone.go +++ b/models/issues/milestone.go @@ -15,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" ) @@ -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 util.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 69259c269f..18b67eb305 100644 --- a/models/issues/pull.go +++ b/models/issues/pull.go @@ -46,6 +46,10 @@ func (err ErrPullRequestNotExist) Error() string { err.ID, err.IssueID, err.HeadRepoID, err.BaseRepoID, err.HeadBranch, err.BaseBranch) } +func (err ErrPullRequestNotExist) Unwrap() error { + return util.ErrNotExist +} + // ErrPullRequestAlreadyExists represents a "PullRequestAlreadyExists"-error type ErrPullRequestAlreadyExists struct { ID int64 @@ -68,6 +72,10 @@ func (err ErrPullRequestAlreadyExists) Error() string { err.ID, err.IssueID, err.HeadRepoID, err.BaseRepoID, err.HeadBranch, err.BaseBranch) } +func (err ErrPullRequestAlreadyExists) Unwrap() error { + return util.ErrAlreadyExist +} + // ErrPullRequestHeadRepoMissing represents a "ErrPullRequestHeadRepoMissing" error type ErrPullRequestHeadRepoMissing struct { ID int64 diff --git a/models/issues/reaction.go b/models/issues/reaction.go index ccda10be2c..02cffad3ba 100644 --- a/models/issues/reaction.go +++ b/models/issues/reaction.go @@ -15,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" ) @@ -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 util.ErrPermissionDenied +} + // 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 util.ErrAlreadyExist +} + // 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 5835900801..26fcea9eef 100644 --- a/models/issues/review.go +++ b/models/issues/review.go @@ -39,6 +39,10 @@ func (err ErrReviewNotExist) Error() string { return fmt.Sprintf("review does not exist [id: %d]", err.ID) } +func (err ErrReviewNotExist) Unwrap() error { + return util.ErrNotExist +} + // ErrNotValidReviewRequest an not allowed review request modify type ErrNotValidReviewRequest struct { Reason string @@ -59,6 +63,10 @@ func (err ErrNotValidReviewRequest) Error() string { err.RepoID) } +func (err ErrNotValidReviewRequest) Unwrap() error { + return util.ErrInvalidArgument +} + // 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 0a7ad41f9c..a87fbfafa2 100644 --- a/models/issues/stopwatch.go +++ b/models/issues/stopwatch.go @@ -25,6 +25,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 util.ErrNotExist +} + // ErrIssueStopwatchAlreadyExist represents an error that stopwatch is already exist type ErrIssueStopwatchAlreadyExist struct { UserID int64 @@ -35,6 +39,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 util.ErrAlreadyExist +} + // 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 9f8767362f..ca21eb5149 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 } |