From 688448c10b95353e9522797ecfb8116de17b67b7 Mon Sep 17 00:00:00 2001 From: Maxime Lagresle Date: Sun, 25 Aug 2024 21:16:32 +0200 Subject: [PATCH] log more details on error --- internal/bitwarden/bw/client.go | 19 +++++++++++++------ internal/bitwarden/bw/client_test.go | 14 ++++++++++++++ internal/bitwarden/bw/errors.go | 5 +++-- 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/internal/bitwarden/bw/client.go b/internal/bitwarden/bw/client.go index d52243c..6729ec5 100644 --- a/internal/bitwarden/bw/client.go +++ b/internal/bitwarden/bw/client.go @@ -103,7 +103,7 @@ func (c *client) CreateObject(ctx context.Context, obj Object) (*Object, error) } err = json.Unmarshal(out, &obj) if err != nil { - return nil, newUnmarshallError(err, "create object", out) + return nil, newUnmarshallError(err, args[0:2], out) } // NOTE(maxime): there is no need to sync after creating an item @@ -134,13 +134,20 @@ func (c *client) EditObject(ctx context.Context, obj Object) (*Object, error) { return nil, err } - out, err := c.cmdWithSession("edit", string(obj.Object), obj.ID, objEncoded).Run(ctx) + args := []string{ + "edit", + string(obj.Object), + obj.ID, + objEncoded, + } + + out, err := c.cmdWithSession(args...).Run(ctx) if err != nil { return nil, err } err = json.Unmarshal(out, &obj) if err != nil { - return nil, newUnmarshallError(err, "edit object", out) + return nil, newUnmarshallError(err, args[0:2], out) } err = c.Sync(ctx) if err != nil { @@ -168,7 +175,7 @@ func (c *client) GetObject(ctx context.Context, obj Object) (*Object, error) { err = json.Unmarshal(out, &obj) if err != nil { - return nil, newUnmarshallError(err, "get object", out) + return nil, newUnmarshallError(err, args[0:2], out) } return &obj, nil @@ -206,7 +213,7 @@ func (c *client) ListObjects(ctx context.Context, objType string, options ...Lis var obj []Object err = json.Unmarshal(out, &obj) if err != nil { - return nil, newUnmarshallError(err, "list object", out) + return nil, newUnmarshallError(err, args[0:2], out) } return obj, nil @@ -272,7 +279,7 @@ func (c *client) Status(ctx context.Context) (*Status, error) { var status Status err = json.Unmarshal(out, &status) if err != nil { - return nil, newUnmarshallError(err, "status", out) + return nil, newUnmarshallError(err, []string{"status"}, out) } return &status, nil diff --git a/internal/bitwarden/bw/client_test.go b/internal/bitwarden/bw/client_test.go index 1f7fe79..445e55b 100644 --- a/internal/bitwarden/bw/client_test.go +++ b/internal/bitwarden/bw/client_test.go @@ -77,3 +77,17 @@ func TestGetOrgCollection(t *testing.T) { assert.Equal(t, "get org-collection object-id --organizationid org-id", commandsExecuted()[0]) } } + +func TestErrorContainsCommand(t *testing.T) { + removeMocks, _ := test_command.MockCommands(t, map[string]string{ + "list org-collection --search search": ``, + }) + defer removeMocks(t) + + b := NewClient("dummy") + _, err := b.ListObjects(context.Background(), "org-collection", WithSearch("search")) + + if assert.Error(t, err) { + assert.ErrorContains(t, err, "unable to parse result of 'list org-collection', error: 'unexpected end of JSON input', output: ''") + } +} diff --git a/internal/bitwarden/bw/errors.go b/internal/bitwarden/bw/errors.go index 268cc04..11fd0c5 100644 --- a/internal/bitwarden/bw/errors.go +++ b/internal/bitwarden/bw/errors.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "regexp" + "strings" "github.com/maxlaverse/terraform-provider-bitwarden/internal/command" ) @@ -15,8 +16,8 @@ var ( attachmentNotFoundRegexp = regexp.MustCompile(`^Attachment .* was not found.$`) ) -func newUnmarshallError(err error, cmd string, out []byte) error { - return fmt.Errorf("unable to parse result of '%s', error: '%v', output: '%v'", cmd, err, string(out)) +func newUnmarshallError(err error, args []string, out []byte) error { + return fmt.Errorf("unable to parse result of '%s', error: '%v', output: '%v'", strings.Join(args, " "), err, string(out)) } func remapError(err error) error {