Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions internal/commands/readers/readers.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,8 @@ func addReader(ctx context.Context, cmd *cli.Command) error {
}

reader, err := appCtx.Client.Readers.Create(ctx, merchantCode, body)
if pErr := new(sumup.Problem); errors.As(err, &pErr) {
return fmt.Errorf("create reader: %v %v", *pErr.Detail, *pErr.Title)
} else if err != nil {
return fmt.Errorf("create reader: %w", err)
if err != nil {
return formatCreateReaderError(err)
}

if appCtx.JSONOutput {
Expand All @@ -277,6 +275,13 @@ func addReader(ctx context.Context, cmd *cli.Command) error {
})
}

func formatCreateReaderError(err error) error {
if pErr := new(sumup.Problem); errors.As(err, &pErr) {
return fmt.Errorf("create reader: %w", pErr)
}
return fmt.Errorf("create reader: %w", err)
}

func deleteReader(ctx context.Context, cmd *cli.Command) error {
appCtx, err := app.GetAppContext(cmd)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions internal/commands/readers/readers_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package readers

import (
"strings"
"testing"

sumup "github.com/sumup/sumup-go"
"github.com/urfave/cli/v3"
)

Expand All @@ -18,6 +20,17 @@ func TestContextAwareReaderCommandsDoNotRequireMerchantCodeFlag(t *testing.T) {
}
}

func TestCreateReaderProblemErrorDoesNotPanicOnMissingFields(t *testing.T) {
err := formatCreateReaderError(&sumup.Problem{})

if err == nil {
t.Fatal("expected formatted error")
}
if !strings.Contains(err.Error(), "create reader:") {
t.Fatalf("error = %q, want create reader prefix", err.Error())
}
}

func findSubcommand(t *testing.T, cmd *cli.Command, name string) *cli.Command {
t.Helper()

Expand Down