From 318c9838cab2e27f962bce49c4dbf47043edae64 Mon Sep 17 00:00:00 2001 From: Drew Botwinick Date: Fri, 25 Sep 2026 12:07:24 -0500 Subject: [PATCH] Let typed Go SDK errors unwrap to APIError The README and the APIError doc comment say errors.As(err, &apiErr) matches every typed error, but the typed errors only embed *APIError; errors.As does not look at embedded fields, so the match always failed. Add Unwrap to each typed error so the documented pattern works (including through fmt.Errorf wrapping), and list the 409/412/428 error types in the README. --- memorylayer-sdk-go/README.md | 4 +-- memorylayer-sdk-go/client_test.go | 21 +++++++++++++++ memorylayer-sdk-go/errors.go | 43 +++++++++++++++++++++++++++++-- 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/memorylayer-sdk-go/README.md b/memorylayer-sdk-go/README.md index e59daec..5029cfa 100644 --- a/memorylayer-sdk-go/README.md +++ b/memorylayer-sdk-go/README.md @@ -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{}) @@ -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 diff --git a/memorylayer-sdk-go/client_test.go b/memorylayer-sdk-go/client_test.go index f0cd8e6..5076d0b 100644 --- a/memorylayer-sdk-go/client_test.go +++ b/memorylayer-sdk-go/client_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "io" "net/http" "net/http/httptest" @@ -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 diff --git a/memorylayer-sdk-go/errors.go b/memorylayer-sdk-go/errors.go index 2781fdf..878f8f3 100644 --- a/memorylayer-sdk-go/errors.go +++ b/memorylayer-sdk-go/errors.go @@ -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 @@ -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} }