From 64433098449245c6a2a52443861fed40b8389168 Mon Sep 17 00:00:00 2001 From: shruti2522 Date: Wed, 17 Apr 2024 21:57:32 +0530 Subject: [PATCH] feat:simplify error messages on tuple write corrected indentation feat:simplify error messages on tuple write resolved linting issues updated extractErrMssg function Signed-off-by: shruti2522 resolved linting issues Signed-off-by: shruti2522 feat:simplify error messages on tuple write Signed-off-by: shruti2522 --- cmd/tuple/import.go | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/cmd/tuple/import.go b/cmd/tuple/import.go index d7424d1..890ad24 100644 --- a/cmd/tuple/import.go +++ b/cmd/tuple/import.go @@ -20,6 +20,7 @@ import ( "context" "fmt" "os" + "strings" openfga "github.com/openfga/go-sdk" "github.com/openfga/go-sdk/client" @@ -81,6 +82,20 @@ func ImportTuples( return &result, nil } +func extractErrMssg(err error) string { + errorMsg := err.Error() + startIndex := strings.Index(errorMsg, "error message:") + + if startIndex == -1 { + return errorMsg + } + + errorMsg = errorMsg[startIndex:] + errorMsg = strings.TrimSpace(errorMsg) + + return errorMsg +} + func processWrites( writes []client.ClientWriteRequestWriteResponse, ) ([]client.ClientTupleKey, []failedWriteResponse) { @@ -93,9 +108,10 @@ func processWrites( if write.Status == client.SUCCESS { successfulWrites = append(successfulWrites, write.TupleKey) } else { + reason := extractErrMssg(write.Error) failedWrites = append(failedWrites, failedWriteResponse{ TupleKey: write.TupleKey, - Reason: write.Error.Error(), + Reason: reason, }) } } @@ -121,9 +137,10 @@ func processDeletes( if delete.Status == client.SUCCESS { successfulDeletes = append(successfulDeletes, deletedTupleKey) } else { + reason := extractErrMssg(delete.Error) failedDeletes = append(failedDeletes, failedWriteResponse{ TupleKey: deletedTupleKey, - Reason: delete.Error.Error(), + Reason: reason, }) } }