优化自定义错误类型

This commit is contained in:
duanhf2012
2020-04-01 13:52:59 +08:00
parent 89d41d60fa
commit 4e6e7c0c95

View File

@@ -13,16 +13,11 @@ type FuncRpcClient func(nodeid int,serviceMethod string) ([]*Client,error)
type FuncRpcServer func() (*Server) type FuncRpcServer func() (*Server)
var NilError = reflect.Zero(reflect.TypeOf((*error)(nil)).Elem()) var NilError = reflect.Zero(reflect.TypeOf((*error)(nil)).Elem())
type RpcError struct { type RpcError string
Err string
}
func (slf *RpcError) Error() string {
return slf.Err
}
func NewRpcError(e string) *RpcError { func (e RpcError) Error() string {
return &RpcError{Err:e} return string(e)
} }
func ConvertError(e error) *RpcError{ func ConvertError(e error) *RpcError{
@@ -30,11 +25,13 @@ func ConvertError(e error) *RpcError{
return nil return nil
} }
return &RpcError{Err:e.Error()} rpcErr := RpcError(e.Error())
return &rpcErr
} }
func Errorf(format string, a ...interface{}) *RpcError { func Errorf(format string, a ...interface{}) *RpcError {
return NewRpcError(fmt.Sprintf(format,a...)) rpcErr := RpcError(fmt.Sprintf(format,a...))
return &rpcErr
} }