Răsfoiți Sursa

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

Joe Chen 2 ani în urmă
părinte
comite
44333afd20

+ 5 - 1
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
 }
 

+ 4 - 1
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
 }
 

+ 2 - 0
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

+ 13 - 2
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
 }
 

+ 9 - 6
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
 }

+ 3 - 4
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)

+ 2 - 3
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")

+ 3 - 4
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)

+ 1 - 2
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
 	}

+ 3 - 4
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)