diff --git a/app_test.go b/app_test.go index aa0a5ef72..2b8c8c9d2 100644 --- a/app_test.go +++ b/app_test.go @@ -2,7 +2,7 @@ package kratos import ( "context" - "fmt" + "errors" "net/url" "reflect" "sync" @@ -21,7 +21,7 @@ type mockRegistry struct { func (r *mockRegistry) Register(ctx context.Context, service *registry.ServiceInstance) error { if service == nil || service.ID == "" { - return fmt.Errorf("no service id") + return errors.New("no service id") } r.lk.Lock() defer r.lk.Unlock() @@ -34,7 +34,7 @@ func (r *mockRegistry) Deregister(ctx context.Context, service *registry.Service r.lk.Lock() defer r.lk.Unlock() if r.service[service.ID] == nil { - return fmt.Errorf("deregister service not found") + return errors.New("deregister service not found") } delete(r.service, service.ID) return nil diff --git a/contrib/registry/nacos/registry.go b/contrib/registry/nacos/registry.go index 088cb8c28..1a3722b84 100644 --- a/contrib/registry/nacos/registry.go +++ b/contrib/registry/nacos/registry.go @@ -2,6 +2,7 @@ package nacos import ( "context" + "errors" "fmt" "net" "net/url" @@ -14,6 +15,8 @@ import ( "github.com/go-kratos/kratos/v2/registry" ) +var ErrServiceInstanceNameEmpty = errors.New("kratos/nacos: ServiceInstance.Name can not be empty") + var ( _ registry.Registrar = (*Registry)(nil) _ registry.Discovery = (*Registry)(nil) @@ -82,7 +85,7 @@ func New(cli naming_client.INamingClient, opts ...Option) (r *Registry) { // Register the registration. func (r *Registry) Register(_ context.Context, si *registry.ServiceInstance) error { if si.Name == "" { - return fmt.Errorf("kratos/nacos: serviceInstance.name can not be empty") + return ErrServiceInstanceNameEmpty } for _, endpoint := range si.Endpoints { u, err := url.Parse(endpoint) diff --git a/middleware/circuitbreaker/circuitbreaker_test.go b/middleware/circuitbreaker/circuitbreaker_test.go index b7c2553ce..611419c78 100644 --- a/middleware/circuitbreaker/circuitbreaker_test.go +++ b/middleware/circuitbreaker/circuitbreaker_test.go @@ -5,7 +5,7 @@ import ( "errors" "testing" - kratos_errors "github.com/go-kratos/kratos/v2/errors" + kratoserrors "github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/internal/group" "github.com/go-kratos/kratos/v2/transport" ) @@ -62,7 +62,7 @@ func Test_Server(t *testing.T) { return "Hello valid", nil } nextInvalid := func(ctx context.Context, req interface{}) (interface{}, error) { - return nil, kratos_errors.InternalServer("", "") + return nil, kratoserrors.InternalServer("", "") } ctx := transport.NewClientContext(context.Background(), &transportMock{}) diff --git a/middleware/validate/validate_test.go b/middleware/validate/validate_test.go index f1597820a..34fc33497 100644 --- a/middleware/validate/validate_test.go +++ b/middleware/validate/validate_test.go @@ -2,10 +2,10 @@ package validate import ( "context" - "fmt" + "errors" "testing" - "github.com/go-kratos/kratos/v2/errors" + kratoserrors "github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/middleware" ) @@ -18,7 +18,7 @@ type protoVali struct { func (v protoVali) Validate() error { if v.name == "" || v.age < 0 { - return fmt.Errorf("err") + return errors.New("err") } return nil } @@ -35,7 +35,7 @@ func TestTable(t *testing.T) { t.Run(test.name, func(t *testing.T) { v := Validator()(mock) _, err := v(context.Background(), test) - if want, have := test.isErr, errors.IsBadRequest(err); want != have { + if want, have := test.isErr, kratoserrors.IsBadRequest(err); want != have { t.Errorf("fail data %v, want %v, have %v", test, want, have) } }) diff --git a/transport/grpc/resolver/direct/builder_test.go b/transport/grpc/resolver/direct/builder_test.go index 1cd6bf402..82cd8cd1f 100644 --- a/transport/grpc/resolver/direct/builder_test.go +++ b/transport/grpc/resolver/direct/builder_test.go @@ -1,7 +1,7 @@ package direct import ( - "fmt" + "errors" "reflect" "testing" @@ -22,7 +22,7 @@ type mockConn struct { func (m *mockConn) UpdateState(resolver.State) error { if m.needUpdateStateErr { - return fmt.Errorf("mock test needUpdateStateErr") + return errors.New("mock test needUpdateStateErr") } return nil } diff --git a/transport/http/client_test.go b/transport/http/client_test.go index 4ee00456f..ab8031dd5 100644 --- a/transport/http/client_test.go +++ b/transport/http/client_test.go @@ -33,7 +33,7 @@ type mockCallOption struct { func (x *mockCallOption) before(info *callInfo) error { if x.needErr { - return fmt.Errorf("option need return err") + return errors.New("option need return err") } return nil } @@ -357,7 +357,7 @@ func TestNewClient(t *testing.T) { t.Error("err should be equal to callOption err") } client.opts.encoder = func(ctx context.Context, contentType string, in interface{}) (body []byte, err error) { - return nil, fmt.Errorf("mock test encoder error") + return nil, errors.New("mock test encoder error") } err = client.Invoke(context.Background(), "POST", "/go", map[string]string{"name": "kratos"}, nil, EmptyCallOption{}) if err == nil { diff --git a/transport/http/context_test.go b/transport/http/context_test.go index 3b7466bf0..b440e9df7 100644 --- a/transport/http/context_test.go +++ b/transport/http/context_test.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "errors" - "fmt" "net/http" "net/http/httptest" "net/url" @@ -92,7 +91,7 @@ func TestContextResponse(t *testing.T) { if err != nil { t.Errorf("expected %v, got %v", nil, err) } - needErr := fmt.Errorf("some error") + needErr := errors.New("some error") err = w.Returns(map[string]string{}, needErr) if !errors.Is(err, needErr) { t.Errorf("expected %v, got %v", needErr, err) diff --git a/transport/http/resolver_test.go b/transport/http/resolver_test.go index 27473d12b..e95d8850b 100644 --- a/transport/http/resolver_test.go +++ b/transport/http/resolver_test.go @@ -73,7 +73,7 @@ const errServiceName = "needErr" func (d *mockDiscoveries) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) { if serviceName == errServiceName { - return nil, fmt.Errorf("mock test service name watch err") + return nil, errors.New("mock test service name watch err") } return &mockWatch{ctx: ctx, isSecure: d.isSecure, nextErr: d.nextErr, stopErr: d.stopErr}, nil } @@ -116,7 +116,7 @@ func (m *mockWatch) Next() ([]*registry.ServiceInstance, error) { func (m *mockWatch) Stop() error { if m.stopErr { - return fmt.Errorf("mock test error") + return errors.New("mock test error") } // 标记 next 需要报错 m.nextErr = true diff --git a/transport/http/server_test.go b/transport/http/server_test.go index 05bc8399e..0d4be88bd 100644 --- a/transport/http/server_test.go +++ b/transport/http/server_test.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "encoding/json" + "errors" "fmt" "io" "net" @@ -13,8 +14,7 @@ import ( "testing" "time" - "github.com/go-kratos/kratos/v2/errors" - + kratoserrors "github.com/go-kratos/kratos/v2/errors" "github.com/go-kratos/kratos/v2/internal/host" ) @@ -49,9 +49,9 @@ func TestServeHTTP(t *testing.T) { mux := NewServer(Listener(ln)) mux.HandleFunc("/index", h) mux.Route("/errors").GET("/cause", func(ctx Context) error { - return errors.BadRequest("xxx", "zzz"). + return kratoserrors.BadRequest("xxx", "zzz"). WithMetadata(map[string]string{"foo": "bar"}). - WithCause(fmt.Errorf("error cause")) + WithCause(errors.New("error cause")) }) if err = mux.WalkRoute(func(r RouteInfo) error { t.Logf("WalkRoute: %+v", r) @@ -65,7 +65,7 @@ func TestServeHTTP(t *testing.T) { srv := http.Server{Handler: mux} go func() { if err := srv.Serve(ln); err != nil { - if errors.Is(err, http.ErrServerClosed) { + if kratoserrors.Is(err, http.ErrServerClosed) { return } panic(err) @@ -87,9 +87,9 @@ func TestServer(t *testing.T) { _ = json.NewEncoder(w).Encode(testData{Path: r.RequestURI}) }) srv.Route("/errors").GET("/cause", func(ctx Context) error { - return errors.BadRequest("xxx", "zzz"). + return kratoserrors.BadRequest("xxx", "zzz"). WithMetadata(map[string]string{"foo": "bar"}). - WithCause(fmt.Errorf("error cause")) + WithCause(errors.New("error cause")) }) if e, err := srv.Endpoint(); err != nil || e == nil || strings.HasSuffix(e.Host, ":0") { @@ -135,7 +135,7 @@ func testAccept(t *testing.T, srv *Server) { } req.Header.Set("Content-Type", test.contentType) resp, err := client.Do(req) - if errors.Code(err) != 400 { + if kratoserrors.Code(err) != 400 { t.Errorf("expected 400 got %v", err) } if err == nil { @@ -205,7 +205,7 @@ func testClient(t *testing.T, srv *Server) { t.Fatal(err) } resp, err := client.Do(req) - if errors.Code(err) != test.code { + if kratoserrors.Code(err) != test.code { t.Fatalf("want %v, but got %v", test, err) } if err != nil { @@ -231,7 +231,7 @@ func testClient(t *testing.T, srv *Server) { for _, test := range tests { var res testData err := client.Invoke(context.Background(), test.method, test.path, nil, &res) - if errors.Code(err) != test.code { + if kratoserrors.Code(err) != test.code { t.Fatalf("want %v, but got %v", test, err) } if err != nil {