diff --git a/cluster/cluster_impl/base_cluster_invoker.go b/cluster/cluster_impl/base_cluster_invoker.go index 2bfc10d8977b3087d677fab6937aa29196e880aa..a270bef4580c21f2711e94eb090e744ed79feeed 100644 --- a/cluster/cluster_impl/base_cluster_invoker.go +++ b/cluster/cluster_impl/base_cluster_invoker.go @@ -15,8 +15,8 @@ package cluster_impl import ( - gxnet "github.com/AlexStocks/goext/net" - jerrors "github.com/juju/errors" + "github.com/AlexStocks/goext/net" + "github.com/pkg/errors" "go.uber.org/atomic" ) @@ -60,7 +60,7 @@ func (invoker *baseClusterInvoker) IsAvailable() bool { func (invoker *baseClusterInvoker) checkInvokers(invokers []protocol.Invoker, invocation protocol.Invocation) error { if len(invokers) == 0 { ip, _ := gxnet.GetLocalIP() - return jerrors.Errorf("Failed to invoke the method %v . No provider available for the service %v from "+ + return errors.Errorf("Failed to invoke the method %v. No provider available for the service %v from "+ "registry %v on the consumer %v using the dubbo version %v .Please check if the providers have been started and registered.", invocation.MethodName(), invoker.directory.GetUrl().SubURL.Key(), invoker.directory.GetUrl().String(), ip, version.Version) } @@ -72,7 +72,7 @@ func (invoker *baseClusterInvoker) checkInvokers(invokers []protocol.Invoker, in func (invoker *baseClusterInvoker) checkWhetherDestroyed() error { if invoker.destroyed.Load() { ip, _ := gxnet.GetLocalIP() - return jerrors.Errorf("Rpc cluster invoker for %v on consumer %v use dubbo version %v is now destroyed! can not invoke any more. ", + return errors.Errorf("Rpc cluster invoker for %v on consumer %v use dubbo version %v is now destroyed! can not invoke any more. ", invoker.directory.GetUrl().Service(), ip, version.Version) } return nil diff --git a/cluster/cluster_impl/failover_cluster_invoker.go b/cluster/cluster_impl/failover_cluster_invoker.go index 91b48c595d938bfbfdf264f01c47372de4ab3741..a670817011b9547c2c325c6deda65227a1afe2d1 100644 --- a/cluster/cluster_impl/failover_cluster_invoker.go +++ b/cluster/cluster_impl/failover_cluster_invoker.go @@ -15,9 +15,10 @@ package cluster_impl import ( - gxnet "github.com/AlexStocks/goext/net" - jerrors "github.com/juju/errors" + "github.com/AlexStocks/goext/net" + "github.com/pkg/errors" ) + import ( "github.com/dubbo/go-for-apache-dubbo/cluster" "github.com/dubbo/go-for-apache-dubbo/common/constant" @@ -92,7 +93,7 @@ func (invoker *failoverClusterInvoker) Invoke(invocation protocol.Invocation) pr } } ip, _ := gxnet.GetLocalIP() - return &protocol.RPCResult{Err: jerrors.Errorf("Failed to invoke the method %v in the service %v . Tried %v times of "+ + return &protocol.RPCResult{Err: errors.Errorf("Failed to invoke the method %v in the service %v . Tried %v times of "+ "the providers %v (%v/%v)from the registry %v on the consumer %v using the dubbo version %v. Last error is %v.", methodName, invoker.GetUrl().Service(), retries, providers, len(providers), len(invokers), invoker.directory.GetUrl(), ip, version.Version, result.Error().Error(), )} diff --git a/cluster/cluster_impl/failover_cluster_test.go b/cluster/cluster_impl/failover_cluster_test.go index 9e52e02d661361421bd91d5863854e8e1a684893..6744e821028415049ec4095e94b814163c88b2ac 100644 --- a/cluster/cluster_impl/failover_cluster_test.go +++ b/cluster/cluster_impl/failover_cluster_test.go @@ -16,13 +16,13 @@ package cluster_impl import ( "context" - "errors" "fmt" "net/url" "testing" ) import ( log "github.com/AlexStocks/log4go" + "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) diff --git a/cluster/directory/static_directory_test.go b/cluster/directory/static_directory_test.go index 2f99aa8c09c8e898536e5df9995900f624914d85..929d23edefd3821ae8f6c48a3706f6036b9238d8 100644 --- a/cluster/directory/static_directory_test.go +++ b/cluster/directory/static_directory_test.go @@ -17,13 +17,17 @@ package directory import ( "context" "fmt" - "github.com/dubbo/go-for-apache-dubbo/protocol/invocation" - "github.com/stretchr/testify/assert" "testing" ) + +import ( + "github.com/stretchr/testify/assert" +) + import ( "github.com/dubbo/go-for-apache-dubbo/common" "github.com/dubbo/go-for-apache-dubbo/protocol" + "github.com/dubbo/go-for-apache-dubbo/protocol/invocation" ) func Test_StaticDirList(t *testing.T) { diff --git a/cluster/loadbalance/random_test.go b/cluster/loadbalance/random_test.go index fd1a2d2e24ca45fe6bae8f69f716b578eba038e1..92244a1ce6b49602bcdaf05a1a8fcdec3609e34f 100644 --- a/cluster/loadbalance/random_test.go +++ b/cluster/loadbalance/random_test.go @@ -17,17 +17,21 @@ package loadbalance import ( "context" "fmt" - "github.com/dubbo/go-for-apache-dubbo/common/constant" - "github.com/dubbo/go-for-apache-dubbo/protocol/invocation" - "github.com/stretchr/testify/assert" "net/url" "strconv" "testing" "time" ) + +import ( + "github.com/stretchr/testify/assert" +) + import ( "github.com/dubbo/go-for-apache-dubbo/common" + "github.com/dubbo/go-for-apache-dubbo/common/constant" "github.com/dubbo/go-for-apache-dubbo/protocol" + "github.com/dubbo/go-for-apache-dubbo/protocol/invocation" ) func Test_RandomlbSelect(t *testing.T) { diff --git a/common/proxy/proxy_test.go b/common/proxy/proxy_test.go index 252626e78cf0e53abb6995e66c2adade17818104..ec3df722703f16a957a1a6eec90eb19f326937dd 100644 --- a/common/proxy/proxy_test.go +++ b/common/proxy/proxy_test.go @@ -16,12 +16,12 @@ package proxy import ( "context" - "errors" "reflect" "testing" ) import ( + "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) diff --git a/common/rpc_service.go b/common/rpc_service.go index 02c83ee95add65e29f55dc588f9922ab66a8bdf1..4c71fec46a1dd0aee664f95b6a03729c4e1f8efd 100644 --- a/common/rpc_service.go +++ b/common/rpc_service.go @@ -16,7 +16,6 @@ package common import ( "context" - "errors" "reflect" "strings" "sync" @@ -26,7 +25,7 @@ import ( import ( log "github.com/AlexStocks/log4go" - jerrors "github.com/juju/errors" + "github.com/pkg/errors" ) // rpc service interface @@ -130,17 +129,17 @@ func (sm *serviceMap) Register(protocol string, rcvr RPCService) (string, error) if sname == "" { s := "no service name for type " + s.rcvrType.String() log.Error(s) - return "", jerrors.New(s) + return "", errors.New(s) } if !isExported(sname) { s := "type " + sname + " is not exported" log.Error(s) - return "", jerrors.New(s) + return "", errors.New(s) } sname = rcvr.Service() if server := sm.GetService(protocol, sname); server != nil { - return "", jerrors.New("service already defined: " + sname) + return "", errors.New("service already defined: " + sname) } s.name = sname s.methods = make(map[string]*MethodType) @@ -152,7 +151,7 @@ func (sm *serviceMap) Register(protocol string, rcvr RPCService) (string, error) if len(s.methods) == 0 { s := "type " + sname + " has no exported methods of suitable type" log.Error(s) - return "", jerrors.New(s) + return "", errors.New(s) } sm.mutex.Lock() sm.serviceMap[protocol][s.name] = s diff --git a/common/url.go b/common/url.go index 5e33a8d6ba48c1574ae5fd086e94eee1753f3b7a..960bf3dd53411bae0b03e4229107b2f11a169a74 100644 --- a/common/url.go +++ b/common/url.go @@ -24,8 +24,9 @@ import ( ) import ( - jerrors "github.com/juju/errors" + "github.com/pkg/errors" ) + import ( "github.com/dubbo/go-for-apache-dubbo/common/constant" ) @@ -156,18 +157,18 @@ func NewURL(ctx context.Context, urlString string, opts ...option) (URL, error) rawUrlString, err = url.QueryUnescape(urlString) if err != nil { - return s, jerrors.Errorf("url.QueryUnescape(%s), error{%v}", urlString, err) + return s, errors.Errorf("url.QueryUnescape(%s), error{%v}", urlString, err) } //rawUrlString = "//" + rawUrlString serviceUrl, err = url.Parse(rawUrlString) if err != nil { - return s, jerrors.Errorf("url.Parse(url string{%s}), error{%v}", rawUrlString, err) + return s, errors.Errorf("url.Parse(url string{%s}), error{%v}", rawUrlString, err) } s.Params, err = url.ParseQuery(serviceUrl.RawQuery) if err != nil { - return s, jerrors.Errorf("url.ParseQuery(raw url string{%s}), error{%v}", serviceUrl.RawQuery, err) + return s, errors.Errorf("url.ParseQuery(raw url string{%s}), error{%v}", serviceUrl.RawQuery, err) } s.PrimitiveURL = urlString @@ -179,7 +180,7 @@ func NewURL(ctx context.Context, urlString string, opts ...option) (URL, error) if strings.Contains(s.Location, ":") { s.Ip, s.Port, err = net.SplitHostPort(s.Location) if err != nil { - return s, jerrors.Errorf("net.SplitHostPort(Url.Host{%s}), error{%v}", s.Location, err) + return s, errors.Errorf("net.SplitHostPort(Url.Host{%s}), error{%v}", s.Location, err) } } // diff --git a/config/config_loader.go b/config/config_loader.go index b4e854c58431ea4f37c4f309bcf439d8b7a41582..537f5172739d6cc6714a6eedf84c6bed07400bfd 100644 --- a/config/config_loader.go +++ b/config/config_loader.go @@ -26,7 +26,7 @@ import ( import ( "github.com/AlexStocks/goext/log" log "github.com/AlexStocks/log4go" - jerrors "github.com/juju/errors" + "github.com/pkg/errors" "gopkg.in/yaml.v2" ) @@ -74,10 +74,10 @@ func logInit() error { confFile = os.Getenv(constant.APP_LOG_CONF_FILE) if confFile == "" { - return fmt.Errorf("log configure file name is nil") + return errors.Errorf("log configure file name is nil") } if path.Ext(confFile) != ".xml" { - return fmt.Errorf("log configure file name{%v} suffix must be .xml", confFile) + return errors.Errorf("log configure file name{%v} suffix must be .xml", confFile) } log.LoadConfiguration(confFile) @@ -87,28 +87,28 @@ func logInit() error { func consumerInit(confConFile string) error { if confConFile == "" { - return fmt.Errorf("application configure(consumer) file name is nil") + return errors.Errorf("application configure(consumer) file name is nil") } if path.Ext(confConFile) != ".yml" { - return fmt.Errorf("application configure file name{%v} suffix must be .yml", confConFile) + return errors.Errorf("application configure file name{%v} suffix must be .yml", confConFile) } confFileStream, err := ioutil.ReadFile(confConFile) if err != nil { - return fmt.Errorf("ioutil.ReadFile(file:%s) = error:%s", confConFile, jerrors.ErrorStack(err)) + return errors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confConFile, errors.Cause(err)) } consumerConfig = &ConsumerConfig{} err = yaml.Unmarshal(confFileStream, consumerConfig) if err != nil { - return fmt.Errorf("yaml.Unmarshal() = error:%s", jerrors.ErrorStack(err)) + return errors.Errorf("yaml.Unmarshal() = error:%v", errors.Cause(err)) } if consumerConfig.RequestTimeout, err = time.ParseDuration(consumerConfig.Request_Timeout); err != nil { - return jerrors.Annotatef(err, "time.ParseDuration(Request_Timeout{%#v})", consumerConfig.Request_Timeout) + return errors.WithMessagef(err, "time.ParseDuration(Request_Timeout{%#v})", consumerConfig.Request_Timeout) } if consumerConfig.ConnectTimeout, err = time.ParseDuration(consumerConfig.Connect_Timeout); err != nil { - return jerrors.Annotatef(err, "time.ParseDuration(Connect_Timeout{%#v})", consumerConfig.Connect_Timeout) + return errors.WithMessagef(err, "time.ParseDuration(Connect_Timeout{%#v})", consumerConfig.Connect_Timeout) } gxlog.CInfo("consumer config{%#v}\n", consumerConfig) @@ -117,21 +117,21 @@ func consumerInit(confConFile string) error { func providerInit(confProFile string) error { if confProFile == "" { - return fmt.Errorf("application configure(provider) file name is nil") + return errors.Errorf("application configure(provider) file name is nil") } if path.Ext(confProFile) != ".yml" { - return fmt.Errorf("application configure file name{%v} suffix must be .yml", confProFile) + return errors.Errorf("application configure file name{%v} suffix must be .yml", confProFile) } confFileStream, err := ioutil.ReadFile(confProFile) if err != nil { - return fmt.Errorf("ioutil.ReadFile(file:%s) = error:%s", confProFile, jerrors.ErrorStack(err)) + return errors.Errorf("ioutil.ReadFile(file:%s) = error:%v", confProFile, errors.Cause(err)) } providerConfig = &ProviderConfig{} err = yaml.Unmarshal(confFileStream, providerConfig) if err != nil { - return fmt.Errorf("yaml.Unmarshal() = error:%s", jerrors.ErrorStack(err)) + return errors.Errorf("yaml.Unmarshal() = error:%v", errors.Cause(err)) } gxlog.CInfo("provider config{%#v}\n", providerConfig) diff --git a/config/config_loader_test.go b/config/config_loader_test.go index bf7aa86abadddb1fa0e1106c043916ff1a1c942d..4496671bd5e7d803402c3e954491255e1be337c1 100644 --- a/config/config_loader_test.go +++ b/config/config_loader_test.go @@ -15,7 +15,6 @@ package config import ( - "github.com/dubbo/go-for-apache-dubbo/common" "path/filepath" "testing" ) @@ -26,6 +25,7 @@ import ( import ( "github.com/dubbo/go-for-apache-dubbo/cluster/cluster_impl" + "github.com/dubbo/go-for-apache-dubbo/common" "github.com/dubbo/go-for-apache-dubbo/common/extension" ) diff --git a/config/reference_config_test.go b/config/reference_config_test.go index ce613cda246e150675e9ef8c377e0c3cdb001447..8d1c718f830923a63b79439cd0ba865d23e9ddca 100644 --- a/config/reference_config_test.go +++ b/config/reference_config_test.go @@ -14,14 +14,20 @@ package config +import ( + "sync" + "testing" +) + +import ( + "github.com/stretchr/testify/assert" +) + import ( "github.com/dubbo/go-for-apache-dubbo/cluster/cluster_impl" "github.com/dubbo/go-for-apache-dubbo/common" "github.com/dubbo/go-for-apache-dubbo/common/extension" "github.com/dubbo/go-for-apache-dubbo/protocol" - "github.com/stretchr/testify/assert" - "sync" - "testing" ) var regProtocol protocol.Protocol diff --git a/config/service_config.go b/config/service_config.go index 259a8d6b34ee5b630852e681c0fe325335e8f1c8..40f15e992cf44e01e3868b203776872a76286c29 100644 --- a/config/service_config.go +++ b/config/service_config.go @@ -24,7 +24,7 @@ import ( ) import ( log "github.com/AlexStocks/log4go" - jerrors "github.com/juju/errors" + "github.com/pkg/errors" "go.uber.org/atomic" ) import ( @@ -72,7 +72,7 @@ func (srvconfig *ServiceConfig) Export() error { //TODO:delay export if srvconfig.unexported != nil && srvconfig.unexported.Load() { - err := jerrors.Errorf("The service %v has already unexported! ", srvconfig.InterfaceName) + err := errors.Errorf("The service %v has already unexported! ", srvconfig.InterfaceName) log.Error(err.Error()) return err } @@ -88,7 +88,7 @@ func (srvconfig *ServiceConfig) Export() error { //registry the service reflect methods, err := common.ServiceMap.Register(proto.Name, srvconfig.rpcService) if err != nil { - err := jerrors.Errorf("The service %v export the protocol %v error! Error message is %v .", srvconfig.InterfaceName, proto.Name, err.Error()) + err := errors.Errorf("The service %v export the protocol %v error! Error message is %v .", srvconfig.InterfaceName, proto.Name, err.Error()) log.Error(err.Error()) return err } @@ -114,7 +114,7 @@ func (srvconfig *ServiceConfig) Export() error { srvconfig.cacheMutex.Unlock() exporter := srvconfig.cacheProtocol.Export(invoker) if exporter == nil { - panic(jerrors.New("New exporter error")) + panic(errors.New("New exporter error")) } srvconfig.exporters = append(srvconfig.exporters, exporter) } diff --git a/config/service_config_test.go b/config/service_config_test.go index aee735dd79e7aeae75edd7d51bf597fcda1f94e7..bed7cc5b954c21f9700bff6424ac7c73e7f69df3 100644 --- a/config/service_config_test.go +++ b/config/service_config_test.go @@ -15,11 +15,17 @@ package config import ( - "github.com/dubbo/go-for-apache-dubbo/common/extension" - "github.com/stretchr/testify/assert" "testing" ) +import ( + "github.com/stretchr/testify/assert" +) + +import ( + "github.com/dubbo/go-for-apache-dubbo/common/extension" +) + func doinit() { providerConfig = &ProviderConfig{ ApplicationConfig: ApplicationConfig{ diff --git a/go.mod b/go.mod index 37c5d2a3cd99a0ca0e3f956ca8b65d7b1b6a9c7f..d8c111da88fd7a7795411c78d78f6e8a07b930d1 100644 --- a/go.mod +++ b/go.mod @@ -4,8 +4,8 @@ require ( github.com/AlexStocks/getty v0.0.0-20190513203438-4a52b6874223 github.com/AlexStocks/goext v0.3.2 github.com/AlexStocks/log4go v1.0.2 - github.com/dubbogo/hessian2 v0.0.0-20190515104320-57ac2e777dc6 - github.com/juju/errors v0.0.0-20190207033735-e65537c515d7 + github.com/dubbogo/hessian2 v0.0.0-20190521160428-dff5b932f479 + github.com/pkg/errors v0.8.1 github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec github.com/stretchr/testify v1.3.0 go.uber.org/atomic v1.3.2 diff --git a/go.sum b/go.sum index ecf9e2bf55d6c275aaf8cf663fd8cbd18ad74146..a38eef3fa18d2fd975859d041b2501b003f249ca 100644 --- a/go.sum +++ b/go.sum @@ -24,8 +24,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= -github.com/dubbogo/hessian2 v0.0.0-20190515104320-57ac2e777dc6 h1:qFg1/KkHq4W/c5/3sagBNuMT7VjTiMQGIqknIlnPhu0= -github.com/dubbogo/hessian2 v0.0.0-20190515104320-57ac2e777dc6/go.mod h1:v+gfInE8fm/k3Fjkb2oUCKSO9LKbWvf+PtweEI89BmI= +github.com/dubbogo/hessian2 v0.0.0-20190521160428-dff5b932f479 h1:3fJTIjQ2MAIS4QMdTDpsFzplgG+ckB3KKAxmfeoP/es= +github.com/dubbogo/hessian2 v0.0.0-20190521160428-dff5b932f479/go.mod h1:XFGDn4oSZX26zkcfhkM/fCJrOqwQJxk/xgWW1KMJBKM= github.com/fatih/set v0.2.1/go.mod h1:+RKtMCH+favT2+3YecHGxcc0b4KyVWA1QWWJUs4E0CI= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4= diff --git a/protocol/dubbo/client.go b/protocol/dubbo/client.go index 55e1e3cc2a2ccb40e7c009029a926e0be5afc6dc..b11dd686b98ab5a4df3e3812d818de8654c450dd 100644 --- a/protocol/dubbo/client.go +++ b/protocol/dubbo/client.go @@ -25,7 +25,7 @@ import ( "github.com/AlexStocks/goext/sync/atomic" log "github.com/AlexStocks/log4go" "github.com/dubbogo/hessian2" - jerrors "github.com/juju/errors" + "github.com/pkg/errors" "gopkg.in/yaml.v2" ) @@ -36,11 +36,11 @@ import ( ) var ( - errInvalidCodecType = jerrors.New("illegal CodecType") - errInvalidAddress = jerrors.New("remote address invalid or empty") - errSessionNotExist = jerrors.New("session not exist") - errClientClosed = jerrors.New("client closed") - errClientReadTimeout = jerrors.New("client read timeout") + errInvalidCodecType = errors.New("illegal CodecType") + errInvalidAddress = errors.New("remote address invalid or empty") + errSessionNotExist = errors.New("session not exist") + errClientClosed = errors.New("client closed") + errClientReadTimeout = errors.New("client read timeout") clientConf *ClientConfig ) @@ -168,7 +168,7 @@ func (c *Client) CallOneway(addr string, svcUrl common.URL, method string, args o(&copts) } - return jerrors.Trace(c.call(CT_OneWay, addr, svcUrl, method, args, nil, nil, copts)) + return errors.WithStack(c.call(CT_OneWay, addr, svcUrl, method, args, nil, nil, copts)) } // if @reply is nil, the transport layer will get the response without notify the invoker. @@ -184,7 +184,7 @@ func (c *Client) Call(addr string, svcUrl common.URL, method string, args, reply ct = CT_OneWay } - return jerrors.Trace(c.call(ct, addr, svcUrl, method, args, reply, nil, copts)) + return errors.WithStack(c.call(ct, addr, svcUrl, method, args, reply, nil, copts)) } func (c *Client) AsyncCall(addr string, svcUrl common.URL, method string, args interface{}, @@ -195,7 +195,7 @@ func (c *Client) AsyncCall(addr string, svcUrl common.URL, method string, args i o(&copts) } - return jerrors.Trace(c.call(CT_TwoWay, addr, svcUrl, method, args, reply, callback, copts)) + return errors.WithStack(c.call(CT_TwoWay, addr, svcUrl, method, args, reply, callback, copts)) } func (c *Client) call(ct CallType, addr string, svcUrl common.URL, method string, @@ -246,7 +246,7 @@ func (c *Client) call(ct CallType, addr string, svcUrl common.URL, method string defer c.pool.release(conn, err) if err = c.transfer(session, p, rsp, opts); err != nil { - return jerrors.Trace(err) + return errors.WithStack(err) } if ct == CT_OneWay || callback != nil { @@ -261,7 +261,7 @@ func (c *Client) call(ct CallType, addr string, svcUrl common.URL, method string err = rsp.err } - return jerrors.Trace(err) + return errors.WithStack(err) } func (c *Client) Close() { @@ -274,7 +274,7 @@ func (c *Client) Close() { func (c *Client) selectSession(addr string) (*gettyRPCClient, getty.Session, error) { rpcClient, err := c.pool.getGettyRpcClient(DUBBO, addr) if err != nil { - return nil, nil, jerrors.Trace(err) + return nil, nil, errors.WithStack(err) } return rpcClient, rpcClient.selectSession(), nil } @@ -316,7 +316,7 @@ func (c *Client) transfer(session getty.Session, pkg *DubboPackage, rsp.readStart = time.Now() } - return jerrors.Trace(err) + return errors.WithStack(err) } func (c *Client) addPendingResponse(pr *PendingResponse) { diff --git a/protocol/dubbo/client_test.go b/protocol/dubbo/client_test.go index ec49f8cea0480029dcf73eeb16676d9ea7f2021c..b0adcfcdc3eedde078726a5c6996d38485e7d655 100644 --- a/protocol/dubbo/client_test.go +++ b/protocol/dubbo/client_test.go @@ -16,7 +16,6 @@ package dubbo import ( "context" - "errors" "sync" "testing" "time" @@ -24,6 +23,7 @@ import ( import ( "github.com/dubbogo/hessian2" + "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) diff --git a/protocol/dubbo/codec.go b/protocol/dubbo/codec.go index b669082ea9bd20c0c4f3704aa5a497f5a5fee238..b5f12535069d3d73096786d159643cb4246b749d 100644 --- a/protocol/dubbo/codec.go +++ b/protocol/dubbo/codec.go @@ -23,7 +23,7 @@ import ( import ( "github.com/dubbogo/hessian2" - jerrors "github.com/juju/errors" + "github.com/pkg/errors" ) // serial ID @@ -64,7 +64,7 @@ func (p *DubboPackage) Marshal() (*bytes.Buffer, error) { pkg, err := codec.Write(p.Service, p.Header, p.Body) if err != nil { - return nil, jerrors.Trace(err) + return nil, errors.WithStack(err) } return bytes.NewBuffer(pkg), nil @@ -76,7 +76,7 @@ func (p *DubboPackage) Unmarshal(buf *bytes.Buffer, opts ...interface{}) error { // read header err := codec.ReadHeader(&p.Header) if err != nil { - return jerrors.Trace(err) + return errors.WithStack(err) } if len(opts) != 0 { // for client @@ -84,11 +84,11 @@ func (p *DubboPackage) Unmarshal(buf *bytes.Buffer, opts ...interface{}) error { r := client.pendingResponses[SequenceType(p.Header.ID)] if r == nil { - return fmt.Errorf("pendingResponses[%v] = nil", p.Header.ID) + return errors.Errorf("pendingResponses[%v] = nil", p.Header.ID) } p.Body = client.pendingResponses[SequenceType(p.Header.ID)].reply } else { - return fmt.Errorf("opts[0] is not *Client") + return errors.Errorf("opts[0] is not *Client") } } @@ -98,7 +98,7 @@ func (p *DubboPackage) Unmarshal(buf *bytes.Buffer, opts ...interface{}) error { // read body err = codec.ReadBody(p.Body) - return jerrors.Trace(err) + return errors.WithStack(err) } //////////////////////////////////////////// diff --git a/protocol/dubbo/config.go b/protocol/dubbo/config.go index 97f15f75dbe2ba128cbc1dd8cec5f24fda99a191..61e79e5f1851576bcfa772254f2a9f82fcec9427 100644 --- a/protocol/dubbo/config.go +++ b/protocol/dubbo/config.go @@ -19,7 +19,7 @@ import ( ) import ( - jerrors "github.com/juju/errors" + "github.com/pkg/errors" ) type ( @@ -101,19 +101,19 @@ func (c *GettySessionParam) CheckValidity() error { var err error if c.keepAlivePeriod, err = time.ParseDuration(c.KeepAlivePeriod); err != nil { - return jerrors.Annotatef(err, "time.ParseDuration(KeepAlivePeriod{%#v})", c.KeepAlivePeriod) + return errors.WithMessagef(err, "time.ParseDuration(KeepAlivePeriod{%#v})", c.KeepAlivePeriod) } if c.tcpReadTimeout, err = time.ParseDuration(c.TcpReadTimeout); err != nil { - return jerrors.Annotatef(err, "time.ParseDuration(TcpReadTimeout{%#v})", c.TcpReadTimeout) + return errors.WithMessagef(err, "time.ParseDuration(TcpReadTimeout{%#v})", c.TcpReadTimeout) } if c.tcpWriteTimeout, err = time.ParseDuration(c.TcpWriteTimeout); err != nil { - return jerrors.Annotatef(err, "time.ParseDuration(TcpWriteTimeout{%#v})", c.TcpWriteTimeout) + return errors.WithMessagef(err, "time.ParseDuration(TcpWriteTimeout{%#v})", c.TcpWriteTimeout) } if c.waitTimeout, err = time.ParseDuration(c.WaitTimeout); err != nil { - return jerrors.Annotatef(err, "time.ParseDuration(WaitTimeout{%#v})", c.WaitTimeout) + return errors.WithMessagef(err, "time.ParseDuration(WaitTimeout{%#v})", c.WaitTimeout) } return nil @@ -123,30 +123,30 @@ func (c *ClientConfig) CheckValidity() error { var err error if c.heartbeatPeriod, err = time.ParseDuration(c.HeartbeatPeriod); err != nil { - return jerrors.Annotatef(err, "time.ParseDuration(HeartbeatPeroid{%#v})", c.HeartbeatPeriod) + return errors.WithMessagef(err, "time.ParseDuration(HeartbeatPeroid{%#v})", c.HeartbeatPeriod) } if c.sessionTimeout, err = time.ParseDuration(c.SessionTimeout); err != nil { - return jerrors.Annotatef(err, "time.ParseDuration(SessionTimeout{%#v})", c.SessionTimeout) + return errors.WithMessagef(err, "time.ParseDuration(SessionTimeout{%#v})", c.SessionTimeout) } if c.failFastTimeout, err = time.ParseDuration(c.FailFastTimeout); err != nil { - return jerrors.Annotatef(err, "time.ParseDuration(FailFastTimeout{%#v})", c.FailFastTimeout) + return errors.WithMessagef(err, "time.ParseDuration(FailFastTimeout{%#v})", c.FailFastTimeout) } - return jerrors.Trace(c.GettySessionParam.CheckValidity()) + return errors.WithStack(c.GettySessionParam.CheckValidity()) } func (c *ServerConfig) CheckValidity() error { var err error if c.sessionTimeout, err = time.ParseDuration(c.SessionTimeout); err != nil { - return jerrors.Annotatef(err, "time.ParseDuration(SessionTimeout{%#v})", c.SessionTimeout) + return errors.WithMessagef(err, "time.ParseDuration(SessionTimeout{%#v})", c.SessionTimeout) } if c.failFastTimeout, err = time.ParseDuration(c.FailFastTimeout); err != nil { - return jerrors.Annotatef(err, "time.ParseDuration(FailFastTimeout{%#v})", c.FailFastTimeout) + return errors.WithMessagef(err, "time.ParseDuration(FailFastTimeout{%#v})", c.FailFastTimeout) } - return jerrors.Trace(c.GettySessionParam.CheckValidity()) + return errors.WithStack(c.GettySessionParam.CheckValidity()) } diff --git a/protocol/dubbo/dubbo_invoker.go b/protocol/dubbo/dubbo_invoker.go index 92b67ffd8ddb39094d6f40c3455b7eb90864ecf8..a3856ef0c4ca77cc108921cdccc052fd44773c20 100644 --- a/protocol/dubbo/dubbo_invoker.go +++ b/protocol/dubbo/dubbo_invoker.go @@ -15,13 +15,13 @@ package dubbo import ( - "errors" "strconv" "sync" ) import ( log "github.com/AlexStocks/log4go" + "github.com/pkg/errors" ) import ( diff --git a/protocol/dubbo/listener.go b/protocol/dubbo/listener.go index 550626629bc85ec8c9815c439a52b831a6f01a6c..0d4988dd0f1e2daa903c981b8490fabc2742e619 100644 --- a/protocol/dubbo/listener.go +++ b/protocol/dubbo/listener.go @@ -16,7 +16,6 @@ package dubbo import ( "context" - "errors" "reflect" "sync" "time" @@ -26,7 +25,7 @@ import ( "github.com/AlexStocks/getty" log "github.com/AlexStocks/log4go" "github.com/dubbogo/hessian2" - jerrors "github.com/juju/errors" + "github.com/pkg/errors" ) import ( @@ -40,7 +39,7 @@ import ( const WritePkg_Timeout = 5 * time.Second var ( - errTooManySessions = jerrors.New("too many sessions") + errTooManySessions = errors.New("too many sessions") ) type rpcSession struct { @@ -109,8 +108,8 @@ func (h *RpcClientHandler) OnMessage(session getty.Session, pkg interface{}) { func (h *RpcClientHandler) OnCron(session getty.Session) { rpcSession, err := h.conn.getClientRpcSession(session) if err != nil { - log.Error("client.getClientSession(session{%s}) = error{%s}", - session.Stat(), jerrors.ErrorStack(err)) + log.Error("client.getClientSession(session{%s}) = error{%v}", + session.Stat(), errors.Cause(err)) return } if h.conn.pool.rpcClient.conf.sessionTimeout.Nanoseconds() < time.Since(session.GetActive()).Nanoseconds() { @@ -152,7 +151,7 @@ func (h *RpcServerHandler) OnOpen(session getty.Session) error { } h.rwlock.RUnlock() if err != nil { - return jerrors.Trace(err) + return errors.WithStack(err) } log.Info("got session:%s", session.Stat()) @@ -267,8 +266,8 @@ func (h *RpcServerHandler) callService(req *DubboPackage, ctx context.Context) { log.Error("callService panic: %#v", err) req.Body = e.(error) } else if err, ok := e.(string); ok { - log.Error("callService panic: %#v", jerrors.New(err)) - req.Body = jerrors.New(err) + log.Error("callService panic: %#v", errors.New(err)) + req.Body = errors.New(err) } else { log.Error("callService panic: %#v", e) req.Body = e @@ -334,6 +333,6 @@ func (h *RpcServerHandler) reply(session getty.Session, req *DubboPackage, tp he } if err := session.WritePkg(resp, WritePkg_Timeout); err != nil { - log.Error("WritePkg error: %#v, %#v", jerrors.Trace(err), req.Header) + log.Error("WritePkg error: %#v, %#v", errors.WithStack(err), req.Header) } } diff --git a/protocol/dubbo/pool.go b/protocol/dubbo/pool.go index 7f2acdb09612fd58a4b3d0b392a85abb73ffc00c..e75594467c20357b1e815a49e1d40bded4653826 100644 --- a/protocol/dubbo/pool.go +++ b/protocol/dubbo/pool.go @@ -26,7 +26,7 @@ import ( import ( "github.com/AlexStocks/getty" log "github.com/AlexStocks/log4go" - jerrors "github.com/juju/errors" + "github.com/pkg/errors" ) type gettyRPCClient struct { @@ -43,7 +43,7 @@ type gettyRPCClient struct { } var ( - errClientPoolClosed = jerrors.New("client pool closed") + errClientPoolClosed = errors.New("client pool closed") ) func newGettyRPCClientConn(pool *gettyRPCClientPool, protocol, addr string) (*gettyRPCClient, error) { @@ -66,7 +66,7 @@ func newGettyRPCClientConn(pool *gettyRPCClientPool, protocol, addr string) (*ge } if idx > 5000 { - return nil, jerrors.New(fmt.Sprintf("failed to create client connection to %s in 5 seconds", addr)) + return nil, errors.New(fmt.Sprintf("failed to create client connection to %s in 5 seconds", addr)) } time.Sleep(1e6) } @@ -203,7 +203,7 @@ func (c *gettyRPCClient) getClientRpcSession(session getty.Session) (rpcSession, } } - return rpcSession, jerrors.Trace(err) + return rpcSession, errors.WithStack(err) } func (c *gettyRPCClient) isAvailable() bool { @@ -215,7 +215,7 @@ func (c *gettyRPCClient) isAvailable() bool { } func (c *gettyRPCClient) close() error { - err := jerrors.Errorf("close gettyRPCClient{%#v} again", c) + err := errors.Errorf("close gettyRPCClient{%#v} again", c) c.once.Do(func() { // delete @c from client pool c.pool.remove(c) diff --git a/protocol/dubbo/readwriter.go b/protocol/dubbo/readwriter.go index b4ad3f8e80e480d7b05fd1d77dadd52ecd8f2e21..9fa86bf30321720807b1c9b1e89c55d74d6caa3f 100644 --- a/protocol/dubbo/readwriter.go +++ b/protocol/dubbo/readwriter.go @@ -16,17 +16,17 @@ package dubbo import ( "bytes" - "github.com/dubbo/go-for-apache-dubbo/common/constant" "reflect" ) import ( "github.com/AlexStocks/getty" log "github.com/AlexStocks/log4go" - jerrors "github.com/juju/errors" + "github.com/pkg/errors" ) import ( "github.com/dubbo/go-for-apache-dubbo/common" + "github.com/dubbo/go-for-apache-dubbo/common/constant" ) //////////////////////////////////////////// @@ -49,7 +49,7 @@ func (p *RpcClientPackageHandler) Read(ss getty.Session, data []byte) (interface buf := bytes.NewBuffer(data) err := pkg.Unmarshal(buf, p.client) if err != nil { - pkg.Err = jerrors.Trace(err) // client will get this err + pkg.Err = errors.WithStack(err) // client will get this err return pkg, len(data), nil } @@ -60,16 +60,16 @@ func (p *RpcClientPackageHandler) Write(ss getty.Session, pkg interface{}) error req, ok := pkg.(*DubboPackage) if !ok { log.Error("illegal pkg:%+v\n", pkg) - return jerrors.New("invalid rpc request") + return errors.New("invalid rpc request") } buf, err := req.Marshal() if err != nil { - log.Warn("binary.Write(req{%#v}) = err{%#v}", req, jerrors.ErrorStack(err)) - return jerrors.Trace(err) + log.Warn("binary.Write(req{%#v}) = err{%#v}", req, errors.Cause(err)) + return errors.WithStack(err) } - return jerrors.Trace(ss.WriteBytes(buf.Bytes())) + return errors.WithStack(ss.WriteBytes(buf.Bytes())) } //////////////////////////////////////////// @@ -91,7 +91,7 @@ func (p *RpcServerPackageHandler) Read(ss getty.Session, data []byte) (interface buf := bytes.NewBuffer(data) err := pkg.Unmarshal(buf) if err != nil { - return nil, 0, jerrors.Trace(err) + return nil, 0, errors.WithStack(err) } // convert params of request req := pkg.Body.([]interface{}) // length of body should be 7 @@ -137,14 +137,14 @@ func (p *RpcServerPackageHandler) Write(ss getty.Session, pkg interface{}) error res, ok := pkg.(*DubboPackage) if !ok { log.Error("illegal pkg:%+v\n, it is %+v", pkg, reflect.TypeOf(pkg)) - return jerrors.New("invalid rpc response") + return errors.New("invalid rpc response") } buf, err := res.Marshal() if err != nil { - log.Warn("binary.Write(res{%#v}) = err{%#v}", res, jerrors.ErrorStack(err)) - return jerrors.Trace(err) + log.Warn("binary.Write(res{%#v}) = err{%#v}", res, errors.Cause(err)) + return errors.WithStack(err) } - return jerrors.Trace(ss.WriteBytes(buf.Bytes())) + return errors.WithStack(ss.WriteBytes(buf.Bytes())) } diff --git a/protocol/jsonrpc/http.go b/protocol/jsonrpc/http.go index d19f96dce7440ce3764ea8aabfede5b25ff6d0b2..68e18f889bcaf6cc1f7495f22b19c6befd40a0c1 100644 --- a/protocol/jsonrpc/http.go +++ b/protocol/jsonrpc/http.go @@ -30,7 +30,7 @@ import ( ) import ( - jerrors "github.com/juju/errors" + "github.com/pkg/errors" ) import ( @@ -130,15 +130,15 @@ func (c *HTTPClient) Call(ctx context.Context, service common.URL, req *Request, } reqBody, err := codec.Write(&codecData) if err != nil { - return jerrors.Trace(err) + return errors.WithStack(err) } rspBody, err := c.Do(service.Location, service.Params.Get("interface"), httpHeader, reqBody) if err != nil { - return jerrors.Trace(err) + return errors.WithStack(err) } - return jerrors.Trace(codec.Read(rspBody, rsp)) + return errors.WithStack(codec.Read(rspBody, rsp)) } // !!The high level of complexity and the likelihood that the fasthttp client has not been extensively used @@ -148,19 +148,19 @@ func (c *HTTPClient) Do(addr, path string, httpHeader http.Header, body []byte) u := url.URL{Host: strings.TrimSuffix(addr, ":"), Path: path} httpReq, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(body)) if err != nil { - return nil, jerrors.Trace(err) + return nil, errors.WithStack(err) } httpReq.Header = httpHeader httpReq.Close = true reqBuf := bytes.NewBuffer(make([]byte, 0)) if err := httpReq.Write(reqBuf); err != nil { - return nil, jerrors.Trace(err) + return nil, errors.WithStack(err) } tcpConn, err := net.DialTimeout("tcp", addr, c.options.HandshakeTimeout) if err != nil { - return nil, jerrors.Trace(err) + return nil, errors.WithStack(err) } defer tcpConn.Close() setNetConnTimeout := func(conn net.Conn, timeout time.Duration) { @@ -174,22 +174,22 @@ func (c *HTTPClient) Do(addr, path string, httpHeader http.Header, body []byte) setNetConnTimeout(tcpConn, c.options.HTTPTimeout) if _, err := reqBuf.WriteTo(tcpConn); err != nil { - return nil, jerrors.Trace(err) + return nil, errors.WithStack(err) } httpRsp, err := http.ReadResponse(bufio.NewReader(tcpConn), httpReq) if err != nil { - return nil, jerrors.Trace(err) + return nil, errors.WithStack(err) } defer httpRsp.Body.Close() b, err := ioutil.ReadAll(httpRsp.Body) if err != nil { - return nil, jerrors.Trace(err) + return nil, errors.WithStack(err) } if httpRsp.StatusCode != http.StatusOK { - return nil, jerrors.New(fmt.Sprintf("http status:%q, error string:%q", httpRsp.Status, string(b))) + return nil, errors.New(fmt.Sprintf("http status:%q, error string:%q", httpRsp.Status, string(b))) } return b, nil diff --git a/protocol/jsonrpc/http_test.go b/protocol/jsonrpc/http_test.go index 468eeb09ad12b03b6e4146bbaf2c997e3be82757..030cd609b4e12ae6a61547b9971af0823f0e143d 100644 --- a/protocol/jsonrpc/http_test.go +++ b/protocol/jsonrpc/http_test.go @@ -16,13 +16,13 @@ package jsonrpc import ( "context" - "errors" "strings" "testing" "time" ) import ( + "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) diff --git a/protocol/jsonrpc/json.go b/protocol/jsonrpc/json.go index b0af78879fe5eb88bfef04ff7b0f037a76b70fb4..d82b00f69ac2f8542cd3c6ec54a050f05dddd370 100644 --- a/protocol/jsonrpc/json.go +++ b/protocol/jsonrpc/json.go @@ -24,7 +24,7 @@ import ( ) import ( - jerrors "github.com/juju/errors" + "github.com/pkg/errors" ) const ( @@ -140,10 +140,10 @@ func (c *jsonClientCodec) Write(d *CodecData) ([]byte, error) { } case reflect.Array, reflect.Struct: default: - return nil, jerrors.New("unsupported param type: Ptr to " + k.String()) + return nil, errors.New("unsupported param type: Ptr to " + k.String()) } default: - return nil, jerrors.New("unsupported param type: " + k.String()) + return nil, errors.New("unsupported param type: " + k.String()) } } @@ -158,7 +158,7 @@ func (c *jsonClientCodec) Write(d *CodecData) ([]byte, error) { defer buf.Reset() enc := json.NewEncoder(buf) if err := enc.Encode(&c.req); err != nil { - return nil, jerrors.Trace(err) + return nil, errors.WithStack(err) } return buf.Bytes(), nil @@ -172,24 +172,24 @@ func (c *jsonClientCodec) Read(streamBytes []byte, x interface{}) error { dec := json.NewDecoder(buf) if err := dec.Decode(&c.rsp); err != nil { if err != io.EOF { - err = jerrors.Trace(err) + err = errors.WithStack(err) } return err } _, ok := c.pending[c.rsp.ID] if !ok { - err := jerrors.Errorf("can not find method of rsponse id %v, rsponse error:%v", c.rsp.ID, c.rsp.Error) + err := errors.Errorf("can not find method of rsponse id %v, rsponse error:%v", c.rsp.ID, c.rsp.Error) return err } delete(c.pending, c.rsp.ID) // c.rsp.ID if c.rsp.Error != nil { - return jerrors.New(c.rsp.Error.Error()) + return errors.New(c.rsp.Error.Error()) } - return jerrors.Trace(json.Unmarshal(*c.rsp.Result, x)) + return errors.WithStack(json.Unmarshal(*c.rsp.Result, x)) } ////////////////////////////////////////// @@ -221,32 +221,32 @@ func (r *serverRequest) UnmarshalJSON(raw []byte) error { // Attention: if do not define a new struct named @req, the json.Unmarshal will invoke // (*serverRequest)UnmarshalJSON recursively. if err := json.Unmarshal(raw, req(r)); err != nil { - return jerrors.New("bad request") + return errors.New("bad request") } var o = make(map[string]*json.RawMessage) if err := json.Unmarshal(raw, &o); err != nil { - return jerrors.New("bad request") + return errors.New("bad request") } if o["jsonrpc"] == nil || o["method"] == nil { - return jerrors.New("bad request") + return errors.New("bad request") } _, okID := o["id"] _, okParams := o["params"] if len(o) == 3 && !(okID || okParams) || len(o) == 4 && !(okID && okParams) || len(o) > 4 { - return jerrors.New("bad request") + return errors.New("bad request") } if r.Version != Version { - return jerrors.New("bad request") + return errors.New("bad request") } if okParams { if r.Params == nil || len(*r.Params) == 0 { - return jerrors.New("bad request") + return errors.New("bad request") } switch []byte(*r.Params)[0] { case '[', '{': default: - return jerrors.New("bad request") + return errors.New("bad request") } } if okID && r.ID == nil { @@ -254,11 +254,11 @@ func (r *serverRequest) UnmarshalJSON(raw []byte) error { } if okID { if len(*r.ID) == 0 { - return jerrors.New("bad request") + return errors.New("bad request") } switch []byte(*r.ID)[0] { case 't', 'f', '{', '[': - return jerrors.New("bad request") + return errors.New("bad request") } } @@ -394,7 +394,7 @@ func (c *ServerCodec) Write(errMsg string, x interface{}) ([]byte, error) { defer buf.Reset() enc := json.NewEncoder(buf) if err := enc.Encode(resp); err != nil { - return nil, jerrors.Trace(err) + return nil, errors.WithStack(err) } return buf.Bytes(), nil diff --git a/protocol/jsonrpc/server.go b/protocol/jsonrpc/server.go index 78e96a4110f2c6035bcfc0723d58c4d6d4129fac..aa02a9b1982a165ec4decf999892b288e3185d7a 100644 --- a/protocol/jsonrpc/server.go +++ b/protocol/jsonrpc/server.go @@ -31,7 +31,7 @@ import ( import ( log "github.com/AlexStocks/log4go" - jerrors "github.com/juju/errors" + "github.com/pkg/errors" ) import ( @@ -105,10 +105,10 @@ func (s *Server) handlePkg(conn net.Conn) { rspBuf.Reset() err := rsp.Write(rspBuf) if err != nil { - return jerrors.Trace(err) + return errors.WithStack(err) } _, err = rspBuf.WriteTo(conn) - return jerrors.Trace(err) + return errors.WithStack(err) } for { @@ -140,9 +140,9 @@ func (s *Server) handlePkg(conn net.Conn) { if contentType != "application/json" && contentType != "application/json-rpc" { setTimeout(conn, httpTimeout) r.Header.Set("Content-Type", "text/plain") - if errRsp := sendErrorResp(r.Header, []byte(jerrors.ErrorStack(err))); errRsp != nil { - log.Warn("sendErrorResp(header:%#v, error:%s) = error:%s", - r.Header, jerrors.ErrorStack(err), errRsp) + if errRsp := sendErrorResp(r.Header, []byte(errors.Cause(err).Error())); errRsp != nil { + log.Warn("sendErrorResp(header:%#v, error:%v) = error:%s", + r.Header, errors.Cause(err), errRsp) } return } @@ -159,9 +159,9 @@ func (s *Server) handlePkg(conn net.Conn) { setTimeout(conn, httpTimeout) if err := serveRequest(ctx, reqHeader, reqBody, conn, s.exporter); err != nil { - if errRsp := sendErrorResp(r.Header, []byte(jerrors.ErrorStack(err))); errRsp != nil { - log.Warn("sendErrorResp(header:%#v, error:%s) = error:%s", - r.Header, jerrors.ErrorStack(err), errRsp) + if errRsp := sendErrorResp(r.Header, []byte(errors.Cause(err).Error())); errRsp != nil { + log.Warn("sendErrorResp(header:%#v, error:%v) = error:%s", + r.Header, errors.Cause(err), errRsp) } log.Info("Unexpected error serving request, closing socket: %v", err) @@ -193,7 +193,7 @@ func accept(listener net.Listener, fn func(net.Conn)) error { time.Sleep(tmpDelay) continue } - return jerrors.Trace(err) + return errors.WithStack(err) } go func() { @@ -266,10 +266,10 @@ func serveRequest(ctx context.Context, rspBuf.Reset() err := rsp.Write(rspBuf) if err != nil { - return jerrors.Trace(err) + return errors.WithStack(err) } _, err = rspBuf.WriteTo(conn) - return jerrors.Trace(err) + return errors.WithStack(err) } sendResp := func(header map[string]string, body []byte) error { @@ -290,10 +290,10 @@ func serveRequest(ctx context.Context, rspBuf.Reset() err := rsp.Write(rspBuf) if err != nil { - return jerrors.Trace(err) + return errors.WithStack(err) } _, err = rspBuf.WriteTo(conn) - return jerrors.Trace(err) + return errors.WithStack(err) } // read request header @@ -301,22 +301,22 @@ func serveRequest(ctx context.Context, err := codec.ReadHeader(header, body) if err != nil { if err == io.EOF || err == io.ErrUnexpectedEOF { - return jerrors.Trace(err) + return errors.WithStack(err) } - return jerrors.New("server cannot decode request: " + err.Error()) + return errors.New("server cannot decode request: " + err.Error()) } serviceName := header["Path"] methodName := codec.req.Method if len(serviceName) == 0 || len(methodName) == 0 { codec.ReadBody(nil) - return jerrors.New("service/method request ill-formed: " + serviceName + "/" + methodName) + return errors.New("service/method request ill-formed: " + serviceName + "/" + methodName) } // read body var args interface{} if err = codec.ReadBody(&args); err != nil { - return jerrors.Trace(err) + return errors.WithStack(err) } log.Debug("args: %v", args) @@ -333,18 +333,18 @@ func serveRequest(ctx context.Context, if errRsp := sendErrorResp(header, []byte(err.Error())); errRsp != nil { log.Warn("Exporter: sendErrorResp(header:%#v, error:%v) = error:%s", header, err, errRsp) - return jerrors.Trace(errRsp) + return errors.WithStack(errRsp) } } if res := result.Result(); res != nil { rspStream, err := codec.Write("", res) if err != nil { - return jerrors.Trace(err) + return errors.WithStack(err) } if errRsp := sendResp(header, rspStream); errRsp != nil { log.Warn("Exporter: sendResp(header:%#v, error:%v) = error:%s", header, err, errRsp) - return jerrors.Trace(errRsp) + return errors.WithStack(errRsp) } } } @@ -352,11 +352,11 @@ func serveRequest(ctx context.Context, // get method svc := common.ServiceMap.GetService(JSONRPC, serviceName) if svc == nil { - return jerrors.New("cannot find svc " + serviceName) + return errors.New("cannot find svc " + serviceName) } mtype := svc.Method()[methodName] if mtype == nil { - return jerrors.New("cannot find method " + methodName + " of svc " + serviceName) + return errors.New("cannot find method " + methodName + " of svc " + serviceName) } replyv := reflect.New(mtype.ReplyType().Elem()) @@ -394,7 +394,7 @@ func serveRequest(ctx context.Context, } rspStream, err := codec.Write(errMsg, rspReply) if err != nil { - return jerrors.Trace(err) + return errors.WithStack(err) } rsp := &http.Response{ StatusCode: code, diff --git a/registry/directory/directory.go b/registry/directory/directory.go index 363ed9a2c477e5585237e69e2349104d44b9e953..8122e0155ad910f7bf705cd973ce8b9a5015ac24 100644 --- a/registry/directory/directory.go +++ b/registry/directory/directory.go @@ -21,7 +21,7 @@ import ( import ( log "github.com/AlexStocks/log4go" - jerrors "github.com/juju/errors" + "github.com/pkg/errors" ) import ( @@ -61,7 +61,7 @@ func NewRegistryDirectory(url *common.URL, registry registry.Registry, opts ...O opt(&options) } if url.SubURL == nil { - return nil, jerrors.Errorf("url is invalid, suburl can not be nil") + return nil, errors.Errorf("url is invalid, suburl can not be nil") } return ®istryDirectory{ BaseDirectory: directory.NewBaseDirectory(url), @@ -87,14 +87,14 @@ func (dir *registryDirectory) Subscribe(url common.URL) { log.Warn("event listener game over.") return } - log.Warn("getListener() = err:%s", jerrors.ErrorStack(err)) + log.Warn("getListener() = err:%v", errors.Cause(err)) time.Sleep(time.Duration(RegistryConnDelay) * time.Second) continue } for { if serviceEvent, err := listener.Next(); err != nil { - log.Warn("Selector.watch() = error{%v}", jerrors.ErrorStack(err)) + log.Warn("Selector.watch() = error{%v}", errors.Cause(err)) listener.Close() time.Sleep(time.Duration(RegistryConnDelay) * time.Second) return diff --git a/registry/zookeeper/listener.go b/registry/zookeeper/listener.go index 2d0f91f3e153fb35d52f546ffe88fc048f9e36c7..9727856a849975115268551de528fa1945b21a28 100644 --- a/registry/zookeeper/listener.go +++ b/registry/zookeeper/listener.go @@ -24,7 +24,7 @@ import ( import ( log "github.com/AlexStocks/log4go" - jerrors "github.com/juju/errors" + "github.com/pkg/errors" "github.com/samuel/go-zookeeper/zk" ) @@ -111,7 +111,7 @@ func (l *zkEventListener) handleZkNodeEvent(zkPath string, children []string, co newChildren, err := l.client.getChildren(zkPath) if err != nil { - log.Error("path{%s} child nodes changed, zk.Children() = error{%v}", zkPath, jerrors.ErrorStack(err)) + log.Error("path{%s} child nodes changed, zk.Children() = error{%v}", zkPath, errors.Cause(err)) return } @@ -130,7 +130,7 @@ func (l *zkEventListener) handleZkNodeEvent(zkPath string, children []string, co //context.TODO serviceURL, err = common.NewURL(context.TODO(), n) if err != nil { - log.Error("NewURL(%s) = error{%v}", n, jerrors.ErrorStack(err)) + log.Error("NewURL(%s) = error{%v}", n, errors.Cause(err)) continue } if !conf.URLEqual(serviceURL) { @@ -166,7 +166,7 @@ func (l *zkEventListener) handleZkNodeEvent(zkPath string, children []string, co } log.Warn("delete serviceURL{%s}", serviceURL) if err != nil { - log.Error("NewURL(i{%s}) = error{%v}", n, jerrors.ErrorStack(err)) + log.Error("NewURL(i{%s}) = error{%v}", n, errors.Cause(err)) continue } l.events <- zkEvent{®istry.ServiceEvent{Action: registry.ServiceDel, Service: serviceURL}, nil} @@ -306,16 +306,16 @@ func (l *zkEventListener) Next() (*registry.ServiceEvent, error) { select { case <-l.client.done(): log.Warn("listener's zk client connection is broken, so zk event listener exit now.") - return nil, jerrors.New("listener stopped") + return nil, errors.New("listener stopped") case <-l.registry.done: log.Warn("zk consumer register has quit, so zk event listener exit asap now.") - return nil, jerrors.New("listener stopped") + return nil, errors.New("listener stopped") case e := <-l.events: log.Debug("got zk event %s", e) if e.err != nil { - return nil, jerrors.Trace(e.err) + return nil, errors.WithStack(e.err) } if e.res.Action == registry.ServiceDel && !l.valid() { log.Warn("update @result{%s}. But its connection to registry is invalid", e.res) diff --git a/registry/zookeeper/registry.go b/registry/zookeeper/registry.go index 361fc001d798a363dcd460f9dbe5ddccb1e2bc91..f8f26705b88293641ca0c1ae5830163946486568 100644 --- a/registry/zookeeper/registry.go +++ b/registry/zookeeper/registry.go @@ -28,7 +28,7 @@ import ( import ( "github.com/AlexStocks/goext/net" log "github.com/AlexStocks/log4go" - jerrors "github.com/juju/errors" + "github.com/pkg/errors" "github.com/samuel/go-zookeeper/zk" ) @@ -175,13 +175,13 @@ func (r *zkRegistry) validateZookeeperClient() error { if err != nil { log.Error("timeout config %v is invalid ,err is %v", r.GetParam(constant.REGISTRY_TIMEOUT_KEY, constant.DEFAULT_REG_TIMEOUT), err.Error()) - return jerrors.Annotatef(err, "newZookeeperClient(address:%+v)", r.Location) + return errors.WithMessagef(err, "newZookeeperClient(address:%+v)", r.Location) } r.client, err = newZookeeperClient(RegistryZkClient, []string{r.Location}, timeout) if err != nil { log.Warn("newZookeeperClient(name{%s}, zk addresss{%v}, timeout{%d}) = error{%v}", RegistryZkClient, r.Location, timeout.String(), err) - return jerrors.Annotatef(err, "newZookeeperClient(address:%+v)", r.Location) + return errors.WithMessagef(err, "newZookeeperClient(address:%+v)", r.Location) } } if r.client.conn == nil { @@ -193,7 +193,7 @@ func (r *zkRegistry) validateZookeeperClient() error { } } - return jerrors.Annotatef(err, "newZookeeperClient(address:%+v)", r.PrimitiveURL) + return errors.WithMessagef(err, "newZookeeperClient(address:%+v)", r.PrimitiveURL) } func (r *zkRegistry) handleZkRestart() { @@ -229,7 +229,7 @@ LOOP: } err = r.validateZookeeperClient() log.Info("ZkProviderRegistry.validateZookeeperClient(zkAddr{%s}) = error{%#v}", - r.client.zkAddrs, jerrors.ErrorStack(err)) + r.client.zkAddrs, errors.Cause(err)) if err == nil { // copy r.services services := []common.URL{} @@ -242,7 +242,7 @@ LOOP: err = r.register(confIf) if err != nil { log.Error("(ZkProviderRegistry)register(conf{%#v}) = error{%#v}", - confIf, jerrors.ErrorStack(err)) + confIf, errors.Cause(err)) flag = false break } @@ -275,12 +275,12 @@ func (r *zkRegistry) Register(conf common.URL) error { _, ok = r.services[conf.Key()] r.cltLock.Unlock() if ok { - return jerrors.Errorf("Path{%s} has been registered", conf.Path) + return errors.Errorf("Path{%s} has been registered", conf.Path) } err = r.register(conf) if err != nil { - return jerrors.Trace(err) + return errors.WithStack(err) } r.cltLock.Lock() @@ -304,12 +304,12 @@ func (r *zkRegistry) Register(conf common.URL) error { _, ok = r.services[conf.Key()] r.cltLock.Unlock() if ok { - return jerrors.Errorf("Path{%s} has been registered", conf.Key()) + return errors.Errorf("Path{%s} has been registered", conf.Key()) } err = r.register(conf) if err != nil { - return jerrors.Annotatef(err, "register(conf:%+v)", conf) + return errors.WithMessagef(err, "register(conf:%+v)", conf) } r.cltLock.Lock() @@ -336,7 +336,7 @@ func (r *zkRegistry) register(c common.URL) error { err = r.validateZookeeperClient() if err != nil { - return jerrors.Trace(err) + return errors.WithStack(err) } params = url.Values{} for k, v := range c.Params { @@ -353,7 +353,7 @@ func (r *zkRegistry) register(c common.URL) error { case common.PROVIDER: if c.Path == "" || len(c.Methods) == 0 { - return jerrors.Errorf("conf{Path:%s, Methods:%s}", c.Path, c.Methods) + return errors.Errorf("conf{Path:%s, Methods:%s}", c.Path, c.Methods) } // 鍏堝垱寤烘湇鍔′笅闈㈢殑provider node dubboPath = fmt.Sprintf("/dubbo%s/%s", c.Path, common.DubboNodes[common.PROVIDER]) @@ -361,8 +361,8 @@ func (r *zkRegistry) register(c common.URL) error { err = r.client.Create(dubboPath) r.cltLock.Unlock() if err != nil { - log.Error("zkClient.create(path{%s}) = error{%#v}", dubboPath, jerrors.ErrorStack(err)) - return jerrors.Annotatef(err, "zkclient.Create(path:%s)", dubboPath) + log.Error("zkClient.create(path{%s}) = error{%#v}", dubboPath, errors.Cause(err)) + return errors.WithMessagef(err, "zkclient.Create(path:%s)", dubboPath) } params.Add("anyhost", "true") @@ -403,16 +403,16 @@ func (r *zkRegistry) register(c common.URL) error { err = r.client.Create(dubboPath) r.cltLock.Unlock() if err != nil { - log.Error("zkClient.create(path{%s}) = error{%v}", dubboPath, jerrors.ErrorStack(err)) - return jerrors.Trace(err) + log.Error("zkClient.create(path{%s}) = error{%v}", dubboPath, errors.Cause(err)) + return errors.WithStack(err) } dubboPath = fmt.Sprintf("/dubbo%s/%s", c.Path, common.DubboNodes[common.PROVIDER]) r.cltLock.Lock() err = r.client.Create(dubboPath) r.cltLock.Unlock() if err != nil { - log.Error("zkClient.create(path{%s}) = error{%v}", dubboPath, jerrors.ErrorStack(err)) - return jerrors.Trace(err) + log.Error("zkClient.create(path{%s}) = error{%v}", dubboPath, errors.Cause(err)) + return errors.WithStack(err) } params.Add("protocol", c.Protocol) @@ -426,13 +426,13 @@ func (r *zkRegistry) register(c common.URL) error { dubboPath = fmt.Sprintf("/dubbo%s/%s", c.Path, (common.RoleType(common.CONSUMER)).String()) log.Debug("consumer path:%s, url:%s", dubboPath, rawURL) default: - return jerrors.Errorf("@c{%v} type is not referencer or provider", c) + return errors.Errorf("@c{%v} type is not referencer or provider", c) } err = r.registerTempZookeeperNode(dubboPath, encodedURL) if err != nil { - return jerrors.Annotatef(err, "registerTempZookeeperNode(path:%s, url:%s)", dubboPath, rawURL) + return errors.WithMessagef(err, "registerTempZookeeperNode(path:%s, url:%s)", dubboPath, rawURL) } return nil } @@ -447,13 +447,13 @@ func (r *zkRegistry) registerTempZookeeperNode(root string, node string) error { defer r.cltLock.Unlock() err = r.client.Create(root) if err != nil { - log.Error("zk.Create(root{%s}) = err{%v}", root, jerrors.ErrorStack(err)) - return jerrors.Trace(err) + log.Error("zk.Create(root{%s}) = err{%v}", root, errors.Cause(err)) + return errors.WithStack(err) } zkPath, err = r.client.RegisterTemp(root, node) if err != nil { - log.Error("RegisterTempNode(root{%s}, node{%s}) = error{%v}", root, node, jerrors.ErrorStack(err)) - return jerrors.Annotatef(err, "RegisterTempNode(root{%s}, node{%s})", root, node) + log.Error("RegisterTempNode(root{%s}, node{%s}) = error{%v}", root, node, errors.Cause(err)) + return errors.WithMessagef(err, "RegisterTempNode(root{%s}, node{%s})", root, node) } log.Debug("create a zookeeper node:%s", zkPath) @@ -481,7 +481,7 @@ func (r *zkRegistry) getListener(conf common.URL) (*zkEventListener, error) { client := r.client r.cltLock.Unlock() if client == nil { - return nil, jerrors.New("zk connection broken") + return nil, errors.New("zk connection broken") } // new client & listener diff --git a/registry/zookeeper/zk_client.go b/registry/zookeeper/zk_client.go index 9c9cdb0d538364e90b6eacd40cb5020825f5332f..341142027ccac8329ed10cc1a0c96b60b86b44fd 100644 --- a/registry/zookeeper/zk_client.go +++ b/registry/zookeeper/zk_client.go @@ -15,7 +15,6 @@ package zookeeper import ( - "errors" "path" "strings" "sync" @@ -24,7 +23,7 @@ import ( import ( log "github.com/AlexStocks/log4go" - jerrors "github.com/juju/errors" + "github.com/pkg/errors" "github.com/samuel/go-zookeeper/zk" ) @@ -95,7 +94,7 @@ func newZookeeperClient(name string, zkAddrs []string, timeout time.Duration) (* // connect to zookeeper z.conn, event, err = zk.Connect(zkAddrs, timeout) if err != nil { - return nil, jerrors.Annotatef(err, "zk.Connect(zkAddrs:%+v)", zkAddrs) + return nil, errors.WithMessagef(err, "zk.Connect(zkAddrs:%+v)", zkAddrs) } z.wait.Add(1) @@ -121,7 +120,7 @@ func newMockZookeeperClient(name string, timeout time.Duration) (*zk.TestCluster ts, err := zk.StartTestCluster(1, nil, nil) if err != nil { - return nil, nil, nil, jerrors.Annotatef(err, "zk.Connect") + return nil, nil, nil, errors.WithMessagef(err, "zk.Connect") } //callbackChan := make(chan zk.Event) @@ -131,7 +130,7 @@ func newMockZookeeperClient(name string, timeout time.Duration) (*zk.TestCluster z.conn, event, err = ts.ConnectWithOptions(timeout) if err != nil { - return nil, nil, nil, jerrors.Annotatef(err, "zk.Connect") + return nil, nil, nil, errors.WithMessagef(err, "zk.Connect") } //z.wait.Add(1) @@ -305,8 +304,8 @@ func (z *zookeeperClient) Create(basePath string) error { if err == zk.ErrNodeExists { log.Error("zk.create(\"%s\") exists\n", tmpPath) } else { - log.Error("zk.create(\"%s\") error(%v)\n", tmpPath, jerrors.ErrorStack(err)) - return jerrors.Annotatef(err, "zk.Create(path:%s)", basePath) + log.Error("zk.create(\"%s\") error(%v)\n", tmpPath, errors.Cause(err)) + return errors.WithMessagef(err, "zk.Create(path:%s)", basePath) } } } @@ -326,7 +325,7 @@ func (z *zookeeperClient) Delete(basePath string) error { } z.Unlock() - return jerrors.Annotatef(err, "Delete(basePath:%s)", basePath) + return errors.WithMessagef(err, "Delete(basePath:%s)", basePath) } func (z *zookeeperClient) RegisterTemp(basePath string, node string) (string, error) { @@ -347,8 +346,8 @@ func (z *zookeeperClient) RegisterTemp(basePath string, node string) (string, er z.Unlock() //if err != nil && err != zk.ErrNodeExists { if err != nil { - log.Warn("conn.Create(\"%s\", zk.FlagEphemeral) = error(%v)\n", zkPath, jerrors.ErrorStack(err)) - return "", jerrors.Trace(err) + log.Warn("conn.Create(\"%s\", zk.FlagEphemeral) = error(%v)\n", zkPath, errors.Cause(err)) + return "", errors.WithStack(err) } log.Debug("zkClient{%s} create a temp zookeeper node:%s\n", z.name, tmpPath) @@ -376,7 +375,7 @@ func (z *zookeeperClient) RegisterTempSeq(basePath string, data []byte) (string, if err != nil && err != zk.ErrNodeExists { log.Error("zkClient{%s} conn.Create(\"%s\", \"%s\", zk.FlagEphemeral|zk.FlagSequence) error(%v)\n", z.name, basePath, string(data), err) - return "", jerrors.Trace(err) + return "", errors.WithStack(err) } log.Debug("zkClient{%s} create a temp zookeeper node:%s\n", z.name, tmpPath) @@ -399,16 +398,16 @@ func (z *zookeeperClient) getChildrenW(path string) ([]string, <-chan zk.Event, z.Unlock() if err != nil { if err == zk.ErrNoNode { - return nil, nil, jerrors.Errorf("path{%s} has none children", path) + return nil, nil, errors.Errorf("path{%s} has none children", path) } log.Error("zk.ChildrenW(path{%s}) = error(%v)", path, err) - return nil, nil, jerrors.Annotatef(err, "zk.ChildrenW(path:%s)", path) + return nil, nil, errors.WithMessagef(err, "zk.ChildrenW(path:%s)", path) } if stat == nil { - return nil, nil, jerrors.Errorf("path{%s} has none children", path) + return nil, nil, errors.Errorf("path{%s} has none children", path) } if len(children) == 0 { - return nil, nil, jerrors.Errorf("path{%s} has none children", path) + return nil, nil, errors.Errorf("path{%s} has none children", path) } return children, event, nil @@ -429,16 +428,16 @@ func (z *zookeeperClient) getChildren(path string) ([]string, error) { z.Unlock() if err != nil { if err == zk.ErrNoNode { - return nil, jerrors.Errorf("path{%s} has none children", path) + return nil, errors.Errorf("path{%s} has none children", path) } - log.Error("zk.Children(path{%s}) = error(%v)", path, jerrors.ErrorStack(err)) - return nil, jerrors.Annotatef(err, "zk.Children(path:%s)", path) + log.Error("zk.Children(path{%s}) = error(%v)", path, errors.Cause(err)) + return nil, errors.WithMessagef(err, "zk.Children(path:%s)", path) } if stat == nil { - return nil, jerrors.Errorf("path{%s} has none children", path) + return nil, errors.Errorf("path{%s} has none children", path) } if len(children) == 0 { - return nil, jerrors.Errorf("path{%s} has none children", path) + return nil, errors.Errorf("path{%s} has none children", path) } return children, nil @@ -458,12 +457,12 @@ func (z *zookeeperClient) existW(zkPath string) (<-chan zk.Event, error) { } z.Unlock() if err != nil { - log.Error("zkClient{%s}.ExistsW(path{%s}) = error{%v}.", z.name, zkPath, jerrors.ErrorStack(err)) - return nil, jerrors.Annotatef(err, "zk.ExistsW(path:%s)", zkPath) + log.Error("zkClient{%s}.ExistsW(path{%s}) = error{%v}.", z.name, zkPath, errors.Cause(err)) + return nil, errors.WithMessagef(err, "zk.ExistsW(path:%s)", zkPath) } if !exist { log.Warn("zkClient{%s}'s App zk path{%s} does not exist.", z.name, zkPath) - return nil, jerrors.Errorf("zkClient{%s} App zk path{%s} does not exist.", z.name, zkPath) + return nil, errors.Errorf("zkClient{%s} App zk path{%s} does not exist.", z.name, zkPath) } return event, nil