Skip to content

adcpError should support echoing request context #2506

Description

@bokelley

Summary

The server-side adcpError() builder cannot carry the buyer-supplied opaque request context onto the response envelope. Sellers that must satisfy request/response context correlation therefore need a wrapper that constructs the SDK error and then manually rewrites structuredContent.

Downstream example: https://github.com/scope3data/agentic-api/blob/main/apps/api/src/core-routes/storefront-handlers/_shared.ts

That one wrapper currently has 108 call sites across storefront handlers. The duplication is not error construction itself—the SDK already owns that well—but the missing context-preserving variant means every error path must remember to use a downstream replacement instead of adcpError() directly.

Current workaround

function errorWithContext(code, options, requestContext) {
  const envelope = adcpError(code, options)
  if (requestContext === undefined) return envelope
  return {
    ...envelope,
    structuredContent: {
      ...envelope.structuredContent,
      context: requestContext,
    },
  }
}

This also risks the text fallback and structured payload drifting because the caller is modifying only one of the builder's transport layers after construction.

Expected

Give adcpError() a supported way to include opaque response context while it constructs all transport layers. Exact API shape is a maintainer choice, for example an optional envelope argument:

adcpError(code, options, { context: request.context })

or a dedicated adcpErrorWithContext() helper.

The context belongs beside adcp_error in the response payload, not inside the error details or per-code allowlist.

Acceptance criteria

  • Callers can supply an opaque context value without manually spreading the returned envelope.
  • When context is defined, structuredContent contains both adcp_error and the unchanged context.
  • The text fallback represents the same payload so L2 and L3 consumers do not observe different context behavior.
  • Undefined context remains omitted and preserves the current output exactly.
  • Error-field allowlisting/sanitization continues to apply only to adcp_error; context is not accidentally folded into diagnostic details.
  • Server examples and tests show success and error responses echoing the same request context.

Why this belongs in the SDK

The builder already owns the three-layer error envelope contract. Adding context after construction makes those layers inconsistent and makes the safe path easy to bypass. Encoding the behavior once in the SDK removes a high-fan-out downstream wrapper and keeps correlation semantics uniform across sellers.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions