Skip to content

Commit

Permalink
Simplify Go error handling examples (#776)
Browse files Browse the repository at this point in the history
Split error type analysis and error details extraction into separate
examples to keep cyclomatic complexity below lint threshold.

Signed-off-by: Mark S. Lewis <Mark.S.Lewis@outlook.com>
  • Loading branch information
bestbeforetoday authored Jan 2, 2025
1 parent af65337 commit 19b0c65
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions pkg/client/example_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ExampleContract_Evaluate() {
// Specify additional proposal options, such as transient data
)

fmt.Printf("Result: %s, Err: %v", result, err)
fmt.Printf("Result: %s, Err: %v\n", result, err)
}

func ExampleContract_Evaluate_errorHandling() {
Expand All @@ -38,7 +38,7 @@ func ExampleContract_Evaluate_errorHandling() {
}
}

fmt.Printf("Result: %s, Err: %v", result, err)
fmt.Printf("Result: %s, Err: %v\n", result, err)
}

func ExampleContract_Submit() {
Expand All @@ -50,7 +50,7 @@ func ExampleContract_Submit() {
// Specify additional proposal options, such as transient data.
)

fmt.Printf("Result: %s, Err: %v", result, err)
fmt.Printf("Result: %s, Err: %v\n", result, err)
}

func ExampleContract_Submit_errorHandling() {
Expand All @@ -64,26 +64,37 @@ func ExampleContract_Submit_errorHandling() {
var commitErr *client.CommitError

if errors.As(err, &endorseErr) {
fmt.Printf("Endorse error for transaction %s with gRPC status %v: %s\n",
fmt.Printf("Failed to endorse proposal for transaction %s with gRPC status %v: %s\n",
endorseErr.TransactionID, status.Code(endorseErr), endorseErr)
} else if errors.As(err, &submitErr) {
fmt.Printf("Submit error for transaction %s with gRPC status %v: %s\n",
fmt.Printf("Failed to submit endorsed transaction %s to orderer with gRPC status %v: %s\n",
submitErr.TransactionID, status.Code(submitErr), submitErr)
} else if errors.As(err, &commitStatusErr) {
if errors.Is(err, context.DeadlineExceeded) {
fmt.Printf("Timeout waiting for transaction %s commit status: %s",
commitStatusErr.TransactionID, commitStatusErr)
} else {
fmt.Printf("Error obtaining commit status for transaction %s with gRPC status %v: %s\n",
fmt.Printf("Failed to obtain commit status for transaction %s with gRPC status %v: %s\n",
commitStatusErr.TransactionID, status.Code(commitStatusErr), commitStatusErr)
}
} else if errors.As(err, &commitErr) {
fmt.Printf("Transaction %s failed to commit with status %d: %s\n",
commitErr.TransactionID, int32(commitErr.Code), err)
} else {
fmt.Printf("unexpected error type %T: %s", err, err)
fmt.Printf("Unexpected error type %T: %s", err, err)
}
}

fmt.Printf("Result: %s, Err: %v\n", result, err)
}

func ExampleContract_Submit_errorDetails() {
var contract *client.Contract // Obtained from Network.

result, err := contract.Submit("transactionName")
fmt.Printf("Result: %s, Err: %v\n", result, err)

if err != nil {
// Any error that originates from a peer or orderer node external to the gateway will have its details
// embedded within the gRPC status error. The following code shows how to extract that.
for _, detail := range status.Convert(err).Details() {
Expand All @@ -92,11 +103,7 @@ func ExampleContract_Submit_errorHandling() {
fmt.Printf("- address: %s; mspId: %s; message: %s\n", detail.Address, detail.MspId, detail.Message)
}
}

panic(err)
}

fmt.Printf("Result: %s, Err: %v", result, err)
}

func ExampleContract_Submit_privateData() {
Expand Down

0 comments on commit 19b0c65

Please sign in to comment.