Skip to content

Commit d29aa76

Browse files
authored
Merge pull request #274 from moovfinancial/ledg-3715-fixes
invoices: fix err response on CreateInvoice
2 parents 871ff4c + 791c87e commit d29aa76

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

pkg/moov/call.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func callStatus(name string, retryable bool) CallStatus {
2424
}
2525

2626
var (
27-
StatusCompleted = callStatus("completed", false) // Completely fully
27+
StatusCompleted = callStatus("completed", false) // Completed fully
2828
StatusStarted = callStatus("started", true) // Returned as async. This can be due to timing out, or started as async
2929

3030
StatusBadRequest = callStatus("bad_request", false) // bad request, body, headers, etc...
@@ -295,6 +295,16 @@ func CompletedListOrError[A interface{}](resp CallResponse) ([]A, error) {
295295
}
296296
}
297297

298+
// Helper for a common pattern of successful API calls returning an object body or an error
299+
func StartedObjectOrError[A interface{}](resp CallResponse) (*A, error) {
300+
switch resp.Status() {
301+
case StatusStarted:
302+
return UnmarshalObjectResponse[A](resp)
303+
default:
304+
return nil, resp
305+
}
306+
}
307+
298308
var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
299309

300310
func escapeQuotes(s string) string {

pkg/moov/invoice_api.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ func (c Client) CreateInvoice(ctx context.Context, accountID string, invoice Cre
1414
JsonBody(invoice),
1515
)
1616
if err != nil {
17-
return nil, err
17+
return nil, fmt.Errorf("calling http: %w", err)
1818
}
1919

20-
out, err := UnmarshalObjectResponse[Invoice](resp)
21-
return out, err
20+
return StartedObjectOrError[Invoice](resp)
2221
}
2322

2423
// GetInvoice retrieves an invoice by ID
@@ -28,7 +27,7 @@ func (c Client) GetInvoice(ctx context.Context, accountID, invoiceID string) (*I
2827
AcceptJson(),
2928
)
3029
if err != nil {
31-
return nil, err
30+
return nil, fmt.Errorf("calling http: %w", err)
3231
}
3332

3433
return CompletedObjectOrError[Invoice](resp)
@@ -42,7 +41,7 @@ func (c Client) UpdateInvoice(ctx context.Context, accountID, invoiceID string,
4241
JsonBody(invoice),
4342
)
4443
if err != nil {
45-
return nil, err
44+
return nil, fmt.Errorf("calling http: %w", err)
4645
}
4746

4847
return CompletedObjectOrError[Invoice](resp)
@@ -56,7 +55,7 @@ func (c Client) MarkInvoicePaid(ctx context.Context, accountID, invoiceID string
5655
JsonBody(payment),
5756
)
5857
if err != nil {
59-
return nil, err
58+
return nil, fmt.Errorf("calling http: %w", err)
6059
}
6160

6261
return CompletedObjectOrError[Invoice](resp)
@@ -70,7 +69,7 @@ func (c Client) MarkInvoicePaid(ctx context.Context, accountID, invoiceID string
7069
// AcceptJson(),
7170
// )
7271
// if err != nil {
73-
// return nil, err
72+
// return nil, fmt.Errorf("calling http: %w), err
7473
// }
7574

7675
// return CompletedObjectOrError[Invoice](resp)
@@ -117,7 +116,7 @@ func (c Client) ListInvoices(ctx context.Context, accountID string, filters ...L
117116
args := prependArgs(filters, AcceptJson())
118117
resp, err := c.CallHttp(ctx, Endpoint(http.MethodGet, pathInvoices, accountID), args...)
119118
if err != nil {
120-
return nil, err
119+
return nil, fmt.Errorf("calling http: %w", err)
121120
}
122121

123122
return CompletedListOrError[Invoice](resp)

0 commit comments

Comments
 (0)