diff --git a/cmd/serv.go b/cmd/serv.go index 8759d0c09..41ec7c7cc 100644 --- a/cmd/serv.go +++ b/cmd/serv.go @@ -17,6 +17,7 @@ import ( log "gopkg.in/clog.v1" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/setting" http "github.com/gogits/gogs/routers/repo" ) @@ -153,7 +154,7 @@ func runServ(c *cli.Context) error { owner, err := models.GetUserByName(ownerName) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { fail("Repository owner does not exist", "Unregistered owner: %s", ownerName) } fail("Internal error", "Fail to get repository owner '%s': %v", ownerName, err) diff --git a/models/action.go b/models/action.go index b68e9a47d..b2ddf12f9 100644 --- a/models/action.go +++ b/models/action.go @@ -20,6 +20,7 @@ import ( "github.com/gogits/git-module" api "github.com/gogits/go-gogs-client" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/setting" ) @@ -292,7 +293,7 @@ func (push *PushCommits) AvatarLink(email string) string { u, err := GetUserByEmail(email) if err != nil { push.avatars[email] = base.AvatarLink(email) - if !IsErrUserNotExist(err) { + if !errors.IsUserNotExist(err) { log.Error(4, "GetUserByEmail: %v", err) } } else { diff --git a/models/comment.go b/models/comment.go index 078be5826..2b5ac923e 100644 --- a/models/comment.go +++ b/models/comment.go @@ -15,6 +15,7 @@ import ( api "github.com/gogits/go-gogs-client" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/markdown" ) @@ -95,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 IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { c.PosterID = -1 c.Poster = NewGhostUser() } else { diff --git a/models/error.go b/models/error.go index 182a944a6..18df15ce0 100644 --- a/models/error.go +++ b/models/error.go @@ -54,20 +54,6 @@ func (err ErrUserAlreadyExist) Error() string { return fmt.Sprintf("user already exists [name: %s]", err.Name) } -type ErrUserNotExist struct { - UID int64 - Name string -} - -func IsErrUserNotExist(err error) bool { - _, ok := err.(ErrUserNotExist) - return ok -} - -func (err ErrUserNotExist) Error() string { - return fmt.Sprintf("user does not exist [uid: %d, name: %s]", err.UID, err.Name) -} - type ErrEmailAlreadyUsed struct { Email string } diff --git a/models/errors/user.go b/models/errors/user.go index cda49e0ef..526d4b2d0 100644 --- a/models/errors/user.go +++ b/models/errors/user.go @@ -17,6 +17,20 @@ 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/models/issue.go b/models/issue.go index 3caa3b8fe..1eb261193 100644 --- a/models/issue.go +++ b/models/issue.go @@ -90,7 +90,7 @@ func (issue *Issue) loadAttributes(e Engine) (err error) { if issue.Poster == nil { issue.Poster, err = getUserByID(e, issue.PosterID) if err != nil { - if IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { issue.PosterID = -1 issue.Poster = NewGhostUser() } else { @@ -390,7 +390,7 @@ func (i *Issue) GetAssignee() (err error) { } i.Assignee, err = GetUserByID(i.AssigneeID) - if IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { return nil } return err @@ -595,7 +595,7 @@ func (issue *Issue) ChangeAssignee(doer *User, assigneeID int64) (err error) { } issue.Assignee, err = GetUserByID(issue.AssigneeID) - if err != nil && !IsErrUserNotExist(err) { + if err != nil && !errors.IsUserNotExist(err) { log.Error(4, "GetUserByID [assignee_id: %v]: %v", issue.AssigneeID, err) return nil } @@ -668,7 +668,7 @@ func newIssue(e *xorm.Session, opts NewIssueOptions) (err error) { if opts.Issue.AssigneeID > 0 { assignee, err := getUserByID(e, opts.Issue.AssigneeID) - if err != nil && !IsErrUserNotExist(err) { + if err != nil && !errors.IsUserNotExist(err) { return fmt.Errorf("getUserByID: %v", err) } diff --git a/models/login_source.go b/models/login_source.go index 1191994dc..29977fa34 100644 --- a/models/login_source.go +++ b/models/login_source.go @@ -296,7 +296,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, ErrUserNotExist{0, login} + return nil, errors.UserNotExist{0, login} } if !autoRegister { @@ -404,9 +404,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, ErrUserNotExist{0, login} + return nil, errors.UserNotExist{0, login} } else if !com.IsSliceContainsStr(strings.Split(cfg.AllowedDomains, ","), login[idx+1:]) { - return nil, ErrUserNotExist{0, login} + return nil, errors.UserNotExist{0, login} } } @@ -425,7 +425,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, ErrUserNotExist{0, login} + return nil, errors.UserNotExist{0, login} } return nil, err } @@ -465,7 +465,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, ErrUserNotExist{0, login} + return nil, errors.UserNotExist{0, login} } return nil, err } @@ -525,7 +525,7 @@ func UserSignIn(username, password string) (*User, error) { return user, nil } - return nil, ErrUserNotExist{user.ID, user.Name} + return nil, errors.UserNotExist{user.ID, user.Name} default: var source LoginSource @@ -554,5 +554,5 @@ func UserSignIn(username, password string) (*User, error) { log.Warn("Failed to login '%s' via '%s': %v", username, source.Name, err) } - return nil, ErrUserNotExist{user.ID, user.Name} + return nil, errors.UserNotExist{user.ID, user.Name} } diff --git a/models/pull.go b/models/pull.go index d1c5078d7..f36a0e18e 100644 --- a/models/pull.go +++ b/models/pull.go @@ -18,6 +18,7 @@ import ( "github.com/gogits/git-module" api "github.com/gogits/go-gogs-client" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/process" "github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/sync" @@ -101,7 +102,7 @@ func (pr *PullRequest) loadAttributes(e Engine) (err error) { if pr.HasMerged && pr.Merger == nil { pr.Merger, err = getUserByID(e, pr.MergerID) - if IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { pr.MergerID = -1 pr.Merger = NewGhostUser() } else if err != nil { diff --git a/models/release.go b/models/release.go index 47771bc1a..993fa19a5 100644 --- a/models/release.go +++ b/models/release.go @@ -16,6 +16,7 @@ import ( "github.com/gogits/git-module" api "github.com/gogits/go-gogs-client" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/process" ) @@ -67,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 IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { r.PublisherID = -1 r.Publisher = NewGhostUser() } else { diff --git a/models/user.go b/models/user.go index 2737d375f..a19f2a177 100644 --- a/models/user.go +++ b/models/user.go @@ -896,7 +896,7 @@ func getUserByID(e Engine, id int64) (*User, error) { if err != nil { return nil, err } else if !has { - return nil, ErrUserNotExist{id, ""} + return nil, errors.UserNotExist{id, ""} } return u, nil } @@ -912,7 +912,7 @@ func GetAssigneeByID(repo *Repository, userID int64) (*User, error) { if err != nil { return nil, err } else if !has { - return nil, ErrUserNotExist{userID, ""} + return nil, errors.UserNotExist{userID, ""} } return GetUserByID(userID) } @@ -920,14 +920,14 @@ func GetAssigneeByID(repo *Repository, userID int64) (*User, error) { // GetUserByName returns user by given name. func GetUserByName(name string) (*User, error) { if len(name) == 0 { - return nil, ErrUserNotExist{0, name} + return nil, errors.UserNotExist{0, name} } u := &User{LowerName: strings.ToLower(name)} has, err := x.Get(u) if err != nil { return nil, err } else if !has { - return nil, ErrUserNotExist{0, name} + return nil, errors.UserNotExist{0, name} } return u, nil } @@ -1005,7 +1005,7 @@ func ValidateCommitsWithEmails(oldCommits *list.List) *list.List { // GetUserByEmail returns the user object by given e-mail if exists. func GetUserByEmail(email string) (*User, error) { if len(email) == 0 { - return nil, ErrUserNotExist{0, "email"} + return nil, errors.UserNotExist{0, "email"} } email = strings.ToLower(email) @@ -1029,7 +1029,7 @@ func GetUserByEmail(email string) (*User, error) { return GetUserByID(emailAddress.UID) } - return nil, ErrUserNotExist{0, email} + return nil, errors.UserNotExist{0, email} } type SearchUserOptions struct { diff --git a/models/user_mail.go b/models/user_mail.go index b1b23ee34..7c1cdd16e 100644 --- a/models/user_mail.go +++ b/models/user_mail.go @@ -177,7 +177,7 @@ func MakeEmailPrimary(email *EmailAddress) error { if err != nil { return err } else if !has { - return ErrUserNotExist{email.UID, ""} + return errors.UserNotExist{email.UID, ""} } // Make sure the former primary email doesn't disappear. diff --git a/modules/auth/auth.go b/modules/auth/auth.go index 2e96ec1ae..6ec3afc6d 100644 --- a/modules/auth/auth.go +++ b/modules/auth/auth.go @@ -14,6 +14,7 @@ import ( "gopkg.in/macaron.v1" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/setting" ) @@ -65,7 +66,7 @@ func SignedInID(ctx *macaron.Context, sess session.Store) int64 { } if id, ok := uid.(int64); ok { if _, err := models.GetUserByID(id); err != nil { - if !models.IsErrUserNotExist(err) { + if !errors.IsUserNotExist(err) { log.Error(2, "GetUserById: %v", err) } return 0 @@ -90,7 +91,7 @@ func SignedInUser(ctx *macaron.Context, sess session.Store) (*models.User, bool) if len(webAuthUser) > 0 { u, err := models.GetUserByName(webAuthUser) if err != nil { - if !models.IsErrUserNotExist(err) { + if !errors.IsUserNotExist(err) { log.Error(4, "GetUserByName: %v", err) return nil, false } @@ -125,7 +126,7 @@ func SignedInUser(ctx *macaron.Context, sess session.Store) (*models.User, bool) u, err := models.UserSignIn(uname, passwd) if err != nil { - if !models.IsErrUserNotExist(err) { + if !errors.IsUserNotExist(err) { log.Error(4, "UserSignIn: %v", err) } return nil, false diff --git a/modules/context/org.go b/modules/context/org.go index 24ef5ef6a..3cde8d637 100644 --- a/modules/context/org.go +++ b/modules/context/org.go @@ -10,6 +10,7 @@ import ( "gopkg.in/macaron.v1" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/setting" ) @@ -49,11 +50,7 @@ func HandleOrgAssignment(ctx *Context, args ...bool) { var err error ctx.Org.Organization, err = models.GetUserByName(orgName) if err != nil { - if models.IsErrUserNotExist(err) { - ctx.Handle(404, "GetUserByName", err) - } else { - ctx.Handle(500, "GetUserByName", err) - } + ctx.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err) return } org := ctx.Org.Organization diff --git a/modules/context/repo.go b/modules/context/repo.go index 6a2911a79..209ea1e3c 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -18,6 +18,7 @@ import ( "github.com/gogits/git-module" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/setting" ) @@ -169,7 +170,7 @@ func RepoAssignment(args ...bool) macaron.Handler { } else { owner, err = models.GetUserByName(ownerName) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { if ctx.Query("go-get") == "1" { earlyResponseForGoGetMeta(ctx) return diff --git a/routers/api/v1/api.go b/routers/api/v1/api.go index 35234fa8e..49c1ee597 100644 --- a/routers/api/v1/api.go +++ b/routers/api/v1/api.go @@ -13,6 +13,7 @@ import ( api "github.com/gogits/go-gogs-client" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/form" "github.com/gogits/gogs/routers/api/v1/admin" @@ -38,7 +39,7 @@ func repoAssignment() macaron.Handler { } else { owner, err = models.GetUserByName(userName) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetUserByName", err) @@ -137,7 +138,7 @@ func orgAssignment(args ...bool) macaron.Handler { if assignOrg { ctx.Org.Organization, err = models.GetUserByName(ctx.Params(":orgname")) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetUserByName", err) @@ -149,7 +150,7 @@ func orgAssignment(args ...bool) macaron.Handler { if assignTeam { ctx.Org.Team, err = models.GetTeamByID(ctx.ParamsInt64(":teamid")) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetTeamById", err) diff --git a/routers/api/v1/repo/collaborators.go b/routers/api/v1/repo/collaborators.go index e1a3b423f..f240661f8 100644 --- a/routers/api/v1/repo/collaborators.go +++ b/routers/api/v1/repo/collaborators.go @@ -8,13 +8,14 @@ import ( api "github.com/gogits/go-gogs-client" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/context" ) func ListCollaborators(ctx *context.APIContext) { collaborators, err := ctx.Repo.Repository.GetCollaborators() if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "GetCollaborators", err) @@ -32,7 +33,7 @@ func ListCollaborators(ctx *context.APIContext) { func AddCollaborator(ctx *context.APIContext, form api.AddCollaboratorOption) { collaborator, err := models.GetUserByName(ctx.Params(":collaborator")) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "GetUserByName", err) @@ -58,7 +59,7 @@ func AddCollaborator(ctx *context.APIContext, form api.AddCollaboratorOption) { func IsCollaborator(ctx *context.APIContext) { collaborator, err := models.GetUserByName(ctx.Params(":collaborator")) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "GetUserByName", err) @@ -82,7 +83,7 @@ func IsCollaborator(ctx *context.APIContext) { func DeleteCollaborator(ctx *context.APIContext) { collaborator, err := models.GetUserByName(ctx.Params(":collaborator")) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "GetUserByName", err) diff --git a/routers/api/v1/repo/issue.go b/routers/api/v1/repo/issue.go index fe3b01cbb..97b058a9d 100644 --- a/routers/api/v1/repo/issue.go +++ b/routers/api/v1/repo/issue.go @@ -11,6 +11,7 @@ import ( api "github.com/gogits/go-gogs-client" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/setting" ) @@ -86,7 +87,7 @@ func CreateIssue(ctx *context.APIContext, form api.CreateIssueOption) { if len(form.Assignee) > 0 { assignee, err := models.GetUserByName(form.Assignee) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Error(422, "", fmt.Sprintf("Assignee does not exist: [name: %s]", form.Assignee)) } else { ctx.Error(500, "GetUserByName", err) @@ -152,7 +153,7 @@ func EditIssue(ctx *context.APIContext, form api.EditIssueOption) { } else { assignee, err := models.GetUserByName(*form.Assignee) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Error(422, "", fmt.Sprintf("assignee does not exist: [name: %s]", *form.Assignee)) } else { ctx.Error(500, "GetUserByName", err) diff --git a/routers/api/v1/repo/repo.go b/routers/api/v1/repo/repo.go index a6dd284ae..a491e7ceb 100644 --- a/routers/api/v1/repo/repo.go +++ b/routers/api/v1/repo/repo.go @@ -12,6 +12,7 @@ import ( api "github.com/gogits/go-gogs-client" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/form" "github.com/gogits/gogs/modules/setting" @@ -80,7 +81,7 @@ func Search(ctx *context.APIContext) { func listUserRepositories(ctx *context.APIContext, username string) { user, err := models.GetUserByName(username) if err != nil { - ctx.NotFoundOrServerError("GetUserByName", models.IsErrUserNotExist, err) + ctx.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err) return } @@ -190,7 +191,7 @@ func Create(ctx *context.APIContext, opt api.CreateRepoOption) { func CreateOrgRepo(ctx *context.APIContext, opt api.CreateRepoOption) { org, err := models.GetOrgByName(ctx.Params(":org")) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "GetOrgByName", err) @@ -213,7 +214,7 @@ func Migrate(ctx *context.APIContext, f form.MigrateRepo) { if f.Uid != ctxUser.ID { org, err := models.GetUserByID(f.Uid) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "GetUserByID", err) @@ -280,7 +281,7 @@ func Migrate(ctx *context.APIContext, f form.MigrateRepo) { func parseOwnerAndRepo(ctx *context.APIContext) (*models.User, *models.Repository) { owner, err := models.GetUserByName(ctx.Params(":username")) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Error(422, "", err) } else { ctx.Error(500, "GetUserByName", err) diff --git a/routers/api/v1/user/key.go b/routers/api/v1/user/key.go index 3e407feff..941cfb605 100644 --- a/routers/api/v1/user/key.go +++ b/routers/api/v1/user/key.go @@ -8,6 +8,7 @@ import ( api "github.com/gogits/go-gogs-client" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/routers/api/v1/convert" @@ -17,7 +18,7 @@ import ( func GetUserByParamsName(ctx *context.APIContext, name string) *models.User { user, err := models.GetUserByName(ctx.Params(name)) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetUserByName", err) diff --git a/routers/api/v1/user/user.go b/routers/api/v1/user/user.go index b526af5da..b1f06ec71 100644 --- a/routers/api/v1/user/user.go +++ b/routers/api/v1/user/user.go @@ -10,6 +10,7 @@ import ( api "github.com/gogits/go-gogs-client" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/context" ) @@ -54,7 +55,7 @@ func Search(ctx *context.APIContext) { func GetInfo(ctx *context.APIContext) { u, err := models.GetUserByName(ctx.Params(":username")) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Status(404) } else { ctx.Error(500, "GetUserByName", err) diff --git a/routers/org/members.go b/routers/org/members.go index 6eade743d..2f059c8c5 100644 --- a/routers/org/members.go +++ b/routers/org/members.go @@ -9,6 +9,7 @@ import ( log "gopkg.in/clog.v1" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/setting" @@ -100,7 +101,7 @@ func Invitation(ctx *context.Context) { uname := ctx.Query("uname") u, err := models.GetUserByName(uname) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Flash.Error(ctx.Tr("form.user_not_exist")) ctx.Redirect(ctx.Org.OrgLink + "/invitations/new") } else { diff --git a/routers/org/setting.go b/routers/org/setting.go index ae16854ab..5a07e2cb7 100644 --- a/routers/org/setting.go +++ b/routers/org/setting.go @@ -10,6 +10,7 @@ import ( log "gopkg.in/clog.v1" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/form" @@ -113,7 +114,7 @@ func SettingsDelete(ctx *context.Context) { org := ctx.Org.Organization if ctx.Req.Method == "POST" { if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil) } else { ctx.Handle(500, "UserSignIn", err) diff --git a/routers/org/teams.go b/routers/org/teams.go index 221b636ff..503739de5 100644 --- a/routers/org/teams.go +++ b/routers/org/teams.go @@ -11,6 +11,7 @@ import ( log "gopkg.in/clog.v1" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/form" @@ -73,7 +74,7 @@ func TeamsAction(ctx *context.Context) { var u *models.User u, err = models.GetUserByName(uname) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Flash.Error(ctx.Tr("form.user_not_exist")) ctx.Redirect(ctx.Org.OrgLink + "/teams/" + ctx.Org.Team.LowerName) } else { diff --git a/routers/repo/http.go b/routers/repo/http.go index b35d5a9c8..b385a13a2 100644 --- a/routers/repo/http.go +++ b/routers/repo/http.go @@ -22,6 +22,7 @@ import ( "gopkg.in/macaron.v1" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/setting" @@ -59,7 +60,7 @@ func HTTPContexter() macaron.Handler { owner, err := models.GetUserByName(ownerName) if err != nil { - ctx.NotFoundOrServerError("GetUserByName", models.IsErrUserNotExist, err) + ctx.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err) return } @@ -107,7 +108,7 @@ func HTTPContexter() macaron.Handler { } authUser, err := models.UserSignIn(authUsername, authPassword) - if err != nil && !models.IsErrUserNotExist(err) { + if err != nil && !errors.IsUserNotExist(err) { ctx.Handle(http.StatusInternalServerError, "UserSignIn", err) return } diff --git a/routers/repo/pull.go b/routers/repo/pull.go index c4c5187c5..9184c5e8e 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -15,6 +15,7 @@ import ( "github.com/gogits/git-module" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/form" @@ -449,11 +450,7 @@ func ParseCompareInfo(ctx *context.Context) (*models.User, *models.Repository, * } else if len(headInfos) == 2 { headUser, err = models.GetUserByName(headInfos[0]) if err != nil { - if models.IsErrUserNotExist(err) { - ctx.Handle(404, "GetUserByName", nil) - } else { - ctx.Handle(500, "GetUserByName", err) - } + ctx.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err) return nil, nil, nil, nil, "", "" } headBranch = headInfos[1] @@ -719,7 +716,7 @@ func CompareAndPullRequestPost(ctx *context.Context, f form.NewIssue) { func parseOwnerAndRepo(ctx *context.Context) (*models.User, *models.Repository) { owner, err := models.GetUserByName(ctx.Params(":username")) if err != nil { - ctx.NotFoundOrServerError("GetUserByName", models.IsErrUserNotExist, err) + ctx.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err) return nil, nil } @@ -753,11 +750,7 @@ func TriggerTask(ctx *context.Context) { pusher, err := models.GetUserByID(pusherID) if err != nil { - if models.IsErrUserNotExist(err) { - ctx.Error(404) - } else { - ctx.Handle(500, "GetUserByID", err) - } + ctx.NotFoundOrServerError("GetUserByID", errors.IsUserNotExist, err) return } diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 8a32f214d..4604d643b 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -16,6 +16,7 @@ import ( "github.com/gogits/git-module" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/form" @@ -47,7 +48,7 @@ func checkContextUser(ctx *context.Context, uid int64) *models.User { } org, err := models.GetUserByID(uid) - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { return ctx.User } diff --git a/routers/repo/setting.go b/routers/repo/setting.go index 6ccd058a3..c52be187d 100644 --- a/routers/repo/setting.go +++ b/routers/repo/setting.go @@ -14,6 +14,7 @@ import ( "github.com/gogits/git-module" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/form" @@ -311,7 +312,7 @@ func SettingsCollaborationPost(ctx *context.Context) { u, err := models.GetUserByName(name) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Flash.Error(ctx.Tr("form.user_not_exist")) ctx.Redirect(setting.AppSubUrl + ctx.Req.URL.Path) } else { diff --git a/routers/repo/webhook.go b/routers/repo/webhook.go index 506acec76..73bd87864 100644 --- a/routers/repo/webhook.go +++ b/routers/repo/webhook.go @@ -6,7 +6,6 @@ package repo import ( "encoding/json" - "errors" "fmt" "strings" @@ -16,6 +15,7 @@ import ( api "github.com/gogits/go-gogs-client" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/form" @@ -468,7 +468,7 @@ func TestWebhook(ctx *context.Context) { author, err := models.GetUserByEmail(commit.Author.Email) if err == nil { authorUsername = author.Name - } else if !models.IsErrUserNotExist(err) { + } else if !errors.IsUserNotExist(err) { ctx.Flash.Error(fmt.Sprintf("GetUserByEmail.(author) [%s]: %v", commit.Author.Email, err)) ctx.Status(500) return @@ -477,7 +477,7 @@ func TestWebhook(ctx *context.Context) { committer, err := models.GetUserByEmail(commit.Committer.Email) if err == nil { committerUsername = committer.Name - } else if !models.IsErrUserNotExist(err) { + } else if !errors.IsUserNotExist(err) { ctx.Flash.Error(fmt.Sprintf("GetUserByEmail.(committer) [%s]: %v", commit.Committer.Email, err)) ctx.Status(500) return diff --git a/routers/user/auth.go b/routers/user/auth.go index 36aaa1928..014a33170 100644 --- a/routers/user/auth.go +++ b/routers/user/auth.go @@ -12,6 +12,7 @@ import ( log "gopkg.in/clog.v1" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/form" @@ -49,7 +50,7 @@ func AutoSignIn(ctx *context.Context) (bool, error) { u, err := models.GetUserByName(uname) if err != nil { - if !models.IsErrUserNotExist(err) { + if !errors.IsUserNotExist(err) { return false, fmt.Errorf("GetUserByName: %v", err) } return false, nil @@ -113,7 +114,7 @@ func SignInPost(ctx *context.Context, f form.SignIn) { u, err := models.UserSignIn(f.UserName, f.Password) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.RenderWithErr(ctx.Tr("form.username_password_incorrect"), SIGNIN, &f) } else { ctx.Handle(500, "UserSignIn", err) @@ -286,11 +287,7 @@ func Activate(ctx *context.Context) { return } if err := models.UpdateUser(user); err != nil { - if models.IsErrUserNotExist(err) { - ctx.Error(404) - } else { - ctx.Handle(500, "UpdateUser", err) - } + ctx.Handle(500, "UpdateUser", err) return } @@ -351,7 +348,7 @@ func ForgotPasswdPost(ctx *context.Context) { u, err := models.GetUserByEmail(email) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.Data["Hours"] = setting.Service.ActiveCodeLives / 60 ctx.Data["IsResetSent"] = true ctx.HTML(200, FORGOT_PASSWORD) diff --git a/routers/user/home.go b/routers/user/home.go index cb85bd8ff..39123da2b 100644 --- a/routers/user/home.go +++ b/routers/user/home.go @@ -12,6 +12,7 @@ import ( "github.com/Unknwon/paginater" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/setting" @@ -32,11 +33,7 @@ func getDashboardContextUser(ctx *context.Context) *models.User { // Organization. org, err := models.GetUserByName(orgName) if err != nil { - if models.IsErrUserNotExist(err) { - ctx.Handle(404, "GetUserByName", err) - } else { - ctx.Handle(500, "GetUserByName", err) - } + ctx.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err) return nil } ctxUser = org @@ -71,7 +68,7 @@ func retrieveFeeds(ctx *context.Context, ctxUser *models.User, userID, offset in if !ok { u, err := models.GetUserByName(act.ActUserName) if err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { continue } ctx.Handle(500, "GetUserByName", err) @@ -406,11 +403,7 @@ func showOrgProfile(ctx *context.Context) { func Email2User(ctx *context.Context) { u, err := models.GetUserByEmail(ctx.Query("email")) if err != nil { - if models.IsErrUserNotExist(err) { - ctx.Handle(404, "GetUserByEmail", err) - } else { - ctx.Handle(500, "GetUserByEmail", err) - } + ctx.NotFoundOrServerError("GetUserByEmail", errors.IsUserNotExist, err) return } ctx.Redirect(setting.AppSubUrl + "/user/" + u.Name) diff --git a/routers/user/profile.go b/routers/user/profile.go index 414425b23..be4356198 100644 --- a/routers/user/profile.go +++ b/routers/user/profile.go @@ -12,6 +12,7 @@ import ( "github.com/Unknwon/paginater" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/setting" @@ -26,11 +27,7 @@ const ( func GetUserByName(ctx *context.Context, name string) *models.User { user, err := models.GetUserByName(name) if err != nil { - if models.IsErrUserNotExist(err) { - ctx.Handle(404, "GetUserByName", nil) - } else { - ctx.Handle(500, "GetUserByName", err) - } + ctx.NotFoundOrServerError("GetUserByName", errors.IsUserNotExist, err) return nil } return user diff --git a/routers/user/setting.go b/routers/user/setting.go index 0ea00770e..c2c273c22 100644 --- a/routers/user/setting.go +++ b/routers/user/setting.go @@ -5,7 +5,6 @@ package user import ( - "errors" "fmt" "io/ioutil" "strings" @@ -14,6 +13,7 @@ import ( log "gopkg.in/clog.v1" "github.com/gogits/gogs/models" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/context" "github.com/gogits/gogs/modules/form" @@ -456,7 +456,7 @@ func SettingsDelete(ctx *context.Context) { if ctx.Req.Method == "POST" { if _, err := models.UserSignIn(ctx.User.Name, ctx.Query("password")); err != nil { - if models.IsErrUserNotExist(err) { + if errors.IsUserNotExist(err) { ctx.RenderWithErr(ctx.Tr("form.enterred_invalid_password"), SETTINGS_DELETE, nil) } else { ctx.Handle(500, "UserSignIn", err)