Skip to content

Commit

Permalink
Merge pull request #153 from maxlaverse/log_more_details_on_error
Browse files Browse the repository at this point in the history
log more details on error
  • Loading branch information
maxlaverse authored Aug 25, 2024
2 parents 6dc6e27 + 688448c commit bb06473
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 8 deletions.
19 changes: 13 additions & 6 deletions internal/bitwarden/bw/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions internal/bitwarden/bw/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''")
}
}
5 changes: 3 additions & 2 deletions internal/bitwarden/bw/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"regexp"
"strings"

"github.com/maxlaverse/terraform-provider-bitwarden/internal/command"
)
Expand All @@ -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 {
Expand Down

0 comments on commit bb06473

Please sign in to comment.