Selaa lähdekoodia

chore: refactor errors (#3651)

Kevin Wan 1 vuosi sitten
vanhempi
sitoutus
944e76edb9
3 muutettua tiedostoa jossa 5 lisäystä ja 6 poistoa
  1. 3 3
      core/breaker/breaker.go
  2. 2 2
      core/breaker/googlebreaker_test.go
  3. 0 1
      core/logx/writer.go

+ 3 - 3
core/breaker/breaker.go

@@ -46,7 +46,7 @@ type (
 		// DoWithAcceptable returns an error instantly if the Breaker rejects the request.
 		// DoWithAcceptable returns an error instantly if the Breaker rejects the request.
 		// If a panic occurs in the request, the Breaker handles it as an error
 		// If a panic occurs in the request, the Breaker handles it as an error
 		// and causes the same panic again.
 		// and causes the same panic again.
-		// acceptable checks if it's a successful call, even if the err is not nil.
+		// acceptable checks if it's a successful call, even if the error is not nil.
 		DoWithAcceptable(req func() error, acceptable Acceptable) error
 		DoWithAcceptable(req func() error, acceptable Acceptable) error
 
 
 		// DoWithFallback runs the given request if the Breaker accepts it.
 		// DoWithFallback runs the given request if the Breaker accepts it.
@@ -59,7 +59,7 @@ type (
 		// DoWithFallbackAcceptable runs the fallback if the Breaker rejects the request.
 		// DoWithFallbackAcceptable runs the fallback if the Breaker rejects the request.
 		// If a panic occurs in the request, the Breaker handles it as an error
 		// If a panic occurs in the request, the Breaker handles it as an error
 		// and causes the same panic again.
 		// and causes the same panic again.
-		// acceptable checks if it's a successful call, even if the err is not nil.
+		// acceptable checks if it's a successful call, even if the error is not nil.
 		DoWithFallbackAcceptable(req func() error, fallback func(err error) error, acceptable Acceptable) error
 		DoWithFallbackAcceptable(req func() error, fallback func(err error) error, acceptable Acceptable) error
 	}
 	}
 
 
@@ -179,7 +179,7 @@ func (lt loggedThrottle) doReq(req func() error, fallback func(err error) error,
 }
 }
 
 
 func (lt loggedThrottle) logError(err error) error {
 func (lt loggedThrottle) logError(err error) error {
-	if err == ErrServiceUnavailable {
+	if errors.Is(err, ErrServiceUnavailable) {
 		// if circuit open, not possible to have empty error window
 		// if circuit open, not possible to have empty error window
 		stat.Report(fmt.Sprintf(
 		stat.Report(fmt.Sprintf(
 			"proc(%s/%d), callee: %s, breaker is open and requests dropped\nlast errors:\n%s",
 			"proc(%s/%d), callee: %s, breaker is open and requests dropped\nlast errors:\n%s",

+ 2 - 2
core/breaker/googlebreaker_test.go

@@ -95,7 +95,7 @@ func TestGoogleBreakerAcceptable(t *testing.T) {
 	assert.Equal(t, errAcceptable, b.doReq(func() error {
 	assert.Equal(t, errAcceptable, b.doReq(func() error {
 		return errAcceptable
 		return errAcceptable
 	}, nil, func(err error) bool {
 	}, nil, func(err error) bool {
-		return err == errAcceptable
+		return errors.Is(err, errAcceptable)
 	}))
 	}))
 }
 }
 
 
@@ -105,7 +105,7 @@ func TestGoogleBreakerNotAcceptable(t *testing.T) {
 	assert.Equal(t, errAcceptable, b.doReq(func() error {
 	assert.Equal(t, errAcceptable, b.doReq(func() error {
 		return errAcceptable
 		return errAcceptable
 	}, nil, func(err error) bool {
 	}, nil, func(err error) bool {
-		return err != errAcceptable
+		return !errors.Is(err, errAcceptable)
 	}))
 	}))
 }
 }
 
 

+ 0 - 1
core/logx/writer.go

@@ -13,7 +13,6 @@ import (
 	"sync/atomic"
 	"sync/atomic"
 
 
 	fatihcolor "github.com/fatih/color"
 	fatihcolor "github.com/fatih/color"
-
 	"github.com/zeromicro/go-zero/core/color"
 	"github.com/zeromicro/go-zero/core/color"
 )
 )