diff --git a/models/errors/errors.go b/models/errors/errors.go new file mode 100644 index 000000000..43ed63352 --- /dev/null +++ b/models/errors/errors.go @@ -0,0 +1,12 @@ +// 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 "errors" + +// New is a wrapper of real errors.New function. +func New(text string) error { + return errors.New(text) +} diff --git a/models/errors/issue.go b/models/errors/issue.go new file mode 100644 index 000000000..4ea898fb9 --- /dev/null +++ b/models/errors/issue.go @@ -0,0 +1,20 @@ +// 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 InvalidIssueReference struct { + Ref string +} + +func IsInvalidIssueReference(err error) bool { + _, ok := err.(InvalidIssueReference) + return ok +} + +func (err InvalidIssueReference) Error() string { + return fmt.Sprintf("invalid issue reference [ref: %s]", err.Ref) +} diff --git a/models/errors/login_source.go b/models/errors/login_source.go new file mode 100644 index 000000000..db0cd1f9d --- /dev/null +++ b/models/errors/login_source.go @@ -0,0 +1,33 @@ +// 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 LoginSourceNotActivated struct { + SourceID int64 +} + +func IsLoginSourceNotActivated(err error) bool { + _, ok := err.(LoginSourceNotActivated) + return ok +} + +func (err LoginSourceNotActivated) Error() string { + return fmt.Sprintf("login source is not activated [source_id: %d]", err.SourceID) +} + +type InvalidLoginSourceType struct { + Type interface{} +} + +func IsInvalidLoginSourceType(err error) bool { + _, ok := err.(InvalidLoginSourceType) + return ok +} + +func (err InvalidLoginSourceType) Error() string { + return fmt.Sprintf("invalid login source type [type: %v]", err.Type) +} diff --git a/models/errors/repo.go b/models/errors/repo.go new file mode 100644 index 000000000..9abcaaa93 --- /dev/null +++ b/models/errors/repo.go @@ -0,0 +1,33 @@ +// 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 InvalidRepoReference struct { + Ref string +} + +func IsInvalidRepoReference(err error) bool { + _, ok := err.(InvalidRepoReference) + return ok +} + +func (err InvalidRepoReference) Error() string { + return fmt.Sprintf("invalid repository reference [ref: %s]", err.Ref) +} + +type MirrorNotExist struct { + RepoID int64 +} + +func IsMirrorNotExist(err error) bool { + _, ok := err.(MirrorNotExist) + return ok +} + +func (err MirrorNotExist) Error() string { + return fmt.Sprintf("mirror does not exist [repo_id: %d]", err.RepoID) +} diff --git a/models/errors/user.go b/models/errors/user.go new file mode 100644 index 000000000..cda49e0ef --- /dev/null +++ b/models/errors/user.go @@ -0,0 +1,31 @@ +// 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 EmptyName struct{} + +func IsEmptyName(err error) bool { + _, ok := err.(EmptyName) + return ok +} + +func (err EmptyName) Error() string { + return "empty name" +} + +type UserNotKeyOwner struct { + KeyID int64 +} + +func IsUserNotKeyOwner(err error) bool { + _, ok := err.(UserNotKeyOwner) + return ok +} + +func (err UserNotKeyOwner) Error() string { + return fmt.Sprintf("user is not the owner of public key [key_id: %d]", err.KeyID) +} diff --git a/models/errors/user_mail.go b/models/errors/user_mail.go new file mode 100644 index 000000000..fcdeb78c2 --- /dev/null +++ b/models/errors/user_mail.go @@ -0,0 +1,33 @@ +// 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 EmailNotFound struct { + Email string +} + +func IsEmailNotFound(err error) bool { + _, ok := err.(EmailNotFound) + return ok +} + +func (err EmailNotFound) Error() string { + return fmt.Sprintf("email is not found [email: %s]", err.Email) +} + +type EmailNotVerified struct { + Email string +} + +func IsEmailNotVerified(err error) bool { + _, ok := err.(EmailNotVerified) + return ok +} + +func (err EmailNotVerified) Error() string { + return fmt.Sprintf("email has not been verified [email: %s]", err.Email) +} diff --git a/models/issue.go b/models/issue.go index 8cf5add3c..8bc5914c6 100644 --- a/models/issue.go +++ b/models/issue.go @@ -5,7 +5,6 @@ package models import ( - "errors" "fmt" "strings" "time" @@ -16,6 +15,7 @@ import ( 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" ) @@ -796,7 +796,7 @@ func NewIssue(repo *Repository, issue *Issue, labelIDs []int64, uuids []string) func GetIssueByRef(ref string) (*Issue, error) { n := strings.IndexByte(ref, byte('#')) if n == -1 { - return nil, ErrMissingIssueNumber + return nil, errors.InvalidIssueReference{ref} } index, err := com.StrTo(ref[n+1:]).Int64() diff --git a/models/login_source.go b/models/login_source.go index 8a30ad228..1191994dc 100644 --- a/models/login_source.go +++ b/models/login_source.go @@ -7,7 +7,6 @@ package models import ( "crypto/tls" "encoding/json" - "errors" "fmt" "net/smtp" "net/textproto" @@ -20,6 +19,7 @@ import ( "github.com/go-xorm/xorm" log "gopkg.in/clog.v1" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/auth/ldap" "github.com/gogits/gogs/modules/auth/pam" ) @@ -394,7 +394,7 @@ func SMTPAuth(a smtp.Auth, cfg *SMTPConfig) error { } return nil } - return ErrUnsupportedLoginType + return errors.New("Unsupported SMTP authentication method") } // LoginViaSMTP queries if login/password is valid against the SMTP, @@ -416,7 +416,7 @@ func LoginViaSMTP(user *User, login, password string, sourceID int64, cfg *SMTPC } else if cfg.Auth == SMTP_LOGIN { auth = &smtpLoginAuth{login, password} } else { - return nil, errors.New("Unsupported SMTP auth type") + return nil, errors.New("Unsupported SMTP authentication type") } if err := SMTPAuth(auth, cfg); err != nil { @@ -489,7 +489,7 @@ func LoginViaPAM(user *User, login, password string, sourceID int64, cfg *PAMCon func ExternalUserLogin(user *User, login, password string, source *LoginSource, autoRegister bool) (*User, error) { if !source.IsActived { - return nil, ErrLoginSourceNotActived + return nil, errors.LoginSourceNotActivated{source.ID} } switch source.Type { @@ -501,7 +501,7 @@ func ExternalUserLogin(user *User, login, password string, source *LoginSource, return LoginViaPAM(user, login, password, source.ID, source.Cfg.(*PAMConfig), autoRegister) } - return nil, ErrUnsupportedLoginType + return nil, errors.InvalidLoginSourceType{source.Type} } // UserSignIn validates user name and password. diff --git a/models/mirror.go b/models/mirror.go index a1c14e04e..2107bc07b 100644 --- a/models/mirror.go +++ b/models/mirror.go @@ -16,6 +16,7 @@ import ( "github.com/gogits/git-module" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/process" "github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/sync" @@ -185,7 +186,7 @@ func getMirrorByRepoID(e Engine, repoID int64) (*Mirror, error) { if err != nil { return nil, err } else if !has { - return nil, ErrMirrorNotExist + return nil, errors.MirrorNotExist{repoID} } return m, nil } diff --git a/models/repo.go b/models/repo.go index 7fee5154d..23213b66a 100644 --- a/models/repo.go +++ b/models/repo.go @@ -6,7 +6,6 @@ package models import ( "bytes" - "errors" "fmt" "html/template" "io/ioutil" @@ -29,6 +28,7 @@ import ( git "github.com/gogits/git-module" api "github.com/gogits/go-gogs-client" + "github.com/gogits/gogs/models/errors" "github.com/gogits/gogs/modules/bindata" "github.com/gogits/gogs/modules/markdown" "github.com/gogits/gogs/modules/process" @@ -38,14 +38,6 @@ import ( var repoWorkingPool = sync.NewExclusivePool() -var ( - ErrRepoFileNotExist = errors.New("Repository file does not exist") - ErrRepoFileNotLoaded = errors.New("Repository file not loaded") - ErrMirrorNotExist = errors.New("Mirror does not exist") - ErrInvalidReference = errors.New("Invalid reference specified") - ErrNameEmpty = errors.New("Name is empty") -) - var ( Gitignores, Licenses, Readmes, LabelTemplates []string @@ -1028,7 +1020,7 @@ func CreateRepository(doer, owner *User, opts CreateRepoOptions) (_ *Repository, repoPath, fmt.Sprintf("CreateRepository 'git update-server-info': %s", repoPath), "git", "update-server-info") if err != nil { - return nil, errors.New("CreateRepository 'git update-server-info': " + stderr) + return nil, fmt.Errorf("CreateRepository 'git update-server-info': %s", stderr) } } @@ -1474,7 +1466,7 @@ func DeleteRepository(uid, repoID int64) error { func GetRepositoryByRef(ref string) (*Repository, error) { n := strings.IndexByte(ref, byte('/')) if n < 2 { - return nil, ErrInvalidReference + return nil, errors.InvalidRepoReference{ref} } userName, repoName := ref[:n], ref[n+1:] diff --git a/models/user.go b/models/user.go index 993c69dc0..2737d375f 100644 --- a/models/user.go +++ b/models/user.go @@ -10,7 +10,6 @@ import ( "crypto/sha256" "crypto/subtle" "encoding/hex" - "errors" "fmt" "image" _ "image/jpeg" @@ -30,6 +29,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/avatar" "github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/markdown" @@ -43,15 +43,6 @@ const ( USER_TYPE_ORGANIZATION ) -var ( - ErrUserNotKeyOwner = errors.New("User does not the owner of public key") - ErrEmailNotExist = errors.New("E-mail does not exist") - ErrEmailNotActivated = errors.New("E-mail address has not been activated") - ErrUserNameIllegal = errors.New("User name contains illegal characters") - ErrLoginSourceNotActived = errors.New("Login source is not actived") - ErrUnsupportedLoginType = errors.New("Login source is unknown") -) - // User represents the object of individual and member of organization. type User struct { ID int64 `xorm:"pk autoincr"` @@ -519,7 +510,7 @@ var ( func isUsableName(names, patterns []string, name string) error { name = strings.TrimSpace(strings.ToLower(name)) if utf8.RuneCountInString(name) == 0 { - return ErrNameEmpty + return errors.EmptyName{} } for i := range names { @@ -890,11 +881,11 @@ func UserPath(userName string) string { func GetUserByKeyID(keyID int64) (*User, error) { user := new(User) - has, err := x.Sql("SELECT a.* FROM `user` AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?", keyID).Get(user) + has, err := x.SQL("SELECT a.* FROM `user` AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?", keyID).Get(user) if err != nil { return nil, err } else if !has { - return nil, ErrUserNotKeyOwner + return nil, errors.UserNotKeyOwner{keyID} } return user, nil } diff --git a/models/user_mail.go b/models/user_mail.go index 4c6efd423..b1b23ee34 100644 --- a/models/user_mail.go +++ b/models/user_mail.go @@ -7,6 +7,8 @@ package models import ( "fmt" "strings" + + "github.com/gogits/gogs/models/errors" ) // EmailAdresses is the list of all email addresses of a user. Can contain the @@ -163,11 +165,11 @@ func MakeEmailPrimary(email *EmailAddress) error { if err != nil { return err } else if !has { - return ErrEmailNotExist + return errors.EmailNotFound{email.Email} } if !email.IsActivated { - return ErrEmailNotActivated + return errors.EmailNotVerified{email.Email} } user := &User{ID: email.UID} diff --git a/routers/org/setting.go b/routers/org/setting.go index 136e1b663..ae16854ab 100644 --- a/routers/org/setting.go +++ b/routers/org/setting.go @@ -51,10 +51,13 @@ func SettingsPost(ctx *context.Context, f form.UpdateOrgSetting) { ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &f) return } else if err = models.ChangeUserName(org, f.Name); err != nil { - if err == models.ErrUserNameIllegal { - ctx.Data["OrgName"] = true - ctx.RenderWithErr(ctx.Tr("form.illegal_username"), SETTINGS_OPTIONS, &f) - } else { + ctx.Data["OrgName"] = true + switch { + case models.IsErrNameReserved(err): + ctx.RenderWithErr(ctx.Tr("user.form.name_reserved"), SETTINGS_OPTIONS, &f) + case models.IsErrNamePatternNotAllowed(err): + ctx.RenderWithErr(ctx.Tr("user.form.name_pattern_not_allowed"), SETTINGS_OPTIONS, &f) + default: ctx.Handle(500, "ChangeUserName", err) } return