diff --git a/internal/commands/readers/readers.go b/internal/commands/readers/readers.go index 46eeef3..706716c 100644 --- a/internal/commands/readers/readers.go +++ b/internal/commands/readers/readers.go @@ -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 { @@ -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 { diff --git a/internal/commands/readers/readers_test.go b/internal/commands/readers/readers_test.go index e3e9f35..f33e427 100644 --- a/internal/commands/readers/readers_test.go +++ b/internal/commands/readers/readers_test.go @@ -1,8 +1,10 @@ package readers import ( + "strings" "testing" + sumup "github.com/sumup/sumup-go" "github.com/urfave/cli/v3" ) @@ -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()