cleanup: remove fmt and errors new err (#2377)

* cleanup: remove fmt and errors new err

* fix lint

* fix style

* modify global variable
pull/2408/head
jesse.tang 2 years ago committed by GitHub
parent 2936b5ba80
commit 0e698b248a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      app_test.go
  2. 5
      contrib/registry/nacos/registry.go
  3. 4
      middleware/circuitbreaker/circuitbreaker_test.go
  4. 8
      middleware/validate/validate_test.go
  5. 4
      transport/grpc/resolver/direct/builder_test.go
  6. 4
      transport/http/client_test.go
  7. 3
      transport/http/context_test.go
  8. 4
      transport/http/resolver_test.go
  9. 20
      transport/http/server_test.go

@ -2,7 +2,7 @@ package kratos
import ( import (
"context" "context"
"fmt" "errors"
"net/url" "net/url"
"reflect" "reflect"
"sync" "sync"
@ -21,7 +21,7 @@ type mockRegistry struct {
func (r *mockRegistry) Register(ctx context.Context, service *registry.ServiceInstance) error { func (r *mockRegistry) Register(ctx context.Context, service *registry.ServiceInstance) error {
if service == nil || service.ID == "" { if service == nil || service.ID == "" {
return fmt.Errorf("no service id") return errors.New("no service id")
} }
r.lk.Lock() r.lk.Lock()
defer r.lk.Unlock() defer r.lk.Unlock()
@ -34,7 +34,7 @@ func (r *mockRegistry) Deregister(ctx context.Context, service *registry.Service
r.lk.Lock() r.lk.Lock()
defer r.lk.Unlock() defer r.lk.Unlock()
if r.service[service.ID] == nil { 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) delete(r.service, service.ID)
return nil return nil

@ -2,6 +2,7 @@ package nacos
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"net" "net"
"net/url" "net/url"
@ -14,6 +15,8 @@ import (
"github.com/go-kratos/kratos/v2/registry" "github.com/go-kratos/kratos/v2/registry"
) )
var ErrServiceInstanceNameEmpty = errors.New("kratos/nacos: ServiceInstance.Name can not be empty")
var ( var (
_ registry.Registrar = (*Registry)(nil) _ registry.Registrar = (*Registry)(nil)
_ registry.Discovery = (*Registry)(nil) _ registry.Discovery = (*Registry)(nil)
@ -82,7 +85,7 @@ func New(cli naming_client.INamingClient, opts ...Option) (r *Registry) {
// Register the registration. // Register the registration.
func (r *Registry) Register(_ context.Context, si *registry.ServiceInstance) error { func (r *Registry) Register(_ context.Context, si *registry.ServiceInstance) error {
if si.Name == "" { if si.Name == "" {
return fmt.Errorf("kratos/nacos: serviceInstance.name can not be empty") return ErrServiceInstanceNameEmpty
} }
for _, endpoint := range si.Endpoints { for _, endpoint := range si.Endpoints {
u, err := url.Parse(endpoint) u, err := url.Parse(endpoint)

@ -5,7 +5,7 @@ import (
"errors" "errors"
"testing" "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/internal/group"
"github.com/go-kratos/kratos/v2/transport" "github.com/go-kratos/kratos/v2/transport"
) )
@ -62,7 +62,7 @@ func Test_Server(t *testing.T) {
return "Hello valid", nil return "Hello valid", nil
} }
nextInvalid := func(ctx context.Context, req interface{}) (interface{}, error) { nextInvalid := func(ctx context.Context, req interface{}) (interface{}, error) {
return nil, kratos_errors.InternalServer("", "") return nil, kratoserrors.InternalServer("", "")
} }
ctx := transport.NewClientContext(context.Background(), &transportMock{}) ctx := transport.NewClientContext(context.Background(), &transportMock{})

@ -2,10 +2,10 @@ package validate
import ( import (
"context" "context"
"fmt" "errors"
"testing" "testing"
"github.com/go-kratos/kratos/v2/errors" kratoserrors "github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/middleware" "github.com/go-kratos/kratos/v2/middleware"
) )
@ -18,7 +18,7 @@ type protoVali struct {
func (v protoVali) Validate() error { func (v protoVali) Validate() error {
if v.name == "" || v.age < 0 { if v.name == "" || v.age < 0 {
return fmt.Errorf("err") return errors.New("err")
} }
return nil return nil
} }
@ -35,7 +35,7 @@ func TestTable(t *testing.T) {
t.Run(test.name, func(t *testing.T) { t.Run(test.name, func(t *testing.T) {
v := Validator()(mock) v := Validator()(mock)
_, err := v(context.Background(), test) _, 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) t.Errorf("fail data %v, want %v, have %v", test, want, have)
} }
}) })

@ -1,7 +1,7 @@
package direct package direct
import ( import (
"fmt" "errors"
"reflect" "reflect"
"testing" "testing"
@ -22,7 +22,7 @@ type mockConn struct {
func (m *mockConn) UpdateState(resolver.State) error { func (m *mockConn) UpdateState(resolver.State) error {
if m.needUpdateStateErr { if m.needUpdateStateErr {
return fmt.Errorf("mock test needUpdateStateErr") return errors.New("mock test needUpdateStateErr")
} }
return nil return nil
} }

@ -33,7 +33,7 @@ type mockCallOption struct {
func (x *mockCallOption) before(info *callInfo) error { func (x *mockCallOption) before(info *callInfo) error {
if x.needErr { if x.needErr {
return fmt.Errorf("option need return err") return errors.New("option need return err")
} }
return nil return nil
} }
@ -357,7 +357,7 @@ func TestNewClient(t *testing.T) {
t.Error("err should be equal to callOption err") t.Error("err should be equal to callOption err")
} }
client.opts.encoder = func(ctx context.Context, contentType string, in interface{}) (body []byte, err error) { 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{}) err = client.Invoke(context.Background(), "POST", "/go", map[string]string{"name": "kratos"}, nil, EmptyCallOption{})
if err == nil { if err == nil {

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"context" "context"
"errors" "errors"
"fmt"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
@ -92,7 +91,7 @@ func TestContextResponse(t *testing.T) {
if err != nil { if err != nil {
t.Errorf("expected %v, got %v", nil, err) 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) err = w.Returns(map[string]string{}, needErr)
if !errors.Is(err, needErr) { if !errors.Is(err, needErr) {
t.Errorf("expected %v, got %v", needErr, err) t.Errorf("expected %v, got %v", needErr, err)

@ -73,7 +73,7 @@ const errServiceName = "needErr"
func (d *mockDiscoveries) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) { func (d *mockDiscoveries) Watch(ctx context.Context, serviceName string) (registry.Watcher, error) {
if serviceName == errServiceName { 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 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 { func (m *mockWatch) Stop() error {
if m.stopErr { if m.stopErr {
return fmt.Errorf("mock test error") return errors.New("mock test error")
} }
// 标记 next 需要报错 // 标记 next 需要报错
m.nextErr = true m.nextErr = true

@ -4,6 +4,7 @@ import (
"context" "context"
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io" "io"
"net" "net"
@ -13,8 +14,7 @@ import (
"testing" "testing"
"time" "time"
"github.com/go-kratos/kratos/v2/errors" kratoserrors "github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/internal/host" "github.com/go-kratos/kratos/v2/internal/host"
) )
@ -49,9 +49,9 @@ func TestServeHTTP(t *testing.T) {
mux := NewServer(Listener(ln)) mux := NewServer(Listener(ln))
mux.HandleFunc("/index", h) mux.HandleFunc("/index", h)
mux.Route("/errors").GET("/cause", func(ctx Context) error { 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"}). WithMetadata(map[string]string{"foo": "bar"}).
WithCause(fmt.Errorf("error cause")) WithCause(errors.New("error cause"))
}) })
if err = mux.WalkRoute(func(r RouteInfo) error { if err = mux.WalkRoute(func(r RouteInfo) error {
t.Logf("WalkRoute: %+v", r) t.Logf("WalkRoute: %+v", r)
@ -65,7 +65,7 @@ func TestServeHTTP(t *testing.T) {
srv := http.Server{Handler: mux} srv := http.Server{Handler: mux}
go func() { go func() {
if err := srv.Serve(ln); err != nil { if err := srv.Serve(ln); err != nil {
if errors.Is(err, http.ErrServerClosed) { if kratoserrors.Is(err, http.ErrServerClosed) {
return return
} }
panic(err) panic(err)
@ -87,9 +87,9 @@ func TestServer(t *testing.T) {
_ = json.NewEncoder(w).Encode(testData{Path: r.RequestURI}) _ = json.NewEncoder(w).Encode(testData{Path: r.RequestURI})
}) })
srv.Route("/errors").GET("/cause", func(ctx Context) error { 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"}). 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") { 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) req.Header.Set("Content-Type", test.contentType)
resp, err := client.Do(req) resp, err := client.Do(req)
if errors.Code(err) != 400 { if kratoserrors.Code(err) != 400 {
t.Errorf("expected 400 got %v", err) t.Errorf("expected 400 got %v", err)
} }
if err == nil { if err == nil {
@ -205,7 +205,7 @@ func testClient(t *testing.T, srv *Server) {
t.Fatal(err) t.Fatal(err)
} }
resp, err := client.Do(req) 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) t.Fatalf("want %v, but got %v", test, err)
} }
if err != nil { if err != nil {
@ -231,7 +231,7 @@ func testClient(t *testing.T, srv *Server) {
for _, test := range tests { for _, test := range tests {
var res testData var res testData
err := client.Invoke(context.Background(), test.method, test.path, nil, &res) 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) t.Fatalf("want %v, but got %v", test, err)
} }
if err != nil { if err != nil {

Loading…
Cancel
Save