Skip to content

Commit

Permalink
feat: simplify error messages on tuple write (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
ewanharris authored Apr 26, 2024
2 parents 41075bc + 6443309 commit facb1ea
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions cmd/tuple/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"os"
"strings"

openfga "github.com/openfga/go-sdk"
"github.com/openfga/go-sdk/client"
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
})
}
}
Expand All @@ -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,
})
}
}
Expand Down

0 comments on commit facb1ea

Please sign in to comment.