diff --git a/internal/auth/auth.go b/internal/auth/auth.go index 2ae012d37..c1fef5fc1 100644 --- a/internal/auth/auth.go +++ b/internal/auth/auth.go @@ -7,6 +7,8 @@ package auth import ( "fmt" + "github.com/pkg/errors" + "gogs.io/gogs/internal/errutil" ) @@ -40,8 +42,10 @@ type ErrBadCredentials struct { Args errutil.Args } +// IsErrBadCredentials returns true if the underlying error has the type +// ErrBadCredentials. func IsErrBadCredentials(err error) bool { - _, ok := err.(ErrBadCredentials) + _, ok := errors.Cause(err).(ErrBadCredentials) return ok } diff --git a/internal/db/access_tokens.go b/internal/db/access_tokens.go index 58f77858f..9dab65d4d 100644 --- a/internal/db/access_tokens.go +++ b/internal/db/access_tokens.go @@ -9,6 +9,7 @@ import ( "fmt" "time" + "github.com/pkg/errors" gouuid "github.com/satori/go.uuid" "gorm.io/gorm" @@ -130,8 +131,10 @@ type ErrAccessTokenNotExist struct { args errutil.Args } +// IsErrAccessTokenNotExist returns true if the underlying error has the type +// ErrAccessTokenNotExist. func IsErrAccessTokenNotExist(err error) bool { - _, ok := err.(ErrAccessTokenNotExist) + _, ok := errors.Cause(err).(ErrAccessTokenNotExist) return ok } diff --git a/internal/db/email_addresses.go b/internal/db/email_addresses.go index 8cef705e0..4f30a8981 100644 --- a/internal/db/email_addresses.go +++ b/internal/db/email_addresses.go @@ -45,6 +45,8 @@ type ErrEmailNotExist struct { args errutil.Args } +// IsErrEmailAddressNotExist returns true if the underlying error has the type +// ErrEmailNotExist. func IsErrEmailAddressNotExist(err error) bool { _, ok := errors.Cause(err).(ErrEmailNotExist) return ok diff --git a/internal/db/users.go b/internal/db/users.go index 2b599597e..9e5687d43 100644 --- a/internal/db/users.go +++ b/internal/db/users.go @@ -112,6 +112,13 @@ type ErrLoginSourceMismatch struct { args errutil.Args } +// IsErrLoginSourceMismatch returns true if the underlying error has the type +// ErrLoginSourceMismatch. +func IsErrLoginSourceMismatch(err error) bool { + _, ok := errors.Cause(err).(ErrLoginSourceMismatch) + return ok +} + func (err ErrLoginSourceMismatch) Error() string { return fmt.Sprintf("login source mismatch: %v", err.args) } @@ -302,8 +309,10 @@ type ErrUserAlreadyExist struct { args errutil.Args } +// IsErrUserAlreadyExist returns true if the underlying error has the type +// ErrUserAlreadyExist. func IsErrUserAlreadyExist(err error) bool { - _, ok := err.(ErrUserAlreadyExist) + _, ok := errors.Cause(err).(ErrUserAlreadyExist) return ok } @@ -879,8 +888,10 @@ type ErrNameNotAllowed struct { args errutil.Args } +// IsErrNameNotAllowed returns true if the underlying error has the type +// ErrNameNotAllowed. func IsErrNameNotAllowed(err error) bool { - _, ok := err.(ErrNameNotAllowed) + _, ok := errors.Cause(err).(ErrNameNotAllowed) return ok } diff --git a/internal/gitutil/error.go b/internal/gitutil/error.go index 205546aa9..a823dd722 100644 --- a/internal/gitutil/error.go +++ b/internal/gitutil/error.go @@ -6,6 +6,7 @@ package gitutil import ( "github.com/gogs/git-module" + "github.com/pkg/errors" "gogs.io/gogs/internal/errutil" ) @@ -27,17 +28,19 @@ func NewError(err error) error { return Error{error: err} } -// IsErrSubmoduleNotExist returns true if the error is git.ErrSubmoduleNotExist. +// IsErrSubmoduleNotExist returns true if the underlying error is +// git.ErrSubmoduleNotExist. func IsErrSubmoduleNotExist(err error) bool { - return err == git.ErrSubmoduleNotExist + return errors.Cause(err) == git.ErrSubmoduleNotExist } -// IsErrRevisionNotExist returns true if the error is git.ErrRevisionNotExist. +// IsErrRevisionNotExist returns true if the underlying error is +// git.ErrRevisionNotExist. func IsErrRevisionNotExist(err error) bool { - return err == git.ErrRevisionNotExist + return errors.Cause(err) == git.ErrRevisionNotExist } -// IsErrNoMergeBase returns true if the error is git.ErrNoMergeBase. +// IsErrNoMergeBase returns true if the underlying error is git.ErrNoMergeBase. func IsErrNoMergeBase(err error) bool { - return err == git.ErrNoMergeBase + return errors.Cause(err) == git.ErrNoMergeBase } diff --git a/internal/route/lfs/route.go b/internal/route/lfs/route.go index b2ab2b0c7..3440c89b0 100644 --- a/internal/route/lfs/route.go +++ b/internal/route/lfs/route.go @@ -8,7 +8,6 @@ import ( "net/http" "strings" - "github.com/pkg/errors" "gopkg.in/macaron.v1" log "unknwon.dev/clog/v2" @@ -76,15 +75,15 @@ func authenticate() macaron.Handler { // or password as the token. if auth.IsErrBadCredentials(err) { user, err = context.AuthenticateByToken(c.Req.Context(), username) - if err != nil && !db.IsErrAccessTokenNotExist(errors.Cause(err)) { + if err != nil && !db.IsErrAccessTokenNotExist(err) { internalServerError(c.Resp) log.Error("Failed to authenticate by access token via username: %v", err) return - } else if db.IsErrAccessTokenNotExist(errors.Cause(err)) { + } else if db.IsErrAccessTokenNotExist(err) { // Try again using the password field as the token. user, err = context.AuthenticateByToken(c.Req.Context(), password) if err != nil { - if db.IsErrAccessTokenNotExist(errors.Cause(err)) { + if db.IsErrAccessTokenNotExist(err) { askCredentials(c.Resp) } else { c.Status(http.StatusInternalServerError) diff --git a/internal/route/org/setting.go b/internal/route/org/setting.go index bcef4ea1b..0cfc24546 100644 --- a/internal/route/org/setting.go +++ b/internal/route/org/setting.go @@ -5,7 +5,6 @@ package org import ( - "github.com/pkg/errors" log "unknwon.dev/clog/v2" "gogs.io/gogs/internal/auth" @@ -45,9 +44,9 @@ func SettingsPost(c *context.Context, f form.UpdateOrgSetting) { c.Data["OrgName"] = true var msg string switch { - case db.IsErrUserAlreadyExist(errors.Cause(err)): + case db.IsErrUserAlreadyExist(err): msg = c.Tr("form.username_been_taken") - case db.IsErrNameNotAllowed(errors.Cause(err)): + case db.IsErrNameNotAllowed(err): msg = c.Tr("user.form.name_not_allowed", err.(db.ErrNameNotAllowed).Value()) default: c.Error(err, "change organization name") diff --git a/internal/route/repo/http.go b/internal/route/repo/http.go index e8e1ac15b..96ac22e07 100644 --- a/internal/route/repo/http.go +++ b/internal/route/repo/http.go @@ -17,7 +17,6 @@ import ( "strings" "time" - "github.com/pkg/errors" "gopkg.in/macaron.v1" log "unknwon.dev/clog/v2" @@ -136,15 +135,15 @@ func HTTPContexter() macaron.Handler { // or password as the token. if authUser == nil { authUser, err = context.AuthenticateByToken(c.Req.Context(), authUsername) - if err != nil && !db.IsErrAccessTokenNotExist(errors.Cause(err)) { + if err != nil && !db.IsErrAccessTokenNotExist(err) { c.Status(http.StatusInternalServerError) log.Error("Failed to authenticate by access token via username: %v", err) return - } else if db.IsErrAccessTokenNotExist(errors.Cause(err)) { + } else if db.IsErrAccessTokenNotExist(err) { // Try again using the password field as the token. authUser, err = context.AuthenticateByToken(c.Req.Context(), authPassword) if err != nil { - if db.IsErrAccessTokenNotExist(errors.Cause(err)) { + if db.IsErrAccessTokenNotExist(err) { askCredentials(c, http.StatusUnauthorized, "") } else { c.Status(http.StatusInternalServerError) diff --git a/internal/route/repo/view.go b/internal/route/repo/view.go index 89d6f86d7..f4b6a162a 100644 --- a/internal/route/repo/view.go +++ b/internal/route/repo/view.go @@ -13,7 +13,6 @@ import ( "time" "github.com/gogs/git-module" - "github.com/pkg/errors" "github.com/unknwon/paginater" log "unknwon.dev/clog/v2" @@ -221,7 +220,7 @@ func renderFile(c *context.Context, entry *git.TreeEntry, treeLink, rawLink stri func setEditorconfigIfExists(c *context.Context) { ec, err := c.Repo.Editorconfig() - if err != nil && !gitutil.IsErrRevisionNotExist(errors.Cause(err)) { + if err != nil && !gitutil.IsErrRevisionNotExist(err) { log.Warn("setEditorconfigIfExists.Editorconfig [repo_id: %d]: %v", c.Repo.Repository.ID, err) return } diff --git a/internal/route/user/auth.go b/internal/route/user/auth.go index 1977fe177..8e63d9147 100644 --- a/internal/route/user/auth.go +++ b/internal/route/user/auth.go @@ -12,7 +12,6 @@ import ( "net/url" "github.com/go-macaron/captcha" - "github.com/pkg/errors" "github.com/unknwon/com" log "unknwon.dev/clog/v2" @@ -168,11 +167,11 @@ func LoginPost(c *context.Context, f form.SignIn) { u, err := db.Users.Authenticate(c.Req.Context(), f.UserName, f.Password, f.LoginSource) if err != nil { - switch errors.Cause(err).(type) { - case auth.ErrBadCredentials: + switch { + case auth.IsErrBadCredentials(err): c.FormErr("UserName", "Password") c.RenderWithErr(c.Tr("form.username_password_incorrect"), LOGIN, &f) - case db.ErrLoginSourceMismatch: + case db.IsErrLoginSourceMismatch(err): c.FormErr("LoginSource") c.RenderWithErr(c.Tr("form.auth_source_mismatch"), LOGIN, &f)