aboutsummaryrefslogtreecommitdiff
path: root/internal/db
diff options
context:
space:
mode:
authorᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-03-16 01:22:27 +0800
committerᴜɴᴋɴᴡᴏɴ <u@gogs.io>2020-03-16 01:22:27 +0800
commit9e9ca66467116e9079a2639c00e9e623aca23015 (patch)
treedacdef5392608ff7107e4dd498959d4899e13e54 /internal/db
parent82ff0c5852f29daa5f95d965fd50665581e7ea3c (diff)
refactor: unify error handling in routing layer
Diffstat (limited to 'internal/db')
-rw-r--r--internal/db/access.go6
-rw-r--r--internal/db/action.go19
-rw-r--r--internal/db/attachment.go24
-rw-r--r--internal/db/comment.go25
-rw-r--r--internal/db/error.go139
-rw-r--r--internal/db/errors/issue.go15
-rw-r--r--internal/db/errors/org.go21
-rw-r--r--internal/db/errors/repo.go45
-rw-r--r--internal/db/errors/user.go18
-rw-r--r--internal/db/errors/webhook.go34
-rw-r--r--internal/db/issue.go38
-rw-r--r--internal/db/issue_label.go32
-rw-r--r--internal/db/login_source.go16
-rw-r--r--internal/db/milestone.go22
-rw-r--r--internal/db/org_team.go24
-rw-r--r--internal/db/pull.go42
-rw-r--r--internal/db/release.go27
-rw-r--r--internal/db/repo.go55
-rw-r--r--internal/db/repo_branch.go25
-rw-r--r--internal/db/ssh_key.go24
-rw-r--r--internal/db/user.go36
-rw-r--r--internal/db/user_mail.go2
-rw-r--r--internal/db/webhook.go44
23 files changed, 381 insertions, 352 deletions
diff --git a/internal/db/access.go b/internal/db/access.go
index b5c6cd28..63911f8e 100644
--- a/internal/db/access.go
+++ b/internal/db/access.go
@@ -8,8 +8,6 @@ import (
"fmt"
log "unknwon.dev/clog/v2"
-
- "gogs.io/gogs/internal/db/errors"
)
type AccessMode int
@@ -110,8 +108,8 @@ func (u *User) GetRepositoryAccesses() (map[*Repository]AccessMode, error) {
for _, access := range accesses {
repo, err := GetRepositoryByID(access.RepoID)
if err != nil {
- if errors.IsRepoNotExist(err) {
- log.Error("GetRepositoryByID: %v", err)
+ if IsErrRepoNotExist(err) {
+ log.Error("Failed to get repository by ID: %v", err)
continue
}
return nil, err
diff --git a/internal/db/action.go b/internal/db/action.go
index 0b803836..77938f02 100644
--- a/internal/db/action.go
+++ b/internal/db/action.go
@@ -20,7 +20,6 @@ import (
api "github.com/gogs/go-gogs-client"
"gogs.io/gogs/internal/conf"
- "gogs.io/gogs/internal/db/errors"
"gogs.io/gogs/internal/lazyregexp"
"gogs.io/gogs/internal/tool"
)
@@ -256,16 +255,16 @@ func (pc *PushCommits) ToApiPayloadCommits(repoPath, repoURL string) ([]*api.Pay
author, err := GetUserByEmail(commit.AuthorEmail)
if err == nil {
authorUsername = author.Name
- } else if !errors.IsUserNotExist(err) {
- return nil, fmt.Errorf("GetUserByEmail: %v", err)
+ } else if !IsErrUserNotExist(err) {
+ return nil, fmt.Errorf("get user by email: %v", err)
}
committerUsername := ""
committer, err := GetUserByEmail(commit.CommitterEmail)
if err == nil {
committerUsername = committer.Name
- } else if !errors.IsUserNotExist(err) {
- return nil, fmt.Errorf("GetUserByEmail: %v", err)
+ } else if !IsErrUserNotExist(err) {
+ return nil, fmt.Errorf("get user by email: %v", err)
}
nameStatus, err := git.RepoShowNameStatus(repoPath, commit.Sha1)
@@ -304,8 +303,8 @@ func (pcs *PushCommits) AvatarLink(email string) string {
u, err := GetUserByEmail(email)
if err != nil {
pcs.avatars[email] = tool.AvatarLink(email)
- if !errors.IsUserNotExist(err) {
- log.Error("GetUserByEmail: %v", err)
+ if !IsErrUserNotExist(err) {
+ log.Error("get user by email: %v", err)
}
} else {
pcs.avatars[email] = u.RelAvatarLink()
@@ -341,7 +340,7 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit) err
issue, err := GetIssueByRef(ref)
if err != nil {
- if errors.IsIssueNotExist(err) {
+ if IsErrIssueNotExist(err) {
continue
}
return err
@@ -383,7 +382,7 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit) err
issue, err := GetIssueByRef(ref)
if err != nil {
- if errors.IsIssueNotExist(err) {
+ if IsErrIssueNotExist(err) {
continue
}
return err
@@ -423,7 +422,7 @@ func UpdateIssuesCommit(doer *User, repo *Repository, commits []*PushCommit) err
issue, err := GetIssueByRef(ref)
if err != nil {
- if errors.IsIssueNotExist(err) {
+ if IsErrIssueNotExist(err) {
continue
}
return err
diff --git a/internal/db/attachment.go b/internal/db/attachment.go
index 44ac4fe2..75d92746 100644
--- a/internal/db/attachment.go
+++ b/internal/db/attachment.go
@@ -16,6 +16,7 @@ import (
"xorm.io/xorm"
"gogs.io/gogs/internal/conf"
+ "gogs.io/gogs/internal/errutil"
)
// Attachment represent a attachment of issue/comment/release.
@@ -83,13 +84,32 @@ func NewAttachment(name string, buf []byte, file multipart.File) (_ *Attachment,
return attach, nil
}
+var _ errutil.NotFound = (*ErrAttachmentNotExist)(nil)
+
+type ErrAttachmentNotExist struct {
+ args map[string]interface{}
+}
+
+func IsErrAttachmentNotExist(err error) bool {
+ _, ok := err.(ErrAttachmentNotExist)
+ return ok
+}
+
+func (err ErrAttachmentNotExist) Error() string {
+ return fmt.Sprintf("attachment does not exist: %v", err.args)
+}
+
+func (ErrAttachmentNotExist) NotFound() bool {
+ return true
+}
+
func getAttachmentByUUID(e Engine, uuid string) (*Attachment, error) {
attach := &Attachment{UUID: uuid}
- has, err := x.Get(attach)
+ has, err := e.Get(attach)
if err != nil {
return nil, err
} else if !has {
- return nil, ErrAttachmentNotExist{0, uuid}
+ return nil, ErrAttachmentNotExist{args: map[string]interface{}{"uuid": uuid}}
}
return attach, nil
}
diff --git a/internal/db/comment.go b/internal/db/comment.go
index d0270302..03f95eea 100644
--- a/internal/db/comment.go
+++ b/internal/db/comment.go
@@ -15,7 +15,7 @@ import (
api "github.com/gogs/go-gogs-client"
- "gogs.io/gogs/internal/db/errors"
+ "gogs.io/gogs/internal/errutil"
"gogs.io/gogs/internal/markup"
)
@@ -96,7 +96,7 @@ func (c *Comment) loadAttributes(e Engine) (err error) {
if c.Poster == nil {
c.Poster, err = GetUserByID(c.PosterID)
if err != nil {
- if errors.IsUserNotExist(err) {
+ if IsErrUserNotExist(err) {
c.PosterID = -1
c.Poster = NewGhostUser()
} else {
@@ -391,6 +391,25 @@ func CreateRefComment(doer *User, repo *Repository, issue *Issue, content, commi
return err
}
+var _ errutil.NotFound = (*ErrCommentNotExist)(nil)
+
+type ErrCommentNotExist struct {
+ args map[string]interface{}
+}
+
+func IsErrCommentNotExist(err error) bool {
+ _, ok := err.(ErrCommentNotExist)
+ return ok
+}
+
+func (err ErrCommentNotExist) Error() string {
+ return fmt.Sprintf("comment does not exist: %v", err.args)
+}
+
+func (ErrCommentNotExist) NotFound() bool {
+ return true
+}
+
// GetCommentByID returns the comment by given ID.
func GetCommentByID(id int64) (*Comment, error) {
c := new(Comment)
@@ -398,7 +417,7 @@ func GetCommentByID(id int64) (*Comment, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, ErrCommentNotExist{id, 0}
+ return nil, ErrCommentNotExist{args: map[string]interface{}{"commentID": id}}
}
return c, c.LoadAttributes()
}
diff --git a/internal/db/error.go b/internal/db/error.go
index 033d631b..dff234d9 100644
--- a/internal/db/error.go
+++ b/internal/db/error.go
@@ -190,21 +190,6 @@ func (err ErrKeyAccessDenied) Error() string {
err.UserID, err.KeyID, err.Note)
}
-type ErrDeployKeyNotExist struct {
- ID int64
- KeyID int64
- RepoID int64
-}
-
-func IsErrDeployKeyNotExist(err error) bool {
- _, ok := err.(ErrDeployKeyNotExist)
- return ok
-}
-
-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)
-}
-
type ErrDeployKeyAlreadyExist struct {
KeyID int64
RepoID int64
@@ -348,20 +333,6 @@ func (err ErrReleaseAlreadyExist) Error() string {
return fmt.Sprintf("release tag already exist [tag_name: %s]", err.TagName)
}
-type ErrReleaseNotExist struct {
- ID int64
- TagName string
-}
-
-func IsErrReleaseNotExist(err error) bool {
- _, ok := err.(ErrReleaseNotExist)
- return ok
-}
-
-func (err ErrReleaseNotExist) Error() string {
- return fmt.Sprintf("release tag does not exist [id: %d, tag_name: %s]", err.ID, err.TagName)
-}
-
type ErrInvalidTagName struct {
TagName string
}
@@ -388,116 +359,6 @@ func (err ErrRepoFileAlreadyExist) Error() string {
return fmt.Sprintf("repository file already exists [file_name: %s]", err.FileName)
}
-// __________ .__ .__ __________ __
-// \______ \__ __| | | |\______ \ ____ ________ __ ____ _______/ |_
-// | ___/ | \ | | | | _// __ \/ ____/ | \_/ __ \ / ___/\ __\
-// | | | | / |_| |_| | \ ___< <_| | | /\ ___/ \___ \ | |
-// |____| |____/|____/____/____|_ /\___ >__ |____/ \___ >____ > |__|
-// \/ \/ |__| \/ \/
-
-type ErrPullRequestNotExist struct {
- ID int64
- IssueID int64
- HeadRepoID int64
- BaseRepoID int64
- HeadBarcnh string
- BaseBranch string
-}
-
-func IsErrPullRequestNotExist(err error) bool {
- _, ok := err.(ErrPullRequestNotExist)
- return ok
-}
-
-func (err ErrPullRequestNotExist) Error() string {
- return fmt.Sprintf("pull request does not exist [id: %d, issue_id: %d, head_repo_id: %d, base_repo_id: %d, head_branch: %s, base_branch: %s]",
- err.ID, err.IssueID, err.HeadRepoID, err.BaseRepoID, err.HeadBarcnh, err.BaseBranch)
-}
-
-// _________ __
-// \_ ___ \ ____ _____ _____ ____ _____/ |_
-// / \ \/ / _ \ / \ / \_/ __ \ / \ __\
-// \ \___( <_> ) Y Y \ Y Y \ ___/| | \ |
-// \______ /\____/|__|_| /__|_| /\___ >___| /__|
-// \/ \/ \/ \/ \/
-
-type ErrCommentNotExist struct {
- ID int64
- IssueID int64
-}
-
-func IsErrCommentNotExist(err error) bool {
- _, ok := err.(ErrCommentNotExist)
- return ok
-}
-
-func (err ErrCommentNotExist) Error() string {
- return fmt.Sprintf("comment does not exist [id: %d, issue_id: %d]", err.ID, err.IssueID)
-}
-
-// .____ ___. .__
-// | | _____ \_ |__ ____ | |
-// | | \__ \ | __ \_/ __ \| |
-// | |___ / __ \| \_\ \ ___/| |__
-// |_______ (____ /___ /\___ >____/
-// \/ \/ \/ \/
-
-type ErrLabelNotExist struct {
- LabelID int64
- RepoID int64
-}
-
-func IsErrLabelNotExist(err error) bool {
- _, ok := err.(ErrLabelNotExist)
- return ok
-}
-
-func (err ErrLabelNotExist) Error() string {
- return fmt.Sprintf("label does not exist [label_id: %d, repo_id: %d]", err.LabelID, err.RepoID)
-}
-
-// _____ .__.__ __
-// / \ |__| | ____ _______/ |_ ____ ____ ____
-// / \ / \| | | _/ __ \ / ___/\ __\/ _ \ / \_/ __ \
-// / Y \ | |_\ ___/ \___ \ | | ( <_> ) | \ ___/
-// \____|__ /__|____/\___ >____ > |__| \____/|___| /\___ >
-// \/ \/ \/ \/ \/
-
-type ErrMilestoneNotExist struct {
- ID int64
- RepoID int64
-}
-
-func IsErrMilestoneNotExist(err error) bool {
- _, ok := err.(ErrMilestoneNotExist)
- return ok
-}
-
-func (err ErrMilestoneNotExist) Error() string {
- return fmt.Sprintf("milestone does not exist [id: %d, repo_id: %d]", err.ID, err.RepoID)
-}
-
-// _____ __ __ .__ __
-// / _ \_/ |__/ |______ ____ | |__ _____ ____ _____/ |_
-// / /_\ \ __\ __\__ \ _/ ___\| | \ / \_/ __ \ / \ __\
-// / | \ | | | / __ \\ \___| Y \ Y Y \ ___/| | \ |
-// \____|__ /__| |__| (____ /\___ >___| /__|_| /\___ >___| /__|
-// \/ \/ \/ \/ \/ \/ \/
-
-type ErrAttachmentNotExist struct {
- ID int64
- UUID string
-}
-
-func IsErrAttachmentNotExist(err error) bool {
- _, ok := err.(ErrAttachmentNotExist)
- return ok
-}
-
-func (err ErrAttachmentNotExist) Error() string {
- return fmt.Sprintf("attachment does not exist [id: %d, uuid: %s]", err.ID, err.UUID)
-}
-
// .____ .__ _________
// | | ____ ____ |__| ____ / _____/ ____ __ _________ ____ ____
// | | / _ \ / ___\| |/ \ \_____ \ / _ \| | \_ __ \_/ ___\/ __ \
diff --git a/internal/db/errors/issue.go b/internal/db/errors/issue.go
index 903cc977..4ea898fb 100644
--- a/internal/db/errors/issue.go
+++ b/internal/db/errors/issue.go
@@ -6,21 +6,6 @@ package errors
import "fmt"
-type IssueNotExist struct {
- ID int64
- RepoID int64
- Index int64
-}
-
-func IsIssueNotExist(err error) bool {
- _, ok := err.(IssueNotExist)
- return ok
-}
-
-func (err IssueNotExist) Error() string {
- return fmt.Sprintf("issue does not exist [id: %d, repo_id: %d, index: %d]", err.ID, err.RepoID, err.Index)
-}
-
type InvalidIssueReference struct {
Ref string
}
diff --git a/internal/db/errors/org.go b/internal/db/errors/org.go
deleted file mode 100644
index 56532746..00000000
--- a/internal/db/errors/org.go
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright 2018 The Gogs 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 errors
-
-import "fmt"
-
-type TeamNotExist struct {
- TeamID int64
- Name string
-}
-
-func IsTeamNotExist(err error) bool {
- _, ok := err.(TeamNotExist)
- return ok
-}
-
-func (err TeamNotExist) Error() string {
- return fmt.Sprintf("team does not exist [team_id: %d, name: %s]", err.TeamID, err.Name)
-}
diff --git a/internal/db/errors/repo.go b/internal/db/errors/repo.go
index c9894af9..ba6c502c 100644
--- a/internal/db/errors/repo.go
+++ b/internal/db/errors/repo.go
@@ -4,35 +4,9 @@
package errors
-import "fmt"
-
-type RepoNotExist struct {
- ID int64
- UserID int64
- Name string
-}
-
-func IsRepoNotExist(err error) bool {
- _, ok := err.(RepoNotExist)
- return ok
-}
-
-func (err RepoNotExist) Error() string {
- return fmt.Sprintf("repository does not exist [id: %d, user_id: %d, name: %s]", err.ID, err.UserID, err.Name)
-}
-
-type ReachLimitOfRepo struct {
- Limit int
-}
-
-func IsReachLimitOfRepo(err error) bool {
- _, ok := err.(ReachLimitOfRepo)
- return ok
-}
-
-func (err ReachLimitOfRepo) Error() string {
- return fmt.Sprintf("user has reached maximum limit of repositories [limit: %d]", err.Limit)
-}
+import (
+ "fmt"
+)
type InvalidRepoReference struct {
Ref string
@@ -72,16 +46,3 @@ func IsBranchAlreadyExists(err error) bool {
func (err BranchAlreadyExists) Error() string {
return fmt.Sprintf("branch already exists [name: %s]", err.Name)
}
-
-type ErrBranchNotExist struct {
- Name string
-}
-
-func IsErrBranchNotExist(err error) bool {
- _, ok := err.(ErrBranchNotExist)
- return ok
-}
-
-func (err ErrBranchNotExist) Error() string {
- return fmt.Sprintf("branch does not exist [name: %s]", err.Name)
-}
diff --git a/internal/db/errors/user.go b/internal/db/errors/user.go
index 526d4b2d..54a6ad50 100644
--- a/internal/db/errors/user.go
+++ b/internal/db/errors/user.go
@@ -4,7 +4,9 @@
package errors
-import "fmt"
+import (
+ "fmt"
+)
type EmptyName struct{}
@@ -17,20 +19,6 @@ func (err EmptyName) Error() string {
return "empty name"
}
-type UserNotExist struct {
- UserID int64
- Name string
-}
-
-func IsUserNotExist(err error) bool {
- _, ok := err.(UserNotExist)
- return ok
-}
-
-func (err UserNotExist) Error() string {
- return fmt.Sprintf("user does not exist [user_id: %d, name: %s]", err.UserID, err.Name)
-}
-
type UserNotKeyOwner struct {
KeyID int64
}
diff --git a/internal/db/errors/webhook.go b/internal/db/errors/webhook.go
deleted file mode 100644
index 76cf8cb4..00000000
--- a/internal/db/errors/webhook.go
+++ /dev/null
@@ -1,34 +0,0 @@
-// Copyright 2017 The Gogs 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 errors
-
-import "fmt"
-
-type WebhookNotExist struct {
- ID int64
-}
-
-func IsWebhookNotExist(err error) bool {
- _, ok := err.(WebhookNotExist)
- return ok
-}
-
-func (err WebhookNotExist) Error() string {
- return fmt.Sprintf("webhook does not exist [id: %d]", err.ID)
-}
-
-type HookTaskNotExist struct {
- HookID int64
- UUID string
-}
-
-func IsHookTaskNotExist(err error) bool {
- _, ok := err.(HookTaskNotExist)
- return ok
-}
-
-func (err HookTaskNotExist) Error() string {
- return fmt.Sprintf("hook task does not exist [hook_id: %d, uuid: %s]", err.HookID, err.UUID)
-}
diff --git a/internal/db/issue.go b/internal/db/issue.go
index 93915244..377e77b4 100644
--- a/internal/db/issue.go
+++ b/internal/db/issue.go
@@ -17,6 +17,7 @@ import (
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/db/errors"
+ "gogs.io/gogs/internal/errutil"
"gogs.io/gogs/internal/tool"
)
@@ -90,7 +91,7 @@ func (issue *Issue) loadAttributes(e Engine) (err error) {
if issue.Poster == nil {
issue.Poster, err = getUserByID(e, issue.PosterID)
if err != nil {
- if errors.IsUserNotExist(err) {
+ if IsErrUserNotExist(err) {
issue.PosterID = -1
issue.Poster = NewGhostUser()
} else {
@@ -395,7 +396,7 @@ func (issue *Issue) GetAssignee() (err error) {
}
issue.Assignee, err = GetUserByID(issue.AssigneeID)
- if errors.IsUserNotExist(err) {
+ if IsErrUserNotExist(err) {
return nil
}
return err
@@ -600,8 +601,8 @@ func (issue *Issue) ChangeAssignee(doer *User, assigneeID int64) (err error) {
}
issue.Assignee, err = GetUserByID(issue.AssigneeID)
- if err != nil && !errors.IsUserNotExist(err) {
- log.Error("GetUserByID [assignee_id: %v]: %v", issue.AssigneeID, err)
+ if err != nil && !IsErrUserNotExist(err) {
+ log.Error("Failed to get user by ID: %v", err)
return nil
}
@@ -673,8 +674,8 @@ func newIssue(e *xorm.Session, opts NewIssueOptions) (err error) {
if opts.Issue.AssigneeID > 0 {
assignee, err := getUserByID(e, opts.Issue.AssigneeID)
- if err != nil && !errors.IsUserNotExist(err) {
- return fmt.Errorf("getUserByID: %v", err)
+ if err != nil && !IsErrUserNotExist(err) {
+ return fmt.Errorf("get user by ID: %v", err)
}
// Assume assignee is invalid and drop silently.
@@ -796,6 +797,25 @@ func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string)
return nil
}
+var _ errutil.NotFound = (*ErrIssueNotExist)(nil)
+
+type ErrIssueNotExist struct {
+ args map[string]interface{}
+}
+
+func IsErrIssueNotExist(err error) bool {
+ _, ok := err.(ErrIssueNotExist)
+ return ok
+}
+
+func (err ErrIssueNotExist) Error() string {
+ return fmt.Sprintf("issue does not exist: %v", err.args)
+}
+
+func (ErrIssueNotExist) NotFound() bool {
+ return true
+}
+
// GetIssueByRef returns an Issue specified by a GFM reference.
// See https://help.github.com/articles/writing-on-github#references for more information on the syntax.
func GetIssueByRef(ref string) (*Issue, error) {
@@ -806,7 +826,7 @@ func GetIssueByRef(ref string) (*Issue, error) {
index := com.StrTo(ref[n+1:]).MustInt64()
if index == 0 {
- return nil, errors.IssueNotExist{}
+ return nil, ErrIssueNotExist{args: map[string]interface{}{"ref": ref}}
}
repo, err := GetRepositoryByRef(ref[:n])
@@ -832,7 +852,7 @@ func GetRawIssueByIndex(repoID, index int64) (*Issue, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, errors.IssueNotExist{RepoID: repoID, Index: index}
+ return nil, ErrIssueNotExist{args: map[string]interface{}{"repoID": repoID, "index": index}}
}
return issue, nil
}
@@ -852,7 +872,7 @@ func getRawIssueByID(e Engine, id int64) (*Issue, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, errors.IssueNotExist{ID: id}
+ return nil, ErrIssueNotExist{args: map[string]interface{}{"issueID": id}}
}
return issue, nil
}
diff --git a/internal/db/issue_label.go b/internal/db/issue_label.go
index b7a6029a..0eaa9b96 100644
--- a/internal/db/issue_label.go
+++ b/internal/db/issue_label.go
@@ -14,6 +14,7 @@ import (
api "github.com/gogs/go-gogs-client"
+ "gogs.io/gogs/internal/errutil"
"gogs.io/gogs/internal/lazyregexp"
"gogs.io/gogs/internal/tool"
)
@@ -103,23 +104,42 @@ func NewLabels(labels ...*Label) error {
return err
}
+var _ errutil.NotFound = (*ErrLabelNotExist)(nil)
+
+type ErrLabelNotExist struct {
+ args map[string]interface{}
+}
+
+func IsErrLabelNotExist(err error) bool {
+ _, ok := err.(ErrLabelNotExist)
+ return ok
+}
+
+func (err ErrLabelNotExist) Error() string {
+ return fmt.Sprintf("label does not exist: %v", err.args)
+}
+
+func (ErrLabelNotExist) NotFound() bool {
+ return true
+}
+
// getLabelOfRepoByName returns a label by Name in given repository.
// If pass repoID as 0, then ORM will ignore limitation of repository
// and can return arbitrary label with any valid ID.
func getLabelOfRepoByName(e Engine, repoID int64, labelName string) (*Label, error) {
if len(labelName) <= 0 {
- return nil, ErrLabelNotExist{0, repoID}
+ return nil, ErrLabelNotExist{args: map[string]interface{}{"repoID": repoID}}
}
l := &Label{
Name: labelName,
RepoID: repoID,
}
- has, err := x.Get(l)
+ has, err := e.Get(l)
if err != nil {
return nil, err
} else if !has {
- return nil, ErrLabelNotExist{0, l.RepoID}
+ return nil, ErrLabelNotExist{args: map[string]interface{}{"repoID": repoID}}
}
return l, nil
}
@@ -129,18 +149,18 @@ func getLabelOfRepoByName(e Engine, repoID int64, labelName string) (*Label, err
// and can return arbitrary label with any valid ID.
func getLabelOfRepoByID(e Engine, repoID, labelID int64) (*Label, error) {
if labelID <= 0 {
- return nil, ErrLabelNotExist{labelID, repoID}
+ return nil, ErrLabelNotExist{args: map[string]interface{}{"repoID": repoID, "labelID": labelID}}
}
l := &Label{
ID: labelID,
RepoID: repoID,
}
- has, err := x.Get(l)
+ has, err := e.Get(l)
if err != nil {
return nil, err
} else if !has {
- return nil, ErrLabelNotExist{l.ID, l.RepoID}
+ return nil, ErrLabelNotExist{args: map[string]interface{}{"repoID": repoID, "labelID": labelID}}
}
return l, nil
}
diff --git a/internal/db/login_source.go b/internal/db/login_source.go
index ee59cdbd..80d65f6e 100644
--- a/internal/db/login_source.go
+++ b/internal/db/login_source.go
@@ -556,7 +556,7 @@ func LoginViaLDAP(user *User, login, password string, source *LoginSource, autoR
username, fn, sn, mail, isAdmin, succeed := source.Cfg.(*LDAPConfig).SearchEntry(login, password, source.Type == LOGIN_DLDAP)
if !succeed {
// User not in LDAP, do nothing
- return nil, errors.UserNotExist{Name: login}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"login": login}}
}
if !autoRegister {
@@ -674,9 +674,9 @@ func LoginViaSMTP(user *User, login, password string, sourceID int64, cfg *SMTPC
if len(cfg.AllowedDomains) > 0 {
idx := strings.Index(login, "@")
if idx == -1 {
- return nil, errors.UserNotExist{Name: login}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"login": login}}
} else if !com.IsSliceContainsStr(strings.Split(cfg.AllowedDomains, ","), login[idx+1:]) {
- return nil, errors.UserNotExist{Name: login}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"login": login}}
}
}
@@ -695,7 +695,7 @@ func LoginViaSMTP(user *User, login, password string, sourceID int64, cfg *SMTPC
tperr, ok := err.(*textproto.Error)
if (ok && tperr.Code == 535) ||
strings.Contains(err.Error(), "Username and Password not accepted") {
- return nil, errors.UserNotExist{Name: login}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"login": login}}
}
return nil, err
}
@@ -735,7 +735,7 @@ func LoginViaSMTP(user *User, login, password string, sourceID int64, cfg *SMTPC
func LoginViaPAM(user *User, login, password string, sourceID int64, cfg *PAMConfig, autoRegister bool) (*User, error) {
if err := pam.PAMAuth(cfg.ServiceName, login, password); err != nil {
if strings.Contains(err.Error(), "Authentication failure") {
- return nil, errors.UserNotExist{Name: login}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"login": login}}
}
return nil, err
}
@@ -768,7 +768,7 @@ func LoginViaGitHub(user *User, login, password string, sourceID int64, cfg *Git
fullname, email, url, location, err := github.Authenticate(cfg.APIEndpoint, login, password)
if err != nil {
if strings.Contains(err.Error(), "401") {
- return nil, errors.UserNotExist{Name: login}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"login": login}}
}
return nil, err
}
@@ -840,7 +840,7 @@ func UserLogin(username, password string, loginSourceID int64) (*User, error) {
return user, nil
}
- return nil, errors.UserNotExist{UserID: user.ID, Name: user.Name}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"userID": user.ID, "name": user.Name}}
}
// Remote login to the login source the user is associated with
@@ -854,7 +854,7 @@ func UserLogin(username, password string, loginSourceID int64) (*User, error) {
// Non-local login source is always greater than 0
if loginSourceID <= 0 {
- return nil, errors.UserNotExist{UserID: -1, Name: username}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"name": username}}
}
source, err := GetLoginSourceByID(loginSourceID)
diff --git a/internal/db/milestone.go b/internal/db/milestone.go
index 77f52f92..df059b30 100644
--- a/internal/db/milestone.go
+++ b/internal/db/milestone.go
@@ -14,6 +14,7 @@ import (
api "github.com/gogs/go-gogs-client"
"gogs.io/gogs/internal/conf"
+ "gogs.io/gogs/internal/errutil"
)
// Milestone represents a milestone of repository.
@@ -130,6 +131,25 @@ func NewMilestone(m *Milestone) (err error) {
return sess.Commit()
}
+var _ errutil.NotFound = (*ErrMilestoneNotExist)(nil)
+
+type ErrMilestoneNotExist struct {
+ args map[string]interface{}
+}
+
+func IsErrMilestoneNotExist(err error) bool {
+ _, ok := err.(ErrMilestoneNotExist)
+ return ok
+}
+
+func (err ErrMilestoneNotExist) Error() string {
+ return fmt.Sprintf("milestone does not exist: %v", err.args)
+}
+
+func (ErrMilestoneNotExist) NotFound() bool {
+ return true
+}
+
func getMilestoneByRepoID(e Engine, repoID, id int64) (*Milestone, error) {
m := &Milestone{
ID: id,
@@ -139,7 +159,7 @@ func getMilestoneByRepoID(e Engine, repoID, id int64) (*Milestone, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, ErrMilestoneNotExist{id, repoID}
+ return nil, ErrMilestoneNotExist{args: map[string]interface{}{"repoID": repoID, "milestoneID": id}}
}
return m, nil
}
diff --git a/internal/db/org_team.go b/internal/db/org_team.go
index accbfe5b..418d4f6b 100644
--- a/internal/db/org_team.go
+++ b/internal/db/org_team.go
@@ -11,6 +11,7 @@ import (
"xorm.io/xorm"
"gogs.io/gogs/internal/db/errors"
+ "gogs.io/gogs/internal/errutil"
)
const OWNER_TEAM = "Owners"
@@ -266,6 +267,25 @@ func NewTeam(t *Team) error {
return sess.Commit()
}
+var _ errutil.NotFound = (*ErrTeamNotExist)(nil)
+
+type ErrTeamNotExist struct {
+ args map[string]interface{}
+}
+
+func IsErrTeamNotExist(err error) bool {
+ _, ok := err.(ErrTeamNotExist)
+ return ok
+}
+
+func (err ErrTeamNotExist) Error() string {
+ return fmt.Sprintf("team does not exist: %v", err.args)
+}
+
+func (ErrTeamNotExist) NotFound() bool {
+ return true
+}
+
func getTeamOfOrgByName(e Engine, orgID int64, name string) (*Team, error) {
t := &Team{
OrgID: orgID,
@@ -275,7 +295,7 @@ func getTeamOfOrgByName(e Engine, orgID int64, name string) (*Team, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, errors.TeamNotExist{Name: name}
+ return nil, ErrTeamNotExist{args: map[string]interface{}{"orgID": orgID, "name": name}}
}
return t, nil
}
@@ -291,7 +311,7 @@ func getTeamByID(e Engine, teamID int64) (*Team, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, errors.TeamNotExist{TeamID: teamID}
+ return nil, ErrTeamNotExist{args: map[string]interface{}{"teamID": teamID}}
}
return t, nil
}
diff --git a/internal/db/pull.go b/internal/db/pull.go
index cd08813f..331bc3dd 100644
--- a/internal/db/pull.go
+++ b/internal/db/pull.go
@@ -19,7 +19,7 @@ import (
api "github.com/gogs/go-gogs-client"
"gogs.io/gogs/internal/conf"
- "gogs.io/gogs/internal/db/errors"
+ "gogs.io/gogs/internal/errutil"
"gogs.io/gogs/internal/osutil"
"gogs.io/gogs/internal/process"
"gogs.io/gogs/internal/sync"
@@ -89,25 +89,25 @@ func (pr *PullRequest) AfterSet(colName string, _ xorm.Cell) {
func (pr *PullRequest) loadAttributes(e Engine) (err error) {
if pr.HeadRepo == nil {
pr.HeadRepo, err = getRepositoryByID(e, pr.HeadRepoID)
- if err != nil && !errors.IsRepoNotExist(err) {
- return fmt.Errorf("getRepositoryByID.(HeadRepo) [%d]: %v", pr.HeadRepoID, err)
+ if err != nil && !IsErrRepoNotExist(err) {
+ return fmt.Errorf("get head repository by ID: %v", err)
}
}
if pr.BaseRepo == nil {
pr.BaseRepo, err = getRepositoryByID(e, pr.BaseRepoID)
if err != nil {
- return fmt.Errorf("getRepositoryByID.(BaseRepo) [%d]: %v", pr.BaseRepoID, err)
+ return fmt.Errorf("get base repository by ID: %v", err)
}
}
if pr.HasMerged && pr.Merger == nil {
pr.Merger, err = getUserByID(e, pr.MergerID)
- if errors.IsUserNotExist(err) {
+ if IsErrUserNotExist(err) {
pr.MergerID = -1
pr.Merger = NewGhostUser()
} else if err != nil {
- return fmt.Errorf("getUserByID [%d]: %v", pr.MergerID, err)
+ return fmt.Errorf("get merger by ID: %v", err)
}
}
@@ -521,7 +521,12 @@ func GetUnmergedPullRequest(headRepoID, baseRepoID int64, headBranch, baseBranch
if err != nil {
return nil, err
} else if !has {
- return nil, ErrPullRequestNotExist{0, 0, headRepoID, baseRepoID, headBranch, baseBranch}
+ return nil, ErrPullRequestNotExist{args: map[string]interface{}{
+ "headRepoID": headRepoID,
+ "baseRepoID": baseRepoID,
+ "headBranch": headBranch,
+ "baseBranch": baseBranch,
+ }}
}
return pr, nil
@@ -545,13 +550,32 @@ func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequ
Join("INNER", "issue", "issue.id=pull_request.issue_id").Find(&prs)
}
+var _ errutil.NotFound = (*ErrPullRequestNotExist)(nil)
+
+type ErrPullRequestNotExist struct {
+ args map[string]interface{}
+}
+
+func IsErrPullRequestNotExist(err error) bool {
+ _, ok := err.(ErrPullRequestNotExist)
+ return ok
+}
+
+func (err ErrPullRequestNotExist) Error() string {
+ return fmt.Sprintf("pull request does not exist: %v", err.args)
+}
+
+func (ErrPullRequestNotExist) NotFound() bool {
+ return true
+}
+
func getPullRequestByID(e Engine, id int64) (*PullRequest, error) {
pr := new(PullRequest)
has, err := e.ID(id).Get(pr)
if err != nil {
return nil, err
} else if !has {
- return nil, ErrPullRequestNotExist{id, 0, 0, 0, "", ""}
+ return nil, ErrPullRequestNotExist{args: map[string]interface{}{"pullRequestID": id}}
}
return pr, pr.loadAttributes(e)
}
@@ -569,7 +593,7 @@ func getPullRequestByIssueID(e Engine, issueID int64) (*PullRequest, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, ErrPullRequestNotExist{0, issueID, 0, 0, "", ""}
+ return nil, ErrPullRequestNotExist{args: map[string]interface{}{"issueID": issueID}}
}
return pr, pr.loadAttributes(e)
}
diff --git a/internal/db/release.go b/internal/db/release.go
index fa69fba8..71a72dae 100644
--- a/internal/db/release.go
+++ b/internal/db/release.go
@@ -16,7 +16,7 @@ import (
"github.com/gogs/git-module"
api "github.com/gogs/go-gogs-client"
- "gogs.io/gogs/internal/db/errors"
+ "gogs.io/gogs/internal/errutil"
"gogs.io/gogs/internal/process"
)
@@ -68,7 +68,7 @@ func (r *Release) loadAttributes(e Engine) (err error) {
if r.Publisher == nil {
r.Publisher, err = getUserByID(e, r.PublisherID)
if err != nil {
- if errors.IsUserNotExist(err) {
+ if IsErrUserNotExist(err) {
r.PublisherID = -1
r.Publisher = NewGhostUser()
} else {
@@ -206,13 +206,32 @@ func NewRelease(gitRepo *git.Repository, r *Release, uuids []string) error {
return nil
}
+var _ errutil.NotFound = (*ErrReleaseNotExist)(nil)
+
+type ErrReleaseNotExist struct {
+ args map[string]interface{}
+}
+
+func IsErrReleaseNotExist(err error) bool {
+ _, ok := err.(ErrReleaseNotExist)
+ return ok
+}
+
+func (err ErrReleaseNotExist) Error() string {
+ return fmt.Sprintf("release does not exist: %v", err.args)
+}
+
+func (ErrReleaseNotExist) NotFound() bool {
+ return true
+}
+
// GetRelease returns release by given ID.
func GetRelease(repoID int64, tagName string) (*Release, error) {
isExist, err := IsReleaseExist(repoID, tagName)
if err != nil {
return nil, err
} else if !isExist {
- return nil, ErrReleaseNotExist{0, tagName}
+ return nil, ErrReleaseNotExist{args: map[string]interface{}{"tag": tagName}}
}
r := &Release{RepoID: repoID, LowerTagName: strings.ToLower(tagName)}
@@ -230,7 +249,7 @@ func GetReleaseByID(id int64) (*Release, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, ErrReleaseNotExist{id, ""}
+ return nil, ErrReleaseNotExist{args: map[string]interface{}{"releaseID": id}}
}
return r, r.LoadAttributes()
diff --git a/internal/db/repo.go b/internal/db/repo.go
index 7e607dc0..1a27a34a 100644
--- a/internal/db/repo.go
+++ b/internal/db/repo.go
@@ -33,6 +33,7 @@ import (
"gogs.io/gogs/internal/avatar"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/db/errors"
+ "gogs.io/gogs/internal/errutil"
"gogs.io/gogs/internal/markup"
"gogs.io/gogs/internal/osutil"
"gogs.io/gogs/internal/process"
@@ -248,11 +249,11 @@ func (repo *Repository) loadAttributes(e Engine) (err error) {
if repo.IsFork && repo.BaseRepo == nil {
repo.BaseRepo, err = getRepositoryByID(e, repo.ForkID)
if err != nil {
- if errors.IsRepoNotExist(err) {
+ if IsErrRepoNotExist(err) {
repo.IsFork = false
repo.ForkID = 0
} else {
- return fmt.Errorf("getRepositoryByID [%d]: %v", repo.ForkID, err)
+ return fmt.Errorf("get fork repository by ID: %v", err)
}
}
}
@@ -1104,10 +1105,23 @@ func createRepository(e *xorm.Session, doer, owner *User, repo *Repository) (err
return repo.loadAttributes(e)
}
+type ErrReachLimitOfRepo struct {
+ Limit int
+}
+
+func IsErrReachLimitOfRepo(err error) bool {
+ _, ok := err.(ErrReachLimitOfRepo)
+ return ok
+}
+
+func (err ErrReachLimitOfRepo) Error() string {
+ return fmt.Sprintf("user has reached maximum limit of repositories [limit: %d]", err.Limit)
+}
+
// CreateRepository creates a repository for given user or organization.
func CreateRepository(doer, owner *User, opts CreateRepoOptions) (_ *Repository, err error) {
if !owner.CanCreateRepo() {
- return nil, errors.ReachLimitOfRepo{Limit: owner.RepoCreationNum()}
+ return nil, ErrReachLimitOfRepo{Limit: owner.RepoCreationNum()}
}
repo := &Repository{
@@ -1481,17 +1495,17 @@ func UpdateRepository(repo *Repository, visibilityChanged bool) (err error) {
}
// DeleteRepository deletes a repository for a user or organization.
-func DeleteRepository(uid, repoID int64) error {
- repo := &Repository{ID: repoID, OwnerID: uid}
+func DeleteRepository(ownerID, repoID int64) error {
+ repo := &Repository{ID: repoID, OwnerID: ownerID}
has, err := x.Get(repo)
if err != nil {
return err
} else if !has {
- return errors.RepoNotExist{ID: repoID, UserID: uid}
+ return ErrRepoNotExist{args: map[string]interface{}{"ownerID": ownerID, "repoID": repoID}}
}
// In case is a organization.
- org, err := GetUserByID(uid)
+ org, err := GetUserByID(ownerID)
if err != nil {
return err
}
@@ -1571,7 +1585,7 @@ func DeleteRepository(uid, repoID int64) error {
}
}
- if _, err = sess.Exec("UPDATE `user` SET num_repos=num_repos-1 WHERE id=?", uid); err != nil {
+ if _, err = sess.Exec("UPDATE `user` SET num_repos=num_repos-1 WHERE id=?", ownerID); err != nil {
return err
}
@@ -1616,6 +1630,25 @@ func GetRepositoryByRef(ref string) (*Repository, error) {
return GetRepositoryByName(user.ID, repoName)
}
+var _ errutil.NotFound = (*ErrRepoNotExist)(nil)
+
+type ErrRepoNotExist struct {
+ args map[string]interface{}
+}
+
+func IsErrRepoNotExist(err error) bool {
+ _, ok := err.(ErrRepoNotExist)
+ return ok
+}
+
+func (err ErrRepoNotExist) Error() string {
+ return fmt.Sprintf("repository does not exist: %v", err.args)
+}
+
+func (ErrRepoNotExist) NotFound() bool {
+ return true
+}
+
// GetRepositoryByName returns the repository by given name under user if exists.
func GetRepositoryByName(ownerID int64, name string) (*Repository, error) {
repo := &Repository{
@@ -1626,7 +1659,7 @@ func GetRepositoryByName(ownerID int64, name string) (*Repository, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, errors.RepoNotExist{UserID: ownerID, Name: name}
+ return nil, ErrRepoNotExist{args: map[string]interface{}{"ownerID": ownerID, "name": name}}
}
return repo, repo.LoadAttributes()
}
@@ -1637,7 +1670,7 @@ func getRepositoryByID(e Engine, id int64) (*Repository, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, errors.RepoNotExist{ID: id}
+ return nil, ErrRepoNotExist{args: map[string]interface{}{"repoID": id}}
}
return repo, repo.loadAttributes(e)
}
@@ -2361,7 +2394,7 @@ func HasForkedRepo(ownerID, repoID int64) (*Repository, bool, error) {
// ForkRepository creates a fork of target repository under another user domain.
func ForkRepository(doer, owner *User, baseRepo *Repository, name, desc string) (_ *Repository, err error) {
if !owner.CanCreateRepo() {
- return nil, errors.ReachLimitOfRepo{Limit: owner.RepoCreationNum()}
+ return nil, ErrReachLimitOfRepo{Limit: owner.RepoCreationNum()}
}
repo := &Repository{
diff --git a/internal/db/repo_branch.go b/internal/db/repo_branch.go
index a4d38ecb..8da99945 100644
--- a/internal/db/repo_branch.go
+++ b/internal/db/repo_branch.go
@@ -11,7 +11,7 @@ import (
"github.com/gogs/git-module"
"github.com/unknwon/com"
- "gogs.io/gogs/internal/db/errors"
+ "gogs.io/gogs/internal/errutil"
"gogs.io/gogs/internal/tool"
)
@@ -44,9 +44,28 @@ func GetBranchesByPath(path string) ([]*Branch, error) {
return branches, nil
}
+var _ errutil.NotFound = (*ErrBranchNotExist)(nil)
+
+type ErrBranchNotExist struct {
+ args map[string]interface{}
+}
+
+func IsErrBranchNotExist(err error) bool {
+ _, ok := err.(ErrBranchNotExist)
+ return ok
+}
+
+func (err ErrBranchNotExist) Error() string {
+ return fmt.Sprintf("branch does not exist: %v", err.args)
+}
+
+func (ErrBranchNotExist) NotFound() bool {
+ return true
+}
+
func (repo *Repository) GetBranch(name string) (*Branch, error) {
if !git.RepoHasBranch(repo.RepoPath(), name) {
- return nil, errors.ErrBranchNotExist{Name: name}
+ return nil, ErrBranchNotExist{args: map[string]interface{}{"name": name}}
}
return &Branch{
RepoPath: repo.RepoPath(),
@@ -102,7 +121,7 @@ func GetProtectBranchOfRepoByName(repoID int64, name string) (*ProtectBranch, er
if err != nil {
return nil, err
} else if !has {
- return nil, errors.ErrBranchNotExist{Name: name}
+ return nil, ErrBranchNotExist{args: map[string]interface{}{"name": name}}
}
return protectBranch, nil
}
diff --git a/internal/db/ssh_key.go b/internal/db/ssh_key.go
index b31c0da1..b82f81a6 100644
--- a/internal/db/ssh_key.go
+++ b/internal/db/ssh_key.go
@@ -24,6 +24,7 @@ import (
"xorm.io/xorm"
"gogs.io/gogs/internal/conf"
+ "gogs.io/gogs/internal/errutil"
"gogs.io/gogs/internal/process"
)
@@ -684,6 +685,25 @@ func AddDeployKey(repoID int64, name, content string) (*DeployKey, error) {
return key, sess.Commit()
}
+var _ errutil.NotFound = (*ErrDeployKeyNotExist)(nil)
+
+type ErrDeployKeyNotExist struct {
+ args map[string]interface{}
+}
+
+func IsErrDeployKeyNotExist(err error) bool {
+ _, ok := err.(ErrDeployKeyNotExist)
+ return ok
+}
+
+func (err ErrDeployKeyNotExist) Error() string {
+ return fmt.Sprintf("deploy key does not exist: %v", err.args)
+}
+
+func (ErrDeployKeyNotExist) NotFound() bool {
+ return true
+}
+
// GetDeployKeyByID returns deploy key by given ID.
func GetDeployKeyByID(id int64) (*DeployKey, error) {
key := new(DeployKey)
@@ -691,7 +711,7 @@ func GetDeployKeyByID(id int64) (*DeployKey, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, ErrDeployKeyNotExist{id, 0, 0}
+ return nil, ErrDeployKeyNotExist{args: map[string]interface{}{"deployKeyID": id}}
}
return key, nil
}
@@ -706,7 +726,7 @@ func GetDeployKeyByRepo(keyID, repoID int64) (*DeployKey, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, ErrDeployKeyNotExist{0, keyID, repoID}
+ return nil, ErrDeployKeyNotExist{args: map[string]interface{}{"keyID": keyID, "repoID": repoID}}
}
return key, nil
}
diff --git a/internal/db/user.go b/internal/db/user.go
index c3c3f31e..0c793170 100644
--- a/internal/db/user.go
+++ b/internal/db/user.go
@@ -31,6 +31,7 @@ import (
"gogs.io/gogs/internal/avatar"
"gogs.io/gogs/internal/conf"
"gogs.io/gogs/internal/db/errors"
+ "gogs.io/gogs/internal/errutil"
"gogs.io/gogs/internal/tool"
)
@@ -606,8 +607,8 @@ func parseUserFromCode(code string) (user *User) {
if b, err := hex.DecodeString(hexStr); err == nil {
if user, err = GetUserByName(string(b)); user != nil {
return user
- } else if !errors.IsUserNotExist(err) {
- log.Error("GetUserByName: %v", err)
+ } else if !IsErrUserNotExist(err) {
+ log.Error("Failed to get user by name %q: %v", string(b), err)
}
}
@@ -890,13 +891,32 @@ func GetUserByKeyID(keyID int64) (*User, error) {
return user, nil
}
+var _ errutil.NotFound = (*ErrUserNotExist)(nil)
+
+type ErrUserNotExist struct {
+ args map[string]interface{}
+}
+
+func IsErrUserNotExist(err error) bool {
+ _, ok := err.(ErrUserNotExist)
+ return ok
+}
+
+func (err ErrUserNotExist) Error() string {
+ return fmt.Sprintf("user does not exist: %v", err.args)
+}
+
+func (ErrUserNotExist) NotFound() bool {
+ return true
+}
+
func getUserByID(e Engine, id int64) (*User, error) {
u := new(User)
has, err := e.ID(id).Get(u)
if err != nil {
return nil, err
} else if !has {
- return nil, errors.UserNotExist{UserID: id}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"userID": id}}
}
return u, nil
}
@@ -912,7 +932,7 @@ func GetAssigneeByID(repo *Repository, userID int64) (*User, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, errors.UserNotExist{UserID: userID}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"userID": userID}}
}
return GetUserByID(userID)
}
@@ -920,14 +940,14 @@ func GetAssigneeByID(repo *Repository, userID int64) (*User, error) {
// GetUserByName returns a user by given name.
func GetUserByName(name string) (*User, error) {
if len(name) == 0 {
- return nil, errors.UserNotExist{Name: name}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"name": name}}
}
u := &User{LowerName: strings.ToLower(name)}
has, err := x.Get(u)
if err != nil {
return nil, err
} else if !has {
- return nil, errors.UserNotExist{Name: name}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"name": name}}
}
return u, nil
}
@@ -999,7 +1019,7 @@ func ValidateCommitsWithEmails(oldCommits []*git.Commit) []*UserCommit {
// GetUserByEmail returns the user object by given e-mail if exists.
func GetUserByEmail(email string) (*User, error) {
if len(email) == 0 {
- return nil, errors.UserNotExist{Name: "email"}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"email": email}}
}
email = strings.ToLower(email)
@@ -1023,7 +1043,7 @@ func GetUserByEmail(email string) (*User, error) {
return GetUserByID(emailAddress.UID)
}
- return nil, errors.UserNotExist{Name: email}
+ return nil, ErrUserNotExist{args: map[string]interface{}{"email": email}}
}
type SearchUserOptions struct {
diff --git a/internal/db/user_mail.go b/internal/db/user_mail.go
index 37f0c2c0..1608dc19 100644
--- a/internal/db/user_mail.go
+++ b/internal/db/user_mail.go
@@ -181,7 +181,7 @@ func MakeEmailPrimary(userID int64, email *EmailAddress) error {
if err != nil {
return err
} else if !has {
- return errors.UserNotExist{UserID: email.UID}
+ return ErrUserNotExist{args: map[string]interface{}{"userID": email.UID}}
}
// Make sure the former primary email doesn't disappear.
diff --git a/internal/db/webhook.go b/internal/db/webhook.go
index 418f2729..1547a276 100644
--- a/internal/db/webhook.go
+++ b/internal/db/webhook.go
@@ -22,7 +22,7 @@ import (
api "github.com/gogs/go-gogs-client"
"gogs.io/gogs/internal/conf"
- "gogs.io/gogs/internal/db/errors"
+ "gogs.io/gogs/internal/errutil"
"gogs.io/gogs/internal/httplib"
"gogs.io/gogs/internal/sync"
)
@@ -235,6 +235,25 @@ func CreateWebhook(w *Webhook) error {
return err
}
+var _ errutil.NotFound = (*ErrWebhookNotExist)(nil)
+
+type ErrWebhookNotExist struct {
+ args map[string]interface{}
+}
+
+func IsErrWebhookNotExist(err error) bool {
+ _, ok := err.(ErrWebhookNotExist)
+ return ok
+}
+
+func (err ErrWebhookNotExist) Error() string {
+ return fmt.Sprintf("webhook does not exist: %v", err.args)
+}
+
+func (ErrWebhookNotExist) NotFound() bool {
+ return true
+}
+
// getWebhook uses argument bean as query condition,
// ID must be specified and do not assign unnecessary fields.
func getWebhook(bean *Webhook) (*Webhook, error) {
@@ -242,7 +261,7 @@ func getWebhook(bean *Webhook) (*Webhook, error) {
if err != nil {
return nil, err
} else if !has {
- return nil, errors.WebhookNotExist{ID: bean.ID}
+ return nil, ErrWebhookNotExist{args: map[string]interface{}{"webhookID": bean.ID}}
}
return bean, nil
}
@@ -499,6 +518,25 @@ func createHookTask(e Engine, t *HookTask) error {
return err
}
+var _ errutil.NotFound = (*ErrHookTaskNotExist)(nil)
+
+type ErrHookTaskNotExist struct {
+ args map[string]interface{}
+}
+
+func IsHookTaskNotExist(err error) bool {
+ _, ok := err.(ErrHookTaskNotExist)
+ return ok
+}
+
+func (err ErrHookTaskNotExist) Error() string {
+ return fmt.Sprintf("hook task does not exist: %v", err.args)
+}
+
+func (ErrHookTaskNotExist) NotFound() bool {
+ return true
+}
+
// GetHookTaskOfWebhookByUUID returns hook task of given webhook by UUID.
func GetHookTaskOfWebhookByUUID(webhookID int64, uuid string) (*HookTask, error) {
hookTask := &HookTask{
@@ -509,7 +547,7 @@ func GetHookTaskOfWebhookByUUID(webhookID int64, uuid string) (*HookTask, error)
if err != nil {
return nil, err
} else if !has {
- return nil, errors.HookTaskNotExist{HookID: webhookID, UUID: uuid}
+ return nil, ErrHookTaskNotExist{args: map[string]interface{}{"webhookID": webhookID, "uuid": uuid}}
}
return hookTask, nil
}