Skip to content

Commit

Permalink
Merge pull request #2576 from tschottdorf/goerror
Browse files Browse the repository at this point in the history
move GoError() meat to *Error
  • Loading branch information
tbg committed Sep 21, 2015
2 parents 24595ab + f7f60f4 commit 415b85b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
32 changes: 1 addition & 31 deletions proto/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"math/rand"
"strings"

"github.com/cockroachdb/cockroach/util/retry"
gogoproto "github.com/gogo/protobuf/proto"
)

Expand Down Expand Up @@ -403,36 +402,7 @@ func (rh *ResponseHeader) Verify(req Request) error {

// GoError returns the non-nil error from the proto.Error union.
func (rh *ResponseHeader) GoError() error {
if rh.Error == nil {
return nil
}
if rh.Error.Detail == nil {
return rh.Error
}
err := rh.Error.getDetail()
if err == nil {
// Unknown error detail; return the generic error.
return rh.Error
}
// Make sure that the flags in the generic portion of the error
// match the methods of the specific error type.
if rh.Error.Retryable {
if r, ok := err.(retry.Retryable); !ok || !r.CanRetry() {
panic(fmt.Sprintf("inconsistent error proto; expected %T to be retryable", err))
}
}
if r, ok := err.(TransactionRestartError); ok {
if r.CanRestartTransaction() != rh.Error.TransactionRestart {
panic(fmt.Sprintf("inconsistent error proto; expected %T to have restart mode %v",
err, rh.Error.TransactionRestart))
}
} else {
// Error type doesn't implement TransactionRestartError, so expect it to have the default.
if rh.Error.TransactionRestart != TransactionRestart_ABORT {
panic(fmt.Sprintf("inconsistent error proto; expected %T to have restart mode ABORT", err))
}
}
return err
return rh.Error.GoError()
}

// SetGoError converts the specified type into either one of the proto-
Expand Down
34 changes: 34 additions & 0 deletions proto/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,40 @@ func (e *Error) Error() string {
return e.Message
}

// GoError returns the non-nil error from the proto.Error union.
func (e *Error) GoError() error {
if e == nil {
return nil
}
if e.Detail == nil {
return e
}
err := e.getDetail()
if err == nil {
// Unknown error detail; return the generic error.
return e
}
// Make sure that the flags in the generic portion of the error
// match the methods of the specific error type.
if e.Retryable {
if r, ok := err.(retry.Retryable); !ok || !r.CanRetry() {
panic(fmt.Sprintf("inconsistent error proto; expected %T to be retryable", err))
}
}
if r, ok := err.(TransactionRestartError); ok {
if r.CanRestartTransaction() != e.TransactionRestart {
panic(fmt.Sprintf("inconsistent error proto; expected %T to have restart mode %v",
err, e.TransactionRestart))
}
} else {
// Error type doesn't implement TransactionRestartError, so expect it to have the default.
if e.TransactionRestart != TransactionRestart_ABORT {
panic(fmt.Sprintf("inconsistent error proto; expected %T to have restart mode ABORT", err))
}
}
return err
}

// CanRetry implements the retry.Retryable interface.
func (e *Error) CanRetry() bool {
return e.Retryable
Expand Down

0 comments on commit 415b85b

Please sign in to comment.