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
99 changes: 67 additions & 32 deletions DOCUMENTATION.md

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
38 changes: 38 additions & 0 deletions api_error.go
Original file line number Diff line number Diff line change
@@ -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
}
40 changes: 40 additions & 0 deletions api_error_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
})
}
130 changes: 27 additions & 103 deletions checkouts.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading