From 9c4b0c4c0d4a3b1474ef8bc0408733e032560b40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Dzivjak?= Date: Mon, 26 Oct 2026 09:20:29 +0100 Subject: [PATCH 1/2] feat!: preserve HTTP context alongside typed API errors Expose typed Problem details and preserve error response context. Add focused regression coverage and concise integration examples. BREAKING CHANGE: API errors now wrap documented bodies. Use errors.As to inspect typed errors instead of direct assertions. --- DOCUMENTATION.md | 99 ++++++---- README.md | 21 +++ api_error.go | 38 ++++ api_error_test.go | 40 +++++ checkouts.go | 130 +++----------- customers.go | 112 +++--------- .../cmd/codegen/templates/resource.go.tmpl | 12 +- members.go | 76 ++------ memberships.go | 14 +- merchants.go | 24 +-- payouts.go | 14 +- readers.go | 170 ++++-------------- receipts.go | 20 +-- roles.go | 58 ++---- transactions.go | 60 ++----- 15 files changed, 311 insertions(+), 577 deletions(-) create mode 100644 api_error.go create mode 100644 api_error_test.go diff --git a/DOCUMENTATION.md b/DOCUMENTATION.md index cad94ea0..bd01e1ea 100644 --- a/DOCUMENTATION.md +++ b/DOCUMENTATION.md @@ -9,6 +9,9 @@ import "github.com/sumup/sumup-go" ## Index - [Variables](<#variables>) +- [type APIError](<#APIError>) + - [func \(e \*APIError\) Error\(\) string](<#APIError.Error>) + - [func \(e \*APIError\) Unwrap\(\) error](<#APIError.Unwrap>) - [type Address](<#Address>) - [type AddressLegacy](<#AddressLegacy>) - [type Affiliate](<#Affiliate>) @@ -346,6 +349,38 @@ var OAuth2Endpoint = oauth2.Endpoint{ } ``` + +## type [APIError]() + +APIError preserves the HTTP response for an unsuccessful API request. Use errors.As to inspect a documented body such as \*Problem. + +```go +type APIError struct { + StatusCode int + Headers http.Header + Body []byte + Err error +} +``` + + +### func \(\*APIError\) [Error]() + +```go +func (e *APIError) Error() string +``` + + + + +### func \(\*APIError\) [Unwrap]() + +```go +func (e *APIError) Unwrap() error +``` + +Unwrap returns the typed API body or the response\-reading error. + ## type [Address]() @@ -1219,7 +1254,7 @@ func NewCheckoutsClient(c *client.Client) *CheckoutsClient -### func \(\*CheckoutsClient\) [Create]() +### func \(\*CheckoutsClient\) [Create]() ```go func (c *CheckoutsClient) Create(ctx context.Context, body CheckoutsCreateParams) (*Checkout, error) @@ -1232,7 +1267,7 @@ Set \`hosted\_checkout.enabled\` to \`true\` to receive a \[Hosted Checkout\]\(h Complete the payment through \[Hosted Checkout\]\(https://developer.sumup.com/online-payments/checkouts/hosted-checkout/\) or the \[Payment Widget\]\(https://developer.sumup.com/online-payments/checkouts/card-widget\). -### func \(\*CheckoutsClient\) [CreateApplePaySession]() +### func \(\*CheckoutsClient\) [CreateApplePaySession]() ```go func (c *CheckoutsClient) CreateApplePaySession(ctx context.Context, checkoutID string, body CheckoutsCreateApplePaySessionParams) (*CheckoutsCreateApplePaySessionResponse, error) @@ -1243,7 +1278,7 @@ Creates an Apple Pay merchant session for the specified checkout. Use this endpoint after the customer selects Apple Pay and before calling \`ApplePaySession.completeMerchantValidation\(...\)\` in the browser. SumUp validates the merchant session request and returns the Apple Pay session object that your frontend should pass to Apple's JavaScript API. -### func \(\*CheckoutsClient\) [Deactivate]() +### func \(\*CheckoutsClient\) [Deactivate]() ```go func (c *CheckoutsClient) Deactivate(ctx context.Context, checkoutID string) (*Checkout, error) @@ -1252,7 +1287,7 @@ func (c *CheckoutsClient) Deactivate(ctx context.Context, checkoutID string) (*C Deactivates an identified checkout resource. If the checkout has already been processed it can not be deactivated. -### func \(\*CheckoutsClient\) [Get]() +### func \(\*CheckoutsClient\) [Get]() ```go func (c *CheckoutsClient) Get(ctx context.Context, checkoutID string) (*CheckoutSuccess, error) @@ -1270,7 +1305,7 @@ func (c *CheckoutsClient) List(ctx context.Context, params CheckoutsListParams) Lists created checkout resources according to the applied \`checkout\_reference\`. -### func \(\*CheckoutsClient\) [ListAvailablePaymentMethods]() +### func \(\*CheckoutsClient\) [ListAvailablePaymentMethods]() ```go func (c *CheckoutsClient) ListAvailablePaymentMethods(ctx context.Context, merchantCode string, params CheckoutsListAvailablePaymentMethodsParams) (*CheckoutsListAvailablePaymentMethodsResponse, error) @@ -1279,7 +1314,7 @@ func (c *CheckoutsClient) ListAvailablePaymentMethods(ctx context.Context, merch Get payment methods available for the given merchant to use with a checkout. -### func \(\*CheckoutsClient\) [Process]() +### func \(\*CheckoutsClient\) [Process]() ```go func (c *CheckoutsClient) Process(ctx context.Context, checkoutID string, body CheckoutsProcessParams) (*CheckoutsProcessResponse, error) @@ -1292,7 +1327,7 @@ Processing a checkout will attempt to charge the provided payment instrument for Follow this request with \`Retrieve a checkout\` to confirm its status. -### func \(\*CheckoutsClient\) [Update]() +### func \(\*CheckoutsClient\) [Update]() ```go func (c *CheckoutsClient) Update(ctx context.Context, checkoutID string, body CheckoutsUpdateParams) (*Checkout, error) @@ -2180,7 +2215,7 @@ func (c *CustomersClient) Create(ctx context.Context, body CustomersCreateParams Creates a new saved customer resource which you can later manipulate and save payment instruments to. -### func \(\*CustomersClient\) [DeactivatePaymentInstrument]() +### func \(\*CustomersClient\) [DeactivatePaymentInstrument]() ```go func (c *CustomersClient) DeactivatePaymentInstrument(ctx context.Context, customerID string, token string) error @@ -2189,7 +2224,7 @@ func (c *CustomersClient) DeactivatePaymentInstrument(ctx context.Context, custo Deactivates an identified card payment instrument resource for a customer. -### func \(\*CustomersClient\) [Get]() +### func \(\*CustomersClient\) [Get]() ```go func (c *CustomersClient) Get(ctx context.Context, customerID string) (*Customer, error) @@ -2198,7 +2233,7 @@ func (c *CustomersClient) Get(ctx context.Context, customerID string) (*Customer Retrieves an identified saved customer resource through the unique \`customer\_id\` parameter, generated upon customer creation. -### func \(\*CustomersClient\) [ListPaymentInstruments]() +### func \(\*CustomersClient\) [ListPaymentInstruments]() ```go func (c *CustomersClient) ListPaymentInstruments(ctx context.Context, customerID string) (*CustomersListPaymentInstrumentsResponse, error) @@ -2207,7 +2242,7 @@ func (c *CustomersClient) ListPaymentInstruments(ctx context.Context, customerID Lists all payment instrument resources that are saved for an identified customer. -### func \(\*CustomersClient\) [Update]() +### func \(\*CustomersClient\) [Update]() ```go func (c *CustomersClient) Update(ctx context.Context, customerID string, body CustomersUpdateParams) (*Customer, error) @@ -2943,7 +2978,7 @@ func NewMembersClient(c *client.Client) *MembersClient -### func \(\*MembersClient\) [Create]() +### func \(\*MembersClient\) [Create]() ```go func (c *MembersClient) Create(ctx context.Context, merchantCode string, body MembersCreateParams) (*Member, error) @@ -2954,7 +2989,7 @@ Adds a member to the merchant account with the specified roles. By default, sends an invitation email to the provided address. The recipient must accept the invitation to join the account. When \`is\_managed\_user\` is \`true\`, creates a managed user with the provided password and optional nickname and assigns the roles directly, without sending an invitation. -### func \(\*MembersClient\) [Delete]() +### func \(\*MembersClient\) [Delete]() ```go func (c *MembersClient) Delete(ctx context.Context, merchantCode string, memberID string) error @@ -2963,7 +2998,7 @@ func (c *MembersClient) Delete(ctx context.Context, merchantCode string, memberI Deletes a merchant member. -### func \(\*MembersClient\) [Get]() +### func \(\*MembersClient\) [Get]() ```go func (c *MembersClient) Get(ctx context.Context, merchantCode string, memberID string) (*Member, error) @@ -2981,7 +3016,7 @@ func (c *MembersClient) List(ctx context.Context, merchantCode string, params Me Lists merchant members. -### func \(\*MembersClient\) [Update]() +### func \(\*MembersClient\) [Update]() ```go func (c *MembersClient) Update(ctx context.Context, merchantCode string, memberID string, body MembersUpdateParams) (*Member, error) @@ -3445,7 +3480,7 @@ func NewMerchantsClient(c *client.Client) *MerchantsClient -### func \(\*MerchantsClient\) [Get]() +### func \(\*MerchantsClient\) [Get]() ```go func (c *MerchantsClient) Get(ctx context.Context, merchantCode string) (*Merchant, error) @@ -3454,7 +3489,7 @@ func (c *MerchantsClient) Get(ctx context.Context, merchantCode string) (*Mercha Returns a Merchant for a valid Merchant code. Merchant documentation: https://developer.sumup.com/tools/models/merchant -### func \(\*MerchantsClient\) [GetPerson]() +### func \(\*MerchantsClient\) [GetPerson]() ```go func (c *MerchantsClient) GetPerson(ctx context.Context, merchantCode string, personID string) (*Person, error) @@ -4252,7 +4287,7 @@ func NewReadersClient(c *client.Client) *ReadersClient -### func \(\*ReadersClient\) [Create]() +### func \(\*ReadersClient\) [Create]() ```go func (c *ReadersClient) Create(ctx context.Context, merchantCode string, body ReadersCreateParams) (*Reader, error) @@ -4261,7 +4296,7 @@ func (c *ReadersClient) Create(ctx context.Context, merchantCode string, body Re Create a new Reader for the merchant account. -### func \(\*ReadersClient\) [CreateCheckout]() +### func \(\*ReadersClient\) [CreateCheckout]() ```go func (c *ReadersClient) CreateCheckout(ctx context.Context, merchantCode string, readerID string, body ReadersCreateCheckoutParams) (*CreateReaderCheckoutResponse, error) @@ -4276,7 +4311,7 @@ There are some caveats when using this endpoint: \* The target device must be on \*\*Note\*\*: If the target device is a Solo, it must be in version 3.3.24.3 or higher. -### func \(\*ReadersClient\) [CreateGoCheckout]() +### func \(\*ReadersClient\) [CreateGoCheckout]() ```go func (c *ReadersClient) CreateGoCheckout(ctx context.Context, merchantCode string, readerID ReaderID, body ReadersCreateGoCheckoutParams) (*ReaderPaymentResponse, error) @@ -4287,7 +4322,7 @@ Initiates a payment on the SumUp Go terminal identified by the reader ID. Use \`client\_transaction\_id\` as an idempotency key: retrying the request with the same value returns the result of the original payment instead of creating a duplicate. -### func \(\*ReadersClient\) [Delete]() +### func \(\*ReadersClient\) [Delete]() ```go func (c *ReadersClient) Delete(ctx context.Context, merchantCode string, readerID ReaderID) error @@ -4296,7 +4331,7 @@ func (c *ReadersClient) Delete(ctx context.Context, merchantCode string, readerI Delete a reader. -### func \(\*ReadersClient\) [Get]() +### func \(\*ReadersClient\) [Get]() ```go func (c *ReadersClient) Get(ctx context.Context, merchantCode string, readerID ReaderID, params ReadersGetParams) (*Reader, error) @@ -4305,7 +4340,7 @@ func (c *ReadersClient) Get(ctx context.Context, merchantCode string, readerID R Retrieve a Reader. -### func \(\*ReadersClient\) [GetCheckout]() +### func \(\*ReadersClient\) [GetCheckout]() ```go func (c *ReadersClient) GetCheckout(ctx context.Context, merchantCode string, readerID string, checkoutID string) (*GetReaderCheckoutResponse, error) @@ -4314,7 +4349,7 @@ func (c *ReadersClient) GetCheckout(ctx context.Context, merchantCode string, re Get a Checkout for a Reader. -### func \(\*ReadersClient\) [GetStatus]() +### func \(\*ReadersClient\) [GetStatus]() ```go func (c *ReadersClient) GetStatus(ctx context.Context, merchantCode string, readerID string) (*StatusResponse, error) @@ -4344,7 +4379,7 @@ func (c *ReadersClient) List(ctx context.Context, merchantCode string) (*Readers List all readers of the merchant. -### func \(\*ReadersClient\) [TerminateCheckout]() +### func \(\*ReadersClient\) [TerminateCheckout]() ```go func (c *ReadersClient) TerminateCheckout(ctx context.Context, merchantCode string, readerID string) error @@ -4361,7 +4396,7 @@ If a transaction is successfully terminated and \`return\_url\` was provided on \*\*Note\*\*: If the target device is a Solo, it must be in version 3.3.28.0 or higher. -### func \(\*ReadersClient\) [Update]() +### func \(\*ReadersClient\) [Update]() ```go func (c *ReadersClient) Update(ctx context.Context, merchantCode string, readerID ReaderID, body ReadersUpdateParams) (*Reader, error) @@ -4879,7 +4914,7 @@ func NewRolesClient(c *client.Client) *RolesClient -### func \(\*RolesClient\) [Create]() +### func \(\*RolesClient\) [Create]() ```go func (c *RolesClient) Create(ctx context.Context, merchantCode string, body RolesCreateParams) (*Role, error) @@ -4888,7 +4923,7 @@ func (c *RolesClient) Create(ctx context.Context, merchantCode string, body Role Create a custom role for the merchant. Roles are defined by the set of permissions that they grant to the members that they are assigned to. -### func \(\*RolesClient\) [Delete]() +### func \(\*RolesClient\) [Delete]() ```go func (c *RolesClient) Delete(ctx context.Context, merchantCode string, roleID string) error @@ -4897,7 +4932,7 @@ func (c *RolesClient) Delete(ctx context.Context, merchantCode string, roleID st Delete a custom role. -### func \(\*RolesClient\) [Get]() +### func \(\*RolesClient\) [Get]() ```go func (c *RolesClient) Get(ctx context.Context, merchantCode string, roleID string) (*Role, error) @@ -4915,7 +4950,7 @@ func (c *RolesClient) List(ctx context.Context, merchantCode string) (*RolesList List merchant's custom roles. -### func \(\*RolesClient\) [Update]() +### func \(\*RolesClient\) [Update]() ```go func (c *RolesClient) Update(ctx context.Context, merchantCode string, roleID string, body RolesUpdateParams) (*Role, error) @@ -5918,7 +5953,7 @@ func NewTransactionsClient(c *client.Client) *TransactionsClient -### func \(\*TransactionsClient\) [Get]() +### func \(\*TransactionsClient\) [Get]() ```go func (c *TransactionsClient) Get(ctx context.Context, merchantCode string, params TransactionsGetParams) (*TransactionFull, error) @@ -5936,7 +5971,7 @@ func (c *TransactionsClient) List(ctx context.Context, merchantCode string, para Lists detailed history of all transactions associated with the merchant profile. -### func \(\*TransactionsClient\) [Refund]() +### func \(\*TransactionsClient\) [Refund]() ```go func (c *TransactionsClient) Refund(ctx context.Context, merchantCode string, transactionID string, body TransactionsRefundParams) (*TransactionsRefundResponse, error) diff --git a/README.md b/README.md index 40caf5a2..7b046581 100644 --- a/README.md +++ b/README.md @@ -101,3 +101,24 @@ For question specifically related to this Golang SDK please [Open an Issue](http `sumup-go` SDK will always support latest 3 version of golang following the Golang [Release Policy](https://go.dev/doc/devel/release). [docs-badge]: https://img.shields.io/badge/SumUp-documentation-white.svg?logo=data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgY29sb3I9IndoaXRlIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogICAgPHBhdGggZD0iTTIyLjI5IDBIMS43Qy43NyAwIDAgLjc3IDAgMS43MVYyMi4zYzAgLjkzLjc3IDEuNyAxLjcxIDEuN0gyMi4zYy45NCAwIDEuNzEtLjc3IDEuNzEtMS43MVYxLjdDMjQgLjc3IDIzLjIzIDAgMjIuMjkgMFptLTcuMjIgMTguMDdhNS42MiA1LjYyIDAgMCAxLTcuNjguMjQuMzYuMzYgMCAwIDEtLjAxLS40OWw3LjQ0LTcuNDRhLjM1LjM1IDAgMCAxIC40OSAwIDUuNiA1LjYgMCAwIDEtLjI0IDcuNjlabTEuNTUtMTEuOS03LjQ0IDcuNDVhLjM1LjM1IDAgMCAxLS41IDAgNS42MSA1LjYxIDAgMCAxIDcuOS03Ljk2bC4wMy4wM2MuMTMuMTMuMTQuMzUuMDEuNDlaIiBmaWxsPSJjdXJyZW50Q29sb3IiLz4KPC9zdmc+ + +## Handling errors + +Use `errors.As` to read typed Problem details: + +```go +var problem *sumup.Problem +if errors.As(err, &problem) { + fmt.Println(problem.Type) + if problem.Detail != nil { + fmt.Println(*problem.Detail) + } +} +var apiError *sumup.APIError +if errors.As(err, &apiError) { + fmt.Println(apiError.StatusCode, string(apiError.Body)) +} +``` + +`APIError.Headers` contains the response headers. Connection failures retain their +original Go error, so `errors.Is` and `errors.As` continue to work. diff --git a/api_error.go b/api_error.go new file mode 100644 index 00000000..3b1eab5e --- /dev/null +++ b/api_error.go @@ -0,0 +1,38 @@ +package sumup + +import ( + "encoding/json" + "fmt" + "io" + "net/http" +) + +// APIError preserves the HTTP response for an unsuccessful API request. +// Use errors.As to inspect a documented body such as *Problem. +type APIError struct { + StatusCode int + Headers http.Header + Body []byte + Err error +} + +func (e *APIError) Error() string { + if e.Err != nil { + return fmt.Sprintf("HTTP %d: %v", e.StatusCode, e.Err) + } + return fmt.Sprintf("HTTP %d: %s", e.StatusCode, http.StatusText(e.StatusCode)) +} + +// Unwrap returns the typed API body or the response-reading error. +func (e *APIError) Unwrap() error { return e.Err } + +func readAPIError(response *http.Response, body error) error { + raw, err := io.ReadAll(response.Body) + result := &APIError{StatusCode: response.StatusCode, Headers: response.Header.Clone(), Body: raw} + if err != nil { + result.Err = fmt.Errorf("read error response: %w", err) + } else if body != nil && json.Unmarshal(raw, body) == nil { + result.Err = body + } + return result +} diff --git a/api_error_test.go b/api_error_test.go new file mode 100644 index 00000000..2238404c --- /dev/null +++ b/api_error_test.go @@ -0,0 +1,40 @@ +package sumup + +import ( + "errors" + "io" + "net/http" + "strings" + "testing" +) + +func TestAPIError(t *testing.T) { + t.Parallel() + t.Run("typed body and response context", func(t *testing.T) { + t.Parallel() + raw := `{"type":"about:blank","detail":"Invalid amount"}` + response := &http.Response{StatusCode: 400, Header: http.Header{"Retry-After": {"10"}}, Body: io.NopCloser(strings.NewReader(raw))} + err := readAPIError(response, &Problem{}) + var problem *Problem + if !errors.As(err, &problem) || problem.Detail == nil || *problem.Detail != "Invalid amount" { + t.Fatalf("typed Problem missing: %v", err) + } + var apiError *APIError + if !errors.As(err, &apiError) || apiError.StatusCode != 400 || string(apiError.Body) != raw || apiError.Headers.Get("Retry-After") != "10" { + t.Fatalf("response context missing: %v", err) + } + }) + t.Run("malformed response", func(t *testing.T) { + t.Parallel() + response := &http.Response{StatusCode: 502, Header: make(http.Header), Body: io.NopCloser(strings.NewReader("{invalid"))} + err := readAPIError(response, &Problem{}) + var apiError *APIError + if !errors.As(err, &apiError) || apiError.StatusCode != 502 || string(apiError.Body) != "{invalid" { + t.Fatalf("response context missing: %v", err) + } + var problem *Problem + if errors.As(err, &problem) { + t.Fatal("invalid body became a Problem") + } + }) +} diff --git a/checkouts.go b/checkouts.go index 04a8f02b..84df3961 100755 --- a/checkouts.go +++ b/checkouts.go @@ -723,13 +723,9 @@ func (c *CheckoutsClient) List(ctx context.Context, params CheckoutsListParams) return &v, nil case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -764,34 +760,18 @@ func (c *CheckoutsClient) Create(ctx context.Context, body CheckoutsCreateParams return &v, nil case http.StatusBadRequest: var apiErr ErrorExtended - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusForbidden: var apiErr ErrorForbidden - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusConflict: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -822,20 +802,12 @@ func (c *CheckoutsClient) CreateApplePaySession(ctx context.Context, checkoutID return &v, nil case http.StatusBadRequest: var apiErr CheckoutsCreateApplePaySession400Response - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -861,13 +833,9 @@ func (c *CheckoutsClient) ListAvailablePaymentMethods(ctx context.Context, merch return &v, nil case http.StatusBadRequest: var apiErr DetailsError - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -893,27 +861,15 @@ func (c *CheckoutsClient) Deactivate(ctx context.Context, checkoutID string) (*C return &v, nil case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusConflict: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -940,20 +896,12 @@ func (c *CheckoutsClient) Get(ctx context.Context, checkoutID string) (*Checkout return &v, nil case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -983,20 +931,12 @@ func (c *CheckoutsClient) Update(ctx context.Context, checkoutID string, body Ch return &v, nil case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -1042,33 +982,17 @@ func (c *CheckoutsClient) Process(ctx context.Context, checkoutID string, body C }, nil case http.StatusBadRequest: var apiErr CheckoutsProcess400Response - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusConflict: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } diff --git a/customers.go b/customers.go index 44c25ff6..84ac2cf5 100755 --- a/customers.go +++ b/customers.go @@ -116,34 +116,18 @@ func (c *CustomersClient) Create(ctx context.Context, body CustomersCreateParams return &v, nil case http.StatusBadRequest: var apiErr CustomersCreate400Response - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusForbidden: var apiErr ErrorForbidden - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusConflict: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -169,27 +153,15 @@ func (c *CustomersClient) ListPaymentInstruments(ctx context.Context, customerID return &v, nil case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusForbidden: var apiErr ErrorForbidden - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -216,27 +188,15 @@ func (c *CustomersClient) Get(ctx context.Context, customerID string) (*Customer return &v, nil case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusForbidden: var apiErr ErrorForbidden - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -265,27 +225,15 @@ func (c *CustomersClient) Update(ctx context.Context, customerID string, body Cu return &v, nil case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusForbidden: var apiErr ErrorForbidden - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -306,33 +254,17 @@ func (c *CustomersClient) DeactivatePaymentInstrument(ctx context.Context, custo return nil case http.StatusBadRequest: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return fmt.Errorf("read error response: %s", err.Error()) - } - - return &apiErr + return readAPIError(resp, &apiErr) case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return fmt.Errorf("read error response: %s", err.Error()) - } - - return &apiErr + return readAPIError(resp, &apiErr) case http.StatusForbidden: var apiErr ErrorForbidden - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return fmt.Errorf("read error response: %s", err.Error()) - } - - return &apiErr + return readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return fmt.Errorf("read error response: %s", err.Error()) - } - - return &apiErr + return readAPIError(resp, &apiErr) default: - return fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return readAPIError(resp, nil) } } diff --git a/internal/cmd/codegen/templates/resource.go.tmpl b/internal/cmd/codegen/templates/resource.go.tmpl index dc0f587c..13b0e4f6 100644 --- a/internal/cmd/codegen/templates/resource.go.tmpl +++ b/internal/cmd/codegen/templates/resource.go.tmpl @@ -71,17 +71,13 @@ func (c *{{$.Service}}Client) {{.FunctionName}}({{.ParamsString}}) {{with .Respo {{- end }} {{- if $resp.IsErr }} {{- with .Type }} - var apiErr {{ $resp.Type }} - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return {{with $responseType}}nil, {{end}}fmt.Errorf("read error response: %s", err.Error()) - } - - return {{with $responseType}}nil, {{end}}&apiErr + var apiErr {{ $resp.Type }} + return {{with $responseType}}nil, {{end}}readAPIError(resp, &apiErr) {{- else}} - return {{with $responseType}}nil, {{end}}errors.New("{{ $resp.ErrDescription }}") + return {{with $responseType}}nil, {{end}}readAPIError(resp, nil) {{- end}} {{- else if $resp.IsUnexpected }} - return {{with $responseType}}nil, {{end}}fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return {{with $responseType}}nil, {{end}}readAPIError(resp, nil) {{- else }} {{- with .Type }} var v {{.}} diff --git a/members.go b/members.go index 7d1aa0fd..29cf7630 100755 --- a/members.go +++ b/members.go @@ -238,13 +238,9 @@ func (c *MembersClient) List(ctx context.Context, merchantCode string, params Me return &v, nil case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -275,27 +271,15 @@ func (c *MembersClient) Create(ctx context.Context, merchantCode string, body Me return &v, nil case http.StatusBadRequest: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusTooManyRequests: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -316,20 +300,12 @@ func (c *MembersClient) Delete(ctx context.Context, merchantCode string, memberI return nil case http.StatusForbidden: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return fmt.Errorf("read error response: %s", err.Error()) - } - - return &apiErr + return readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return fmt.Errorf("read error response: %s", err.Error()) - } - - return &apiErr + return readAPIError(resp, &apiErr) default: - return fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return readAPIError(resp, nil) } } @@ -355,13 +331,9 @@ func (c *MembersClient) Get(ctx context.Context, merchantCode string, memberID s return &v, nil case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -392,33 +364,17 @@ func (c *MembersClient) Update(ctx context.Context, merchantCode string, memberI return &v, nil case http.StatusBadRequest: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusForbidden: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusConflict: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } diff --git a/memberships.go b/memberships.go index 55ccd8e0..0566ad8f 100755 --- a/memberships.go +++ b/memberships.go @@ -202,19 +202,11 @@ func (c *MembershipsClient) List(ctx context.Context, params MembershipsListPara return &v, nil case http.StatusBadRequest: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } diff --git a/merchants.go b/merchants.go index 1327b7f9..0f4c0809 100755 --- a/merchants.go +++ b/merchants.go @@ -557,13 +557,9 @@ func (c *MerchantsClient) ListPersons(ctx context.Context, merchantCode string) return &v, nil case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -590,13 +586,9 @@ func (c *MerchantsClient) Get(ctx context.Context, merchantCode string) (*Mercha return &v, nil case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -623,12 +615,8 @@ func (c *MerchantsClient) GetPerson(ctx context.Context, merchantCode string, pe return &v, nil case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } diff --git a/payouts.go b/payouts.go index 0aa5403d..8879f5fc 100755 --- a/payouts.go +++ b/payouts.go @@ -172,19 +172,11 @@ func (c *PayoutsClient) List(ctx context.Context, merchantCode string, params Pa return &v, nil case http.StatusBadRequest: var apiErr PayoutsList400Response - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } diff --git a/readers.go b/readers.go index a008f504..04bb8c9b 100755 --- a/readers.go +++ b/readers.go @@ -637,13 +637,9 @@ func (c *ReadersClient) List(ctx context.Context, merchantCode string) (*Readers return &v, nil case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -669,27 +665,15 @@ func (c *ReadersClient) Create(ctx context.Context, merchantCode string, body Re return &v, nil case http.StatusBadRequest: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusConflict: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -718,34 +702,18 @@ func (c *ReadersClient) CreateGoCheckout(ctx context.Context, merchantCode strin return &v, nil case http.StatusBadRequest: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusUnprocessableEntity: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -779,34 +747,18 @@ func (c *ReadersClient) TerminateCheckout(ctx context.Context, merchantCode stri return nil case http.StatusBadRequest: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return fmt.Errorf("read error response: %s", err.Error()) - } - - return &apiErr + return readAPIError(resp, &apiErr) case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return fmt.Errorf("read error response: %s", err.Error()) - } - - return &apiErr + return readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return fmt.Errorf("read error response: %s", err.Error()) - } - - return &apiErr + return readAPIError(resp, &apiErr) case http.StatusUnprocessableEntity: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return fmt.Errorf("read error response: %s", err.Error()) - } - - return &apiErr + return readAPIError(resp, &apiErr) default: - return fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return readAPIError(resp, nil) } } @@ -851,27 +803,15 @@ func (c *ReadersClient) GetStatus(ctx context.Context, merchantCode string, read return &v, nil case http.StatusBadRequest: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -906,34 +846,18 @@ func (c *ReadersClient) CreateCheckout(ctx context.Context, merchantCode string, return &v, nil case http.StatusBadRequest: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusUnprocessableEntity: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -954,13 +878,9 @@ func (c *ReadersClient) Delete(ctx context.Context, merchantCode string, readerI return nil case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return fmt.Errorf("read error response: %s", err.Error()) - } - - return &apiErr + return readAPIError(resp, &apiErr) default: - return fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return readAPIError(resp, nil) } } @@ -986,13 +906,9 @@ func (c *ReadersClient) Get(ctx context.Context, merchantCode string, readerID R return &v, nil case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -1021,20 +937,12 @@ func (c *ReadersClient) Update(ctx context.Context, merchantCode string, readerI return &v, nil case http.StatusForbidden: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -1060,19 +968,11 @@ func (c *ReadersClient) GetCheckout(ctx context.Context, merchantCode string, re return &v, nil case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } diff --git a/receipts.go b/receipts.go index 56e92eed..4909d9f9 100755 --- a/receipts.go +++ b/receipts.go @@ -290,26 +290,14 @@ func (c *ReceiptsClient) Get(ctx context.Context, transactionID string, params R return &v, nil case http.StatusBadRequest: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } diff --git a/roles.go b/roles.go index d892b27a..3a909aa1 100755 --- a/roles.go +++ b/roles.go @@ -103,13 +103,9 @@ func (c *RolesClient) List(ctx context.Context, merchantCode string) (*RolesList return &v, nil case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -136,20 +132,12 @@ func (c *RolesClient) Create(ctx context.Context, merchantCode string, body Role return &v, nil case http.StatusBadRequest: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -170,20 +158,12 @@ func (c *RolesClient) Delete(ctx context.Context, merchantCode string, roleID st return nil case http.StatusBadRequest: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return fmt.Errorf("read error response: %s", err.Error()) - } - - return &apiErr + return readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return fmt.Errorf("read error response: %s", err.Error()) - } - - return &apiErr + return readAPIError(resp, &apiErr) default: - return fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return readAPIError(resp, nil) } } @@ -209,13 +189,9 @@ func (c *RolesClient) Get(ctx context.Context, merchantCode string, roleID strin return &v, nil case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -244,19 +220,11 @@ func (c *RolesClient) Update(ctx context.Context, merchantCode string, roleID st return &v, nil case http.StatusBadRequest: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } diff --git a/transactions.go b/transactions.go index 5c3e19a9..106f725d 100755 --- a/transactions.go +++ b/transactions.go @@ -769,20 +769,12 @@ func (c *TransactionsClient) List(ctx context.Context, merchantCode string, para return &v, nil case http.StatusBadRequest: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -813,20 +805,12 @@ func (c *TransactionsClient) Get(ctx context.Context, merchantCode string, param return &v, nil case http.StatusUnauthorized: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Error - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } @@ -852,40 +836,20 @@ func (c *TransactionsClient) Refund(ctx context.Context, merchantCode string, tr return &v, nil case http.StatusBadRequest: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusForbidden: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusNotFound: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusConflict: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) case http.StatusUnprocessableEntity: var apiErr Problem - if err := json.NewDecoder(resp.Body).Decode(&apiErr); err != nil { - return nil, fmt.Errorf("read error response: %s", err.Error()) - } - - return nil, &apiErr + return nil, readAPIError(resp, &apiErr) default: - return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode)) + return nil, readAPIError(resp, nil) } } From f843f6033e8fecdab4f718d91d12a2182b661ccd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matou=C5=A1=20Dzivjak?= Date: Mon, 26 Oct 2026 18:47:51 +0100 Subject: [PATCH 2/2] docs: show errors from an SDK request Place error handling beside the usage guide and show the API request that produces the inspected error. --- README.md | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 7b046581..8fba4f78 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,29 @@ The easiest form of authenticating with SumUp APIs is using [API keys](https://d client := sumup.NewClient(client.WithAPIKey("sup_sk_LZFWoLyd...")) ``` +## Handling errors + +Use `errors.As` from the standard `errors` package to read typed Problem details: + +```go +_, err := client.Checkouts.Get(context.Background(), "your-checkout-id") + +var problem *sumup.Problem +if errors.As(err, &problem) { + fmt.Println(problem.Type) + if problem.Detail != nil { + fmt.Println(*problem.Detail) + } +} +var apiError *sumup.APIError +if errors.As(err, &apiError) { + fmt.Println(apiError.StatusCode, string(apiError.Body)) +} +``` + +`APIError.Headers` contains the response headers. Connection failures retain their +original Go error, so `errors.Is` and `errors.As` continue to work. + ## Examples The repository includes several examples demonstrating different use cases: @@ -101,24 +124,3 @@ For question specifically related to this Golang SDK please [Open an Issue](http `sumup-go` SDK will always support latest 3 version of golang following the Golang [Release Policy](https://go.dev/doc/devel/release). [docs-badge]: https://img.shields.io/badge/SumUp-documentation-white.svg?logo=data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgZmlsbD0ibm9uZSIgY29sb3I9IndoaXRlIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPgogICAgPHBhdGggZD0iTTIyLjI5IDBIMS43Qy43NyAwIDAgLjc3IDAgMS43MVYyMi4zYzAgLjkzLjc3IDEuNyAxLjcxIDEuN0gyMi4zYy45NCAwIDEuNzEtLjc3IDEuNzEtMS43MVYxLjdDMjQgLjc3IDIzLjIzIDAgMjIuMjkgMFptLTcuMjIgMTguMDdhNS42MiA1LjYyIDAgMCAxLTcuNjguMjQuMzYuMzYgMCAwIDEtLjAxLS40OWw3LjQ0LTcuNDRhLjM1LjM1IDAgMCAxIC40OSAwIDUuNiA1LjYgMCAwIDEtLjI0IDcuNjlabTEuNTUtMTEuOS03LjQ0IDcuNDVhLjM1LjM1IDAgMCAxLS41IDAgNS42MSA1LjYxIDAgMCAxIDcuOS03Ljk2bC4wMy4wM2MuMTMuMTMuMTQuMzUuMDEuNDlaIiBmaWxsPSJjdXJyZW50Q29sb3IiLz4KPC9zdmc+ - -## Handling errors - -Use `errors.As` to read typed Problem details: - -```go -var problem *sumup.Problem -if errors.As(err, &problem) { - fmt.Println(problem.Type) - if problem.Detail != nil { - fmt.Println(*problem.Detail) - } -} -var apiError *sumup.APIError -if errors.As(err, &apiError) { - fmt.Println(apiError.StatusCode, string(apiError.Body)) -} -``` - -`APIError.Headers` contains the response headers. Connection failures retain their -original Go error, so `errors.Is` and `errors.As` continue to work.