Make error conveyance more idiomatic.

This commit applies two bulk changes to the grpc error reporting
mechanisms:

(1.) Error strings for errors that originate within grpc are prefixed
    with the package name for better clarity for where they originate
    since they could percolate up in the users call chains to the
    originator.

(2.) Errors that are, in fact, singletons have been converted from
    fmt.Errorf to errors.New and assigned as package-level variables.
    This bodes particularly well for enabling API customers to elect to
    handle these errors upon receipt via equality comparison.  This had
    been previous impossible with the original API.

Supplementarily, ``gofmt -w -s=true`` has been run on the repository to
cleanup residual defects, and it has detected and repaired a few.

TEST=Manual go test ./...
This commit is contained in:
Matt T. Proud
2015-02-19 12:57:41 +01:00
parent 940841bf56
commit a720ae6f48
14 changed files with 68 additions and 45 deletions

View File

@ -34,7 +34,7 @@
package grpc
import (
"fmt"
"errors"
"sync"
"time"
@ -43,6 +43,14 @@ import (
"google.golang.org/grpc/transport"
)
var (
// ErrUnspecTarget indicates that the target address is unspecified.
ErrUnspecTarget = errors.New("grpc: target is unspecified")
// ErrClosingChan indicates that the operation is illegal because the session
// is closing.
ErrClosingChan = errors.New("grpc: the channel is closing")
)
type dialOptions struct {
protocol string
authOptions []credentials.Credentials
@ -73,7 +81,7 @@ func WithPerRPCCredentials(creds credentials.Credentials) DialOption {
// for connection to complete.
func Dial(target string, opts ...DialOption) (*ClientConn, error) {
if target == "" {
return nil, fmt.Errorf("rpc.Dial: target is empty")
return nil, ErrUnspecTarget
}
cc := &ClientConn{
target: target,
@ -119,7 +127,7 @@ func (cc *ClientConn) resetTransport(closeTransport bool) error {
cc.transportSeq = 0
if cc.closing {
cc.mu.Unlock()
return fmt.Errorf("rpc.ClientConn.resetTransport: the channel is closing")
return ErrClosingChan
}
cc.mu.Unlock()
if closeTransport {
@ -174,7 +182,7 @@ func (cc *ClientConn) wait(ctx context.Context, ts int) (transport.ClientTranspo
switch {
case cc.closing:
cc.mu.Unlock()
return nil, 0, fmt.Errorf("ClientConn is closing")
return nil, 0, ErrClosingChan
case ts < cc.transportSeq:
// Worked on a dying transport. Try the new one immediately.
defer cc.mu.Unlock()