Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Go error handling examples #776

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading