Browse Source

refactoring: experimental with models/errors package

pull/4280/head
Unknwon 8 years ago
parent
commit
05dbd3f7d7
No known key found for this signature in database
GPG Key ID: 25B575AE3213B2B3
  1. 12
      models/errors/errors.go
  2. 20
      models/errors/issue.go
  3. 33
      models/errors/login_source.go
  4. 33
      models/errors/repo.go
  5. 31
      models/errors/user.go
  6. 33
      models/errors/user_mail.go
  7. 4
      models/issue.go
  8. 10
      models/login_source.go
  9. 3
      models/mirror.go
  10. 14
      models/repo.go
  11. 17
      models/user.go
  12. 6
      models/user_mail.go
  13. 11
      routers/org/setting.go

12
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)
}

20
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)
}

33
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)
}

33
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)
}

31
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)
}

33
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)
}

4
models/issue.go

@ -5,7 +5,6 @@
package models package models
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
"time" "time"
@ -16,6 +15,7 @@ import (
api "github.com/gogits/go-gogs-client" api "github.com/gogits/go-gogs-client"
"github.com/gogits/gogs/models/errors"
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/setting" "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) { func GetIssueByRef(ref string) (*Issue, error) {
n := strings.IndexByte(ref, byte('#')) n := strings.IndexByte(ref, byte('#'))
if n == -1 { if n == -1 {
return nil, ErrMissingIssueNumber return nil, errors.InvalidIssueReference{ref}
} }
index, err := com.StrTo(ref[n+1:]).Int64() index, err := com.StrTo(ref[n+1:]).Int64()

10
models/login_source.go

@ -7,7 +7,6 @@ package models
import ( import (
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"net/smtp" "net/smtp"
"net/textproto" "net/textproto"
@ -20,6 +19,7 @@ import (
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
log "gopkg.in/clog.v1" log "gopkg.in/clog.v1"
"github.com/gogits/gogs/models/errors"
"github.com/gogits/gogs/modules/auth/ldap" "github.com/gogits/gogs/modules/auth/ldap"
"github.com/gogits/gogs/modules/auth/pam" "github.com/gogits/gogs/modules/auth/pam"
) )
@ -394,7 +394,7 @@ func SMTPAuth(a smtp.Auth, cfg *SMTPConfig) error {
} }
return nil return nil
} }
return ErrUnsupportedLoginType return errors.New("Unsupported SMTP authentication method")
} }
// LoginViaSMTP queries if login/password is valid against the SMTP, // 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 { } else if cfg.Auth == SMTP_LOGIN {
auth = &smtpLoginAuth{login, password} auth = &smtpLoginAuth{login, password}
} else { } else {
return nil, errors.New("Unsupported SMTP auth type") return nil, errors.New("Unsupported SMTP authentication type")
} }
if err := SMTPAuth(auth, cfg); err != nil { 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) { func ExternalUserLogin(user *User, login, password string, source *LoginSource, autoRegister bool) (*User, error) {
if !source.IsActived { if !source.IsActived {
return nil, ErrLoginSourceNotActived return nil, errors.LoginSourceNotActivated{source.ID}
} }
switch source.Type { 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 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. // UserSignIn validates user name and password.

3
models/mirror.go

@ -16,6 +16,7 @@ import (
"github.com/gogits/git-module" "github.com/gogits/git-module"
"github.com/gogits/gogs/models/errors"
"github.com/gogits/gogs/modules/process" "github.com/gogits/gogs/modules/process"
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
"github.com/gogits/gogs/modules/sync" "github.com/gogits/gogs/modules/sync"
@ -185,7 +186,7 @@ func getMirrorByRepoID(e Engine, repoID int64) (*Mirror, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
return nil, ErrMirrorNotExist return nil, errors.MirrorNotExist{repoID}
} }
return m, nil return m, nil
} }

14
models/repo.go

@ -6,7 +6,6 @@ package models
import ( import (
"bytes" "bytes"
"errors"
"fmt" "fmt"
"html/template" "html/template"
"io/ioutil" "io/ioutil"
@ -29,6 +28,7 @@ import (
git "github.com/gogits/git-module" git "github.com/gogits/git-module"
api "github.com/gogits/go-gogs-client" api "github.com/gogits/go-gogs-client"
"github.com/gogits/gogs/models/errors"
"github.com/gogits/gogs/modules/bindata" "github.com/gogits/gogs/modules/bindata"
"github.com/gogits/gogs/modules/markdown" "github.com/gogits/gogs/modules/markdown"
"github.com/gogits/gogs/modules/process" "github.com/gogits/gogs/modules/process"
@ -38,14 +38,6 @@ import (
var repoWorkingPool = sync.NewExclusivePool() 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 ( var (
Gitignores, Licenses, Readmes, LabelTemplates []string 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), repoPath, fmt.Sprintf("CreateRepository 'git update-server-info': %s", repoPath),
"git", "update-server-info") "git", "update-server-info")
if err != nil { 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) { func GetRepositoryByRef(ref string) (*Repository, error) {
n := strings.IndexByte(ref, byte('/')) n := strings.IndexByte(ref, byte('/'))
if n < 2 { if n < 2 {
return nil, ErrInvalidReference return nil, errors.InvalidRepoReference{ref}
} }
userName, repoName := ref[:n], ref[n+1:] userName, repoName := ref[:n], ref[n+1:]

17
models/user.go

@ -10,7 +10,6 @@ import (
"crypto/sha256" "crypto/sha256"
"crypto/subtle" "crypto/subtle"
"encoding/hex" "encoding/hex"
"errors"
"fmt" "fmt"
"image" "image"
_ "image/jpeg" _ "image/jpeg"
@ -30,6 +29,7 @@ import (
"github.com/gogits/git-module" "github.com/gogits/git-module"
api "github.com/gogits/go-gogs-client" api "github.com/gogits/go-gogs-client"
"github.com/gogits/gogs/models/errors"
"github.com/gogits/gogs/modules/avatar" "github.com/gogits/gogs/modules/avatar"
"github.com/gogits/gogs/modules/base" "github.com/gogits/gogs/modules/base"
"github.com/gogits/gogs/modules/markdown" "github.com/gogits/gogs/modules/markdown"
@ -43,15 +43,6 @@ const (
USER_TYPE_ORGANIZATION 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. // User represents the object of individual and member of organization.
type User struct { type User struct {
ID int64 `xorm:"pk autoincr"` ID int64 `xorm:"pk autoincr"`
@ -519,7 +510,7 @@ var (
func isUsableName(names, patterns []string, name string) error { func isUsableName(names, patterns []string, name string) error {
name = strings.TrimSpace(strings.ToLower(name)) name = strings.TrimSpace(strings.ToLower(name))
if utf8.RuneCountInString(name) == 0 { if utf8.RuneCountInString(name) == 0 {
return ErrNameEmpty return errors.EmptyName{}
} }
for i := range names { for i := range names {
@ -890,11 +881,11 @@ func UserPath(userName string) string {
func GetUserByKeyID(keyID int64) (*User, error) { func GetUserByKeyID(keyID int64) (*User, error) {
user := new(User) 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 { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {
return nil, ErrUserNotKeyOwner return nil, errors.UserNotKeyOwner{keyID}
} }
return user, nil return user, nil
} }

6
models/user_mail.go

@ -7,6 +7,8 @@ package models
import ( import (
"fmt" "fmt"
"strings" "strings"
"github.com/gogits/gogs/models/errors"
) )
// EmailAdresses is the list of all email addresses of a user. Can contain the // 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 { if err != nil {
return err return err
} else if !has { } else if !has {
return ErrEmailNotExist return errors.EmailNotFound{email.Email}
} }
if !email.IsActivated { if !email.IsActivated {
return ErrEmailNotActivated return errors.EmailNotVerified{email.Email}
} }
user := &User{ID: email.UID} user := &User{ID: email.UID}

11
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) ctx.RenderWithErr(ctx.Tr("form.username_been_taken"), SETTINGS_OPTIONS, &f)
return return
} else if err = models.ChangeUserName(org, f.Name); err != nil { } else if err = models.ChangeUserName(org, f.Name); err != nil {
if err == models.ErrUserNameIllegal { ctx.Data["OrgName"] = true
ctx.Data["OrgName"] = true switch {
ctx.RenderWithErr(ctx.Tr("form.illegal_username"), SETTINGS_OPTIONS, &f) case models.IsErrNameReserved(err):
} else { 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) ctx.Handle(500, "ChangeUserName", err)
} }
return return

Loading…
Cancel
Save