Skip to content

Commit 7c46e11

Browse files
committed
Enhance error handling in Fabric chaincode operations
- Improved error handling in the ApproveFabricChaincode and CommitFabricChaincode methods to provide more detailed feedback on endorsement errors. - Enhanced error messages to include specific details from gRPC status responses, improving clarity for users during chaincode approval and commit processes. - Updated logging to ensure consistent error reporting across chaincode operations, enhancing overall maintainability and user experience. These changes significantly improve the robustness of chaincode management by providing clearer insights into errors encountered during operations. Signed-off-by: dviejokfs <dviejo@kfs.es>
1 parent 212fcd4 commit 7c46e11

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

pkg/chainlaunchdeploy/handler.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import (
1717
"github.com/go-chi/chi/v5"
1818
"github.com/go-playground/validator/v10"
1919
"github.com/google/uuid"
20+
fabricclient "github.com/hyperledger/fabric-gateway/pkg/client"
21+
"github.com/hyperledger/fabric-protos-go-apiv2/gateway"
22+
"google.golang.org/grpc/status"
2023
)
2124

2225
// Handler handles HTTP requests for smart contract deployment
@@ -450,8 +453,22 @@ func (h *Handler) ApproveFabricChaincode(w http.ResponseWriter, r *http.Request)
450453
reporter := NewInMemoryDeploymentStatusReporter()
451454
result, err := ApproveChaincode(params, reporter)
452455
if err != nil {
453-
h.logger.Error("Fabric chaincode approve failed", "error", err)
454-
return errors.NewInternalError("approve failed", err, nil)
456+
endorseError, ok := err.(*fabricclient.EndorseError)
457+
if ok {
458+
detailsStr := []string{}
459+
for _, detail := range status.Convert(err).Details() {
460+
switch detail := detail.(type) {
461+
case *gateway.ErrorDetail:
462+
detailsStr = append(detailsStr, fmt.Sprintf("- address: %s; mspId: %s; message: %s\n", detail.GetAddress(), detail.GetMspId(), detail.GetMessage()))
463+
}
464+
}
465+
err = fmt.Errorf("failed to approve chaincode: %s (gRPC status: %s)",
466+
endorseError.TransactionError.Error(),
467+
strings.Join(detailsStr, "\n"))
468+
} else {
469+
err = fmt.Errorf("failed to approve chaincode: %w", err)
470+
}
471+
return err
455472
}
456473
resp := FabricApproveResponse{
457474
Status: "success",
@@ -523,8 +540,22 @@ func (h *Handler) CommitFabricChaincode(w http.ResponseWriter, r *http.Request)
523540
reporter := NewInMemoryDeploymentStatusReporter()
524541
result, err := CommitChaincode(params, reporter)
525542
if err != nil {
526-
h.logger.Error("Fabric chaincode commit failed", "error", err)
527-
return errors.NewInternalError("commit failed", err, nil)
543+
endorseError, ok := err.(*fabricclient.EndorseError)
544+
if ok {
545+
detailsStr := []string{}
546+
for _, detail := range status.Convert(err).Details() {
547+
switch detail := detail.(type) {
548+
case *gateway.ErrorDetail:
549+
detailsStr = append(detailsStr, fmt.Sprintf("- address: %s; mspId: %s; message: %s\n", detail.GetAddress(), detail.GetMspId(), detail.GetMessage()))
550+
}
551+
}
552+
err = fmt.Errorf("failed to approve chaincode: %s (gRPC status: %s)",
553+
endorseError.TransactionError.Error(),
554+
strings.Join(detailsStr, "\n"))
555+
} else {
556+
err = fmt.Errorf("failed to approve chaincode: %w", err)
557+
}
558+
return err
528559
}
529560
resp := FabricCommitResponse{
530561
Status: "success",

0 commit comments

Comments
 (0)