Skip to content

Commit

Permalink
unexport internal types/funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
lanzafame committed Jun 27, 2018
1 parent 3492601 commit f4d75b0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 55 deletions.
16 changes: 8 additions & 8 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func (c *Client) makeCall(call *Call) {
call.SvcID.Method,
)
if c.server == nil {
err := &ClientError{"Cannot make local calls: server not set"}
err := &clientError{"Cannot make local calls: server not set"}
call.doneWithError(err)
return
}
Expand All @@ -266,7 +266,7 @@ func (c *Client) send(call *Call) {

s, err := c.host.NewStream(call.ctx, call.Dest, c.protocol)
if err != nil {
call.doneWithError(NewClientError(err))
call.doneWithError(newClientError(err))
return
}
defer s.Close()
Expand All @@ -281,18 +281,18 @@ func (c *Client) send(call *Call) {
call.Dest,
)
if err := sWrap.enc.Encode(call.SvcID); err != nil {
call.doneWithError(NewClientError(err))
call.doneWithError(newClientError(err))
s.Reset()
return
}
if err := sWrap.enc.Encode(call.Args); err != nil {
call.doneWithError(NewClientError(err))
call.doneWithError(newClientError(err))
s.Reset()
return
}

if err := sWrap.w.Flush(); err != nil {
call.doneWithError(NewClientError(err))
call.doneWithError(newClientError(err))
s.Reset()
return
}
Expand All @@ -309,20 +309,20 @@ func receiveResponse(s *streamWrap, call *Call) {
)
var resp Response
if err := s.dec.Decode(&resp); err != nil {
call.doneWithError(NewClientError(err))
call.doneWithError(newClientError(err))
s.stream.Reset()
return
}

defer call.done()
if e := resp.Error; e != "" {
call.setError(ResponseError(resp.ErrType, e))
call.setError(responseError(resp.ErrType, e))
}

// Even on error we sent the reply so it needs to be
// read
if err := s.dec.Decode(call.Reply); err != nil && err != io.EOF {
call.setError(NewClientError(err))
call.setError(newClientError(err))
}
return
}
96 changes: 58 additions & 38 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,101 @@ package rpc

import "errors"

// ResponseErr is an enum type for providing error type
// responseErr is an enum type for providing error type
// information over the wire between rpc server and client.
type ResponseErr int
type responseErr int

const (
// NonRPCErr is an error that hasn't arisen from the gorpc package.
NonRPCErr ResponseErr = iota
// ServerErr is an error that has arisen on the server side.
ServerErr
// ClientErr is an error that has arisen on the client side.
ClientErr
// nonRPCErr is an error that hasn't arisen from the gorpc package.
nonRPCErr responseErr = iota
// serverErr is an error that has arisen on the server side.
serverErr
// clientErr is an error that has arisen on the client side.
clientErr
)

// ServerError indicates that error originated in server
// serverError indicates that error originated in server
// specific code.
type ServerError struct {
type serverError struct {
msg string
}

func (s *ServerError) Error() string {
func (s *serverError) Error() string {
return s.msg
}

// NewServerError wraps an error in the ServerError type.
func NewServerError(err error) error {
return &ServerError{err.Error()}
// newServerError wraps an error in the serverError type.
func newServerError(err error) error {
return &serverError{err.Error()}
}

// ClientError indicates that error originated in client
// clientError indicates that error originated in client
// specific code.
type ClientError struct {
type clientError struct {
msg string
}

func (c *ClientError) Error() string {
func (c *clientError) Error() string {
return c.msg
}

// NewClientError wraps an error in the ClientError type.
func NewClientError(err error) error {
return &ClientError{err.Error()}
// newClientError wraps an error in the clientError type.
func newClientError(err error) error {
return &clientError{err.Error()}
}

// ResponseError converts an ResponseErr and error message string
// responseError converts an responseErr and error message string
// into the appropriate error type.
func ResponseError(errType ResponseErr, errMsg string) error {
func responseError(errType responseErr, errMsg string) error {
switch errType {
case ServerErr:
return &ServerError{errMsg}
case ClientErr:
return &ClientError{errMsg}
case serverErr:
return &serverError{errMsg}
case clientErr:
return &clientError{errMsg}
default:
return errors.New(errMsg)
}
}

// ResponseErrorType determines whether an error is of either
// ServerError or ClientError type and returns the appropriate
// ResponseErr value.
func ResponseErrorType(err error) ResponseErr {
// responseErrorType determines whether an error is of either
// serverError or clientError type and returns the appropriate
// responseErr value.
func responseErrorType(err error) responseErr {
switch err.(type) {
case *ServerError:
return ServerErr
case *ClientError:
return ClientErr
case *serverError:
return serverErr
case *clientError:
return clientErr
default:
return NonRPCErr
return nonRPCErr
}
}

// IsRPCError returns whether an error is either a ServerError
// or ClientError.
// IsRPCError returns whether an error is either a serverError
// or clientError.
func IsRPCError(err error) bool {
switch err.(type) {
case *ServerError, *ClientError:
case *serverError, *clientError:
return true
default:
return false
}
}

// IsServerError returns whether an error is serverError.
func IsServerError(err error) bool {
switch err.(type) {
case *serverError:
return true
default:
return false
}
}

// IsClientError returns whether an error is clientError.
func IsClientError(err error) bool {
switch err.(type) {
case *clientError:
return true
default:
return false
Expand Down
18 changes: 9 additions & 9 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ type ServiceID struct {
type Response struct {
Service ServiceID
Error string // error, if any.
ErrType ResponseErr
ErrType responseErr
}

// Server is an LibP2P RPC server. It can register services which comply to the
Expand Down Expand Up @@ -129,7 +129,7 @@ func NewServer(h host.Host, p protocol.ID) *Server {
err := s.handle(sWrap)
if err != nil {
logger.Error("error handling RPC:", err)
resp := &Response{ServiceID{}, err.Error(), ResponseErrorType(err)}
resp := &Response{ServiceID{}, err.Error(), responseErrorType(err)}
sendResponse(sWrap, resp, nil)
}
})
Expand All @@ -153,14 +153,14 @@ func (server *Server) handle(s *streamWrap) error {

err := s.dec.Decode(&svcID)
if err != nil {
return NewServerError(err)
return newServerError(err)
}

logger.Debugf("RPC ServiceID is %s.%s", svcID.Name, svcID.Method)

service, mtype, err := server.getService(svcID)
if err != nil {
return NewServerError(err)
return newServerError(err)
}

// Decode the argument value.
Expand All @@ -173,7 +173,7 @@ func (server *Server) handle(s *streamWrap) error {
}
// argv guaranteed to be a pointer now.
if err = s.dec.Decode(argv.Interface()); err != nil {
return NewServerError(err)
return newServerError(err)
}
if argIsValue {
argv = argv.Elem()
Expand Down Expand Up @@ -217,7 +217,7 @@ func (s *service) svcCall(sWrap *streamWrap, mtype *methodType, svcID ServiceID,
if errInter != nil {
errmsg = errInter.(error).Error()
}
resp := &Response{svcID, errmsg, NonRPCErr}
resp := &Response{svcID, errmsg, nonRPCErr}

return sendResponse(sWrap, resp, replyv.Interface())
}
Expand Down Expand Up @@ -246,7 +246,7 @@ func (server *Server) Call(call *Call) error {
var argv, replyv reflect.Value
service, mtype, err := server.getService(call.SvcID)
if err != nil {
return NewServerError(err)
return newServerError(err)
}

// Use the context value from the call directly
Expand Down Expand Up @@ -314,12 +314,12 @@ func (server *Server) getService(id ServiceID) (*service, *methodType, error) {
server.mu.RUnlock()
if service == nil {
err := errors.New("rpc: can't find service " + id.Name)
return nil, nil, NewServerError(err)
return nil, nil, newServerError(err)
}
mtype := service.method[id.Method]
if mtype == nil {
err := errors.New("rpc: can't find method " + id.Method)
return nil, nil, NewServerError(err)
return nil, nil, newServerError(err)
}
return service, mtype, nil
}
Expand Down

0 comments on commit f4d75b0

Please sign in to comment.