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
4 changes: 2 additions & 2 deletions memorylayer-sdk-go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ client.Rpg.GetSubgraph(ctx, memorylayer.RpgSubgraphOptions{Path: "src/main.go"})

## Errors

Failures return typed errors that embed `*APIError` (carrying `Message` and `StatusCode`). Match them with `errors.As`, or use the helpers:
Failures return typed errors that embed and unwrap to `*APIError` (carrying `Message` and `StatusCode`). Match a specific type or `*APIError` itself with `errors.As`, or use the helpers:

```go
results, err := client.Recall(ctx, "q", memorylayer.RecallOptions{})
Expand All @@ -113,7 +113,7 @@ if err != nil {
}
```

Error types: `AuthenticationError` (401), `AuthorizationError` (403), `NotFoundError` (404), `ValidationError` (422), `RateLimitError` (429), `ServerError` (5xx), `EnterpriseRequiredError` (501 on an Enterprise feature). Idempotent requests (GET/PUT/DELETE/PATCH) are retried with backoff on transient failures, honoring `Retry-After`; POST is never auto-retried.
Error types: `AuthenticationError` (401), `AuthorizationError` (403), `NotFoundError` (404), `ConflictError` (409), `PreconditionFailedError` (412), `ValidationError` (422), `PreconditionRequiredError` (428), `RateLimitError` (429), `ServerError` (5xx), `EnterpriseRequiredError` (501 on an Enterprise feature). Idempotent requests (GET/PUT/DELETE/PATCH) are retried with backoff on transient failures, honoring `Retry-After`; POST is never auto-retried.

## On-behalf-of (OBO) authority

Expand Down
21 changes: 21 additions & 0 deletions memorylayer-sdk-go/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -153,6 +154,26 @@ func TestErrorMapping(t *testing.T) {
}
}

func TestTypedErrorsUnwrapToAPIError(t *testing.T) {
for _, status := range []int{401, 403, 404, 409, 412, 422, 428, 429, 500, 501, 503} {
for _, feature := range []string{"", "Docs"} {
err := mapError(&Response{StatusCode: status, Body: []byte(`{"detail":"boom"}`)}, feature)
wrapped := fmt.Errorf("call failed: %w", err)
var apiErr *APIError
if !errors.As(wrapped, &apiErr) {
t.Errorf("status %d feature %q: errors.As(*APIError) failed for %T", status, feature, err)
continue
}
if apiErr.StatusCode != status {
t.Errorf("status %d feature %q: StatusCode = %d", status, feature, apiErr.StatusCode)
}
}
}
if (&NotFoundError{}).Unwrap() != nil {
t.Error("Unwrap of a typed error without an embedded *APIError should be nil")
}
}

func TestForgetSendsHardParam(t *testing.T) {
var hard string
var method string
Expand Down
43 changes: 41 additions & 2 deletions memorylayer-sdk-go/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
)

// APIError is the base error type for all MemoryLayer API failures. Every typed
// error below embeds an *APIError, so errors.As against *APIError matches any of
// them and exposes the HTTP StatusCode.
// error below embeds an *APIError and unwraps to it, so errors.As against
// *APIError matches any of them and exposes the HTTP StatusCode.
type APIError struct {
Message string
StatusCode int
Expand Down Expand Up @@ -56,6 +56,45 @@ type RateLimitError struct{ *APIError }
// ServerError is returned for HTTP 5xx responses.
type ServerError struct{ *APIError }

// unwrapAPIError returns the embedded *APIError as an error, or nil when it is
// absent (avoids returning a non-nil interface holding a nil pointer).
func unwrapAPIError(e *APIError) error {
if e == nil {
return nil
}
return e
}

// Unwrap returns the embedded *APIError so errors.As(err, &apiErr) matches.
func (e *AuthenticationError) Unwrap() error { return unwrapAPIError(e.APIError) }

// Unwrap returns the embedded *APIError so errors.As(err, &apiErr) matches.
func (e *AuthorizationError) Unwrap() error { return unwrapAPIError(e.APIError) }

// Unwrap returns the embedded *APIError so errors.As(err, &apiErr) matches.
func (e *NotFoundError) Unwrap() error { return unwrapAPIError(e.APIError) }

// Unwrap returns the embedded *APIError so errors.As(err, &apiErr) matches.
func (e *ConflictError) Unwrap() error { return unwrapAPIError(e.APIError) }

// Unwrap returns the embedded *APIError so errors.As(err, &apiErr) matches.
func (e *PreconditionFailedError) Unwrap() error { return unwrapAPIError(e.APIError) }

// Unwrap returns the embedded *APIError so errors.As(err, &apiErr) matches.
func (e *PreconditionRequiredError) Unwrap() error { return unwrapAPIError(e.APIError) }

// Unwrap returns the embedded *APIError so errors.As(err, &apiErr) matches.
func (e *EnterpriseRequiredError) Unwrap() error { return unwrapAPIError(e.APIError) }

// Unwrap returns the embedded *APIError so errors.As(err, &apiErr) matches.
func (e *ValidationError) Unwrap() error { return unwrapAPIError(e.APIError) }

// Unwrap returns the embedded *APIError so errors.As(err, &apiErr) matches.
func (e *RateLimitError) Unwrap() error { return unwrapAPIError(e.APIError) }

// Unwrap returns the embedded *APIError so errors.As(err, &apiErr) matches.
func (e *ServerError) Unwrap() error { return unwrapAPIError(e.APIError) }

func newError(message string, statusCode int) *APIError {
return &APIError{Message: message, StatusCode: statusCode}
}
Expand Down
Loading