Repository: zeromicro/go-zero
Pull request #3651 (2023-10-20 06:26:02)

chore: refactor errors

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/zeromicro/go-zero/pull/3651?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) Report
> Merging [#3651](https://app.codecov.io/gh/zeromicro/go-zero/pull/3651?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (d47b190) into [master](https://app.codecov.io/gh/zeromicro/go-zero/commit/151768ef82396febc1889181e9df9d4b5dd1287f?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) (151768e) will **not change** coverage.
> The diff coverage is `100.00%`.


| [Files](https://app.codecov.io/gh/zeromicro/go-zero/pull/3651?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None) | Coverage Δ | |
|---|---|---|
| [core/breaker/breaker.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/3651?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9icmVha2VyL2JyZWFrZXIuZ28=) | `100.00% <100.00%> (ø)` | |
| [core/logx/writer.go](https://app.codecov.io/gh/zeromicro/go-zero/pull/3651?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=None#diff-Y29yZS9sb2d4L3dyaXRlci5nbw==) | `95.52% <ø> (ø)` | |


Patch:
diff --git a/core/breaker/breaker.go b/core/breaker/breaker.go
index 3d5102ef..9fc4a483 100644
--- a/core/breaker/breaker.go
+++ b/core/breaker/breaker.go
@@ -46,7 +46,7 @@ type (
 		// 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
 		// 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
 
 		// 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.
 		// If a panic occurs in the request, the Breaker handles it as an error
 		// 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
 	}
 
@@ -179,7 +179,7 @@ func (lt loggedThrottle) doReq(req func() error, fallback func(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
 		stat.Report(fmt.Sprintf(
 			"proc(%s/%d), callee: %s, breaker is open and requests dropped\nlast errors:\n%s",
diff --git a/core/logx/writer.go b/core/logx/writer.go
index d38fb2a6..1d60a5bd 100644
--- a/core/logx/writer.go
+++ b/core/logx/writer.go
@@ -13,7 +13,6 @@ import (
 	"sync/atomic"
 
 	fatihcolor "github.com/fatih/color"
-
 	"github.com/zeromicro/go-zero/core/color"
 )
 


Test patch:
diff --git a/core/breaker/googlebreaker_test.go b/core/breaker/googlebreaker_test.go
index e525de5b..7a8c65b8 100644
--- a/core/breaker/googlebreaker_test.go
+++ b/core/breaker/googlebreaker_test.go
@@ -95,7 +95,7 @@ func TestGoogleBreakerAcceptable(t *testing.T) {
 	assert.Equal(t, errAcceptable, b.doReq(func() error {
 		return errAcceptable
 	}, 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 {
 		return errAcceptable
 	}, nil, func(err error) bool {
-		return err != errAcceptable
+		return !errors.Is(err, errAcceptable)
 	}))
 }