mirror of
https://github.com/gogs/gogs.git
synced 2026-01-15 03:42:21 +01:00
refactoring: experimental with models/errors package
This commit is contained in:
12
models/errors/errors.go
Normal file
12
models/errors/errors.go
Normal file
@@ -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
Normal file
20
models/errors/issue.go
Normal file
@@ -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
Normal file
33
models/errors/login_source.go
Normal file
@@ -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
Normal file
33
models/errors/repo.go
Normal file
@@ -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
Normal file
31
models/errors/user.go
Normal file
@@ -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
Normal file
33
models/errors/user_mail.go
Normal file
@@ -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)
|
||||
}
|
||||
@@ -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()
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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:]
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user