Skip to content

Commit

Permalink
[FAB-9377] Define chaincode status group
Browse files Browse the repository at this point in the history
Change-Id: I976497cf6b08cdeddb64e2ab4cc4ca628d74c475
Signed-off-by: Divyank Katira <Divyank.Katira@securekey.com>
  • Loading branch information
d1vyank committed Apr 5, 2018
1 parent 0a27dbc commit d6f75df
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 46 deletions.
2 changes: 1 addition & 1 deletion pkg/common/errors/retry/invoker_test.go
Expand Up @@ -51,7 +51,7 @@ func TestInvokeError(t *testing.T) {
attempt := 0
expectedResp := "invoked"
firstErr := status.New(status.EndorserClientStatus, status.EndorsementMismatch.ToInt32(), "", nil)
exepectedErr := status.New(status.EndorserClientStatus, status.ChaincodeError.ToInt32(), "", nil)
exepectedErr := status.New(status.ChaincodeStatus, int32(500), "", nil)
invoker := NewInvoker(r)
resp, err := invoker.Invoke(
func() (interface{}, error) {
Expand Down
3 changes: 0 additions & 3 deletions pkg/common/errors/status/codes.go
Expand Up @@ -48,9 +48,6 @@ const (
// MissingEndorsement is if an endoresement is missing
MissingEndorsement Code = 9

// ChaincodeError is for errors returned by Chaincode
ChaincodeError Code = 10

// NoMatchingCertificateAuthorityEntity is if entityMatchers are unable to find any matchingCertificateAuthority
NoMatchingCertificateAuthorityEntity Code = 21

Expand Down
15 changes: 5 additions & 10 deletions pkg/common/errors/status/status.go
Expand Up @@ -72,6 +72,9 @@ const (
OrdererClientStatus
// ClientStatus is a generic client status
ClientStatus

// ChaincodeStatus defines the status codes returned by chaincode
ChaincodeStatus
)

// GroupName maps the groups in this packages to human-readable strings
Expand Down Expand Up @@ -163,16 +166,8 @@ func NewFromGRPCStatus(s *grpcstatus.Status) *Status {
Message: s.Message(), Details: details}
}

// ChaincodeStatus is for extracting Code and message from chaincode GRPC errors
type ChaincodeStatus struct {
Code int
Message string
}

// NewFromExtractedChaincodeError returns Status when a chaincode error occurs
func NewFromExtractedChaincodeError(code int, message string) *Status {
status := &ChaincodeStatus{Code: code, Message: message}

return &Status{Group: ClientStatus, Code: ChaincodeError.ToInt32(),
Message: message, Details: []interface{}{status}}
return &Status{Group: ChaincodeStatus, Code: int32(code),
Message: message, Details: nil}
}
6 changes: 6 additions & 0 deletions pkg/common/errors/status/status_test.go
Expand Up @@ -117,3 +117,9 @@ func TestStatusGroupString(t *testing.T) {
unknownGroup77377 := Group(73777)
assert.Equal(t, UnknownStatus.String(), unknownGroup77377.String())
}

func TestChaincodeStatus(t *testing.T) {
s := NewFromExtractedChaincodeError(500, "key not found")
assert.Equal(t, "key not found", s.Message)
assert.Equal(t, int32(500), s.Code)
}
16 changes: 0 additions & 16 deletions test/integration/e2e/end_to_end.go
Expand Up @@ -13,7 +13,6 @@ import (
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/status"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
"github.com/stretchr/testify/assert"

Expand Down Expand Up @@ -147,21 +146,6 @@ func Run(t *testing.T, configOpt core.ConfigProvider, sdkOpts ...fabsdk.Option)
}
defer client.UnregisterChaincodeEvent(reg)

// Try calling unknown function call and expect an error
response, err = client.Execute(channel.Request{ChaincodeID: ccID, Fcn: "DUMMY_FUNCTION", Args: integration.ExampleCCTxArgs()},
channel.WithRetry(retry.DefaultChClientOpts))
if err == nil {
t.Fatal("Should have failed with dummy function")
}
s, ok := status.FromError(err)
assert.True(t, ok, "expected GRPC status error")
assert.EqualValues(t, status.ChaincodeError, s.Code, "expected ChaincodeError")
assert.True(t, len(s.Details) > 0, "expected Details to exist in ChaincodeError")
chaincodeQueryStatus := s.Details[0].(*status.ChaincodeStatus)
if chaincodeQueryStatus.Code != 500 {
t.Fatalf("Expected 500 grpc status code in ChaincodeError")
}

// Move funds
response, err = client.Execute(channel.Request{ChaincodeID: ccID, Fcn: "invoke", Args: integration.ExampleCCTxArgs()},
channel.WithRetry(retry.DefaultChClientOpts))
Expand Down
24 changes: 8 additions & 16 deletions test/integration/orgs/multiple_orgs_test.go
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
packager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/gopackager"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"

"github.com/hyperledger/fabric-sdk-go/pkg/client/ledger"
Expand Down Expand Up @@ -196,22 +195,15 @@ func testWithOrg1(t *testing.T, sdk *fabsdk.FabricSDK) int {
// Call with a dummy function and expect a fail with multiple errors
response, err := chClientOrg1User.Query(channel.Request{ChaincodeID: "exampleCC", Fcn: "DUMMY_FUNCTION", Args: integration.ExampleCCQueryArgs()},
channel.WithRetry(retry.DefaultChClientOpts))
if err == nil {
t.Fatal("Should have failed with dummy function")
}
unWrappedError := errors.Cause(err)
if _, ok := unWrappedError.(multi.Errors); !ok {
t.Fatal("Should have got multiple errors")
}
for _, err := range unWrappedError.(multi.Errors) {
assert.Error(t, err, "Should have failed with dummy function")
s, ok := status.FromError(err)
assert.True(t, ok, "expected status error")
assert.Equal(t, s.Code, int32(status.MultipleErrors))
for _, err := range err.(multi.Errors) {
s, ok := status.FromError(err)
assert.True(t, ok, "expected GRPC status error")
assert.EqualValues(t, status.ChaincodeError, s.Code, "expected ChaincodeError")
assert.True(t, len(s.Details) > 0, "expected Details to exist in ChaincodeError")
chaincodeQueryStatus := s.Details[0].(*status.ChaincodeStatus)
if chaincodeQueryStatus.Code != 500 {
t.Fatalf("Expected 500 grpc status code in ChaincodeError")
}
assert.True(t, ok, "expected status error")
assert.EqualValues(t, int32(500), s.Code)
assert.Equal(t, status.ChaincodeStatus, s.Group)
}

// Org1 user queries initial value on both peers
Expand Down
17 changes: 17 additions & 0 deletions test/integration/sdk/channel_client_test.go
Expand Up @@ -11,10 +11,12 @@ import (
"time"

pb "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/protos/peer"
"github.com/stretchr/testify/assert"

"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel/invoke"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/status"

"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
Expand Down Expand Up @@ -110,6 +112,9 @@ func TestChannelClient(t *testing.T) {
// Test invocation of custom handler
testInvokeHandler(chaincodeID, chClient, t)

// Test chaincode error
testChaincodeError(chaincodeID, chClient, t)

// Test receive event using separate client
listener, err := channel.New(org1ChannelClientContext)
if err != nil {
Expand Down Expand Up @@ -307,3 +312,15 @@ func testChaincodeEventListener(ccID string, chClient *channel.Client, listener
}

}

func testChaincodeError(ccID string, client *channel.Client, t *testing.T) {
// Try calling unknown function call and expect an error
_, err := client.Execute(channel.Request{ChaincodeID: ccID, Fcn: "DUMMY_FUNCTION", Args: integration.ExampleCCTxArgs()},
channel.WithRetry(retry.DefaultChClientOpts))
assert.Error(t, err)
s, ok := status.FromError(err)
assert.True(t, ok, "expected status error")
assert.EqualValues(t, status.ChaincodeStatus, s.Group, "expected ChaincodeStatus")
assert.Equal(t, int32(500), s.Code)
assert.Equal(t, "Unknown function call", s.Message)
}

0 comments on commit d6f75df

Please sign in to comment.