chore: consistently use `errors.Cause` for identifying error types (#7264)

pull/7267/head
Joe Chen 2022-11-27 15:53:26 +08:00 committed by GitHub
parent 13099a7e4f
commit 44333afd20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 45 additions and 27 deletions

View File

@ -7,6 +7,8 @@ package auth
import ( import (
"fmt" "fmt"
"github.com/pkg/errors"
"gogs.io/gogs/internal/errutil" "gogs.io/gogs/internal/errutil"
) )
@ -40,8 +42,10 @@ type ErrBadCredentials struct {
Args errutil.Args Args errutil.Args
} }
// IsErrBadCredentials returns true if the underlying error has the type
// ErrBadCredentials.
func IsErrBadCredentials(err error) bool { func IsErrBadCredentials(err error) bool {
_, ok := err.(ErrBadCredentials) _, ok := errors.Cause(err).(ErrBadCredentials)
return ok return ok
} }

View File

@ -9,6 +9,7 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/pkg/errors"
gouuid "github.com/satori/go.uuid" gouuid "github.com/satori/go.uuid"
"gorm.io/gorm" "gorm.io/gorm"
@ -130,8 +131,10 @@ type ErrAccessTokenNotExist struct {
args errutil.Args args errutil.Args
} }
// IsErrAccessTokenNotExist returns true if the underlying error has the type
// ErrAccessTokenNotExist.
func IsErrAccessTokenNotExist(err error) bool { func IsErrAccessTokenNotExist(err error) bool {
_, ok := err.(ErrAccessTokenNotExist) _, ok := errors.Cause(err).(ErrAccessTokenNotExist)
return ok return ok
} }

View File

@ -45,6 +45,8 @@ type ErrEmailNotExist struct {
args errutil.Args args errutil.Args
} }
// IsErrEmailAddressNotExist returns true if the underlying error has the type
// ErrEmailNotExist.
func IsErrEmailAddressNotExist(err error) bool { func IsErrEmailAddressNotExist(err error) bool {
_, ok := errors.Cause(err).(ErrEmailNotExist) _, ok := errors.Cause(err).(ErrEmailNotExist)
return ok return ok

View File

@ -112,6 +112,13 @@ type ErrLoginSourceMismatch struct {
args errutil.Args 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 { func (err ErrLoginSourceMismatch) Error() string {
return fmt.Sprintf("login source mismatch: %v", err.args) return fmt.Sprintf("login source mismatch: %v", err.args)
} }
@ -302,8 +309,10 @@ type ErrUserAlreadyExist struct {
args errutil.Args args errutil.Args
} }
// IsErrUserAlreadyExist returns true if the underlying error has the type
// ErrUserAlreadyExist.
func IsErrUserAlreadyExist(err error) bool { func IsErrUserAlreadyExist(err error) bool {
_, ok := err.(ErrUserAlreadyExist) _, ok := errors.Cause(err).(ErrUserAlreadyExist)
return ok return ok
} }
@ -879,8 +888,10 @@ type ErrNameNotAllowed struct {
args errutil.Args args errutil.Args
} }
// IsErrNameNotAllowed returns true if the underlying error has the type
// ErrNameNotAllowed.
func IsErrNameNotAllowed(err error) bool { func IsErrNameNotAllowed(err error) bool {
_, ok := err.(ErrNameNotAllowed) _, ok := errors.Cause(err).(ErrNameNotAllowed)
return ok return ok
} }

View File

@ -6,6 +6,7 @@ package gitutil
import ( import (
"github.com/gogs/git-module" "github.com/gogs/git-module"
"github.com/pkg/errors"
"gogs.io/gogs/internal/errutil" "gogs.io/gogs/internal/errutil"
) )
@ -27,17 +28,19 @@ func NewError(err error) error {
return Error{error: err} 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 { 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 { 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 { func IsErrNoMergeBase(err error) bool {
return err == git.ErrNoMergeBase return errors.Cause(err) == git.ErrNoMergeBase
} }

View File

@ -8,7 +8,6 @@ import (
"net/http" "net/http"
"strings" "strings"
"github.com/pkg/errors"
"gopkg.in/macaron.v1" "gopkg.in/macaron.v1"
log "unknwon.dev/clog/v2" log "unknwon.dev/clog/v2"
@ -76,15 +75,15 @@ func authenticate() macaron.Handler {
// or password as the token. // or password as the token.
if auth.IsErrBadCredentials(err) { if auth.IsErrBadCredentials(err) {
user, err = context.AuthenticateByToken(c.Req.Context(), username) 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) internalServerError(c.Resp)
log.Error("Failed to authenticate by access token via username: %v", err) log.Error("Failed to authenticate by access token via username: %v", err)
return return
} else if db.IsErrAccessTokenNotExist(errors.Cause(err)) { } else if db.IsErrAccessTokenNotExist(err) {
// Try again using the password field as the token. // Try again using the password field as the token.
user, err = context.AuthenticateByToken(c.Req.Context(), password) user, err = context.AuthenticateByToken(c.Req.Context(), password)
if err != nil { if err != nil {
if db.IsErrAccessTokenNotExist(errors.Cause(err)) { if db.IsErrAccessTokenNotExist(err) {
askCredentials(c.Resp) askCredentials(c.Resp)
} else { } else {
c.Status(http.StatusInternalServerError) c.Status(http.StatusInternalServerError)

View File

@ -5,7 +5,6 @@
package org package org
import ( import (
"github.com/pkg/errors"
log "unknwon.dev/clog/v2" log "unknwon.dev/clog/v2"
"gogs.io/gogs/internal/auth" "gogs.io/gogs/internal/auth"
@ -45,9 +44,9 @@ func SettingsPost(c *context.Context, f form.UpdateOrgSetting) {
c.Data["OrgName"] = true c.Data["OrgName"] = true
var msg string var msg string
switch { switch {
case db.IsErrUserAlreadyExist(errors.Cause(err)): case db.IsErrUserAlreadyExist(err):
msg = c.Tr("form.username_been_taken") 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()) msg = c.Tr("user.form.name_not_allowed", err.(db.ErrNameNotAllowed).Value())
default: default:
c.Error(err, "change organization name") c.Error(err, "change organization name")

View File

@ -17,7 +17,6 @@ import (
"strings" "strings"
"time" "time"
"github.com/pkg/errors"
"gopkg.in/macaron.v1" "gopkg.in/macaron.v1"
log "unknwon.dev/clog/v2" log "unknwon.dev/clog/v2"
@ -136,15 +135,15 @@ func HTTPContexter() macaron.Handler {
// or password as the token. // or password as the token.
if authUser == nil { if authUser == nil {
authUser, err = context.AuthenticateByToken(c.Req.Context(), authUsername) 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) c.Status(http.StatusInternalServerError)
log.Error("Failed to authenticate by access token via username: %v", err) log.Error("Failed to authenticate by access token via username: %v", err)
return return
} else if db.IsErrAccessTokenNotExist(errors.Cause(err)) { } else if db.IsErrAccessTokenNotExist(err) {
// Try again using the password field as the token. // Try again using the password field as the token.
authUser, err = context.AuthenticateByToken(c.Req.Context(), authPassword) authUser, err = context.AuthenticateByToken(c.Req.Context(), authPassword)
if err != nil { if err != nil {
if db.IsErrAccessTokenNotExist(errors.Cause(err)) { if db.IsErrAccessTokenNotExist(err) {
askCredentials(c, http.StatusUnauthorized, "") askCredentials(c, http.StatusUnauthorized, "")
} else { } else {
c.Status(http.StatusInternalServerError) c.Status(http.StatusInternalServerError)

View File

@ -13,7 +13,6 @@ import (
"time" "time"
"github.com/gogs/git-module" "github.com/gogs/git-module"
"github.com/pkg/errors"
"github.com/unknwon/paginater" "github.com/unknwon/paginater"
log "unknwon.dev/clog/v2" 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) { func setEditorconfigIfExists(c *context.Context) {
ec, err := c.Repo.Editorconfig() 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) log.Warn("setEditorconfigIfExists.Editorconfig [repo_id: %d]: %v", c.Repo.Repository.ID, err)
return return
} }

View File

@ -12,7 +12,6 @@ import (
"net/url" "net/url"
"github.com/go-macaron/captcha" "github.com/go-macaron/captcha"
"github.com/pkg/errors"
"github.com/unknwon/com" "github.com/unknwon/com"
log "unknwon.dev/clog/v2" 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) u, err := db.Users.Authenticate(c.Req.Context(), f.UserName, f.Password, f.LoginSource)
if err != nil { if err != nil {
switch errors.Cause(err).(type) { switch {
case auth.ErrBadCredentials: case auth.IsErrBadCredentials(err):
c.FormErr("UserName", "Password") c.FormErr("UserName", "Password")
c.RenderWithErr(c.Tr("form.username_password_incorrect"), LOGIN, &f) c.RenderWithErr(c.Tr("form.username_password_incorrect"), LOGIN, &f)
case db.ErrLoginSourceMismatch: case db.IsErrLoginSourceMismatch(err):
c.FormErr("LoginSource") c.FormErr("LoginSource")
c.RenderWithErr(c.Tr("form.auth_source_mismatch"), LOGIN, &f) c.RenderWithErr(c.Tr("form.auth_source_mismatch"), LOGIN, &f)