|
| 1 | +"""Typed exception subclasses for the AdCP error code vocabulary. |
| 2 | +
|
| 3 | +:class:`adcp.decisioning.AdcpError` is the wire-shaped structured error |
| 4 | +adopters raise from inside Protocol method bodies — the framework |
| 5 | +catches at the dispatch seam and projects to the ``adcp_error`` |
| 6 | +envelope. The base class accepts any spec code as a string, but |
| 7 | +adopters who want catch-by-type ergonomics (``except PermissionDeniedError:``) |
| 8 | +or who want the correct ``recovery`` semantic auto-applied repeatedly |
| 9 | +re-derive the boilerplate. These subclasses bind each spec code to its |
| 10 | +canonical recovery classification (per |
| 11 | +``schemas/cache/enums/error-code.json#enumMetadata``) and offer a |
| 12 | +sensible default message adopters can override. |
| 13 | +
|
| 14 | +Recovery values are normative — they MUST match the ``enumMetadata`` |
| 15 | +block in the error-code schema. Adopters MUST NOT override |
| 16 | +``recovery`` on these subclasses; if a different recovery is needed |
| 17 | +for a vendor variant, raise the base :class:`AdcpError` directly. |
| 18 | +""" |
| 19 | + |
| 20 | +from __future__ import annotations |
| 21 | + |
| 22 | +from typing import Any, Literal |
| 23 | + |
| 24 | +from adcp.decisioning.types import AdcpError |
| 25 | + |
| 26 | +__all__ = [ |
| 27 | + "AccountNotFoundError", |
| 28 | + "AuthRequiredError", |
| 29 | + "BillingNotPermittedForAgentError", |
| 30 | + "MediaBuyNotFoundError", |
| 31 | + "PermissionDeniedError", |
| 32 | + "RateLimitedError", |
| 33 | + "ServiceUnavailableError", |
| 34 | + "UnsupportedFeatureError", |
| 35 | + "ValidationError", |
| 36 | +] |
| 37 | + |
| 38 | + |
| 39 | +class PermissionDeniedError(AdcpError): |
| 40 | + """Spec ``PERMISSION_DENIED`` (``recovery='correctable'``). |
| 41 | +
|
| 42 | + Raised when the authenticated caller is not authorized for the |
| 43 | + requested action under the seller's policies, or a required signed |
| 44 | + credential is missing/invalid. |
| 45 | +
|
| 46 | + :param scope: When the gate is a per-agent provisioning constraint, |
| 47 | + set to ``'agent'`` (with ``status``); when billing-relationship, |
| 48 | + set to ``'billing'``. Sellers MUST emit ``scope='agent'`` only |
| 49 | + when buyer-agent identity has been established (signed-request |
| 50 | + derivation or credential-to-agent mapping); otherwise omit. |
| 51 | + :param status: When ``scope='agent'``, the per-agent state |
| 52 | + (``'sandbox_only'``, etc.) — see |
| 53 | + ``error-details/agent-permission-denied.json``. |
| 54 | + :param message: Optional human-readable override of the default. |
| 55 | + :param details: Additional fields merged into ``error.details``. |
| 56 | + """ |
| 57 | + |
| 58 | + def __init__( |
| 59 | + self, |
| 60 | + *, |
| 61 | + scope: Literal["agent", "billing"] | None = None, |
| 62 | + status: str | None = None, |
| 63 | + message: str | None = None, |
| 64 | + field: str | None = None, |
| 65 | + suggestion: str | None = None, |
| 66 | + **details: Any, |
| 67 | + ) -> None: |
| 68 | + merged_details: dict[str, Any] = dict(details) |
| 69 | + if scope is not None: |
| 70 | + merged_details["scope"] = scope |
| 71 | + if status is not None: |
| 72 | + merged_details["status"] = status |
| 73 | + |
| 74 | + super().__init__( |
| 75 | + "PERMISSION_DENIED", |
| 76 | + message=message or "Caller is not authorized for the requested action.", |
| 77 | + recovery="correctable", |
| 78 | + field=field, |
| 79 | + suggestion=suggestion, |
| 80 | + details=merged_details or None, |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +class AuthRequiredError(AdcpError): |
| 85 | + """Spec ``AUTH_REQUIRED`` (``recovery='correctable'``). |
| 86 | +
|
| 87 | + Raised when no credentials were presented. (The schema classifies |
| 88 | + this as ``correctable`` — the buyer fixes by attaching credentials |
| 89 | + and retrying.) |
| 90 | + """ |
| 91 | + |
| 92 | + def __init__( |
| 93 | + self, |
| 94 | + *, |
| 95 | + message: str | None = None, |
| 96 | + field: str | None = None, |
| 97 | + suggestion: str | None = None, |
| 98 | + **details: Any, |
| 99 | + ) -> None: |
| 100 | + super().__init__( |
| 101 | + "AUTH_REQUIRED", |
| 102 | + message=message or "Authentication is required to access this resource.", |
| 103 | + recovery="correctable", |
| 104 | + field=field, |
| 105 | + suggestion=suggestion, |
| 106 | + details=dict(details) or None, |
| 107 | + ) |
| 108 | + |
| 109 | + |
| 110 | +class ServiceUnavailableError(AdcpError): |
| 111 | + """Spec ``SERVICE_UNAVAILABLE`` (``recovery='transient'``). |
| 112 | +
|
| 113 | + Raised when the seller service is temporarily unavailable. The |
| 114 | + buyer retries with exponential backoff; ``retry_after`` MAY be set |
| 115 | + to hint a minimum delay. |
| 116 | + """ |
| 117 | + |
| 118 | + def __init__( |
| 119 | + self, |
| 120 | + *, |
| 121 | + message: str | None = None, |
| 122 | + retry_after: int | None = None, |
| 123 | + **details: Any, |
| 124 | + ) -> None: |
| 125 | + super().__init__( |
| 126 | + "SERVICE_UNAVAILABLE", |
| 127 | + message=message or "Service is temporarily unavailable.", |
| 128 | + recovery="transient", |
| 129 | + retry_after=retry_after, |
| 130 | + details=dict(details) or None, |
| 131 | + ) |
| 132 | + |
| 133 | + |
| 134 | +class RateLimitedError(AdcpError): |
| 135 | + """Spec ``RATE_LIMITED`` (``recovery='transient'``). |
| 136 | +
|
| 137 | + Raised when the request rate exceeds the seller's threshold. The |
| 138 | + buyer retries after the ``retry_after`` interval. |
| 139 | + """ |
| 140 | + |
| 141 | + def __init__( |
| 142 | + self, |
| 143 | + *, |
| 144 | + message: str | None = None, |
| 145 | + retry_after: int | None = None, |
| 146 | + **details: Any, |
| 147 | + ) -> None: |
| 148 | + super().__init__( |
| 149 | + "RATE_LIMITED", |
| 150 | + message=message or "Request rate exceeded.", |
| 151 | + recovery="transient", |
| 152 | + retry_after=retry_after, |
| 153 | + details=dict(details) or None, |
| 154 | + ) |
| 155 | + |
| 156 | + |
| 157 | +class MediaBuyNotFoundError(AdcpError): |
| 158 | + """Spec ``MEDIA_BUY_NOT_FOUND`` (``recovery='correctable'``). |
| 159 | +
|
| 160 | + Raised when the referenced media buy does not exist or is not |
| 161 | + accessible to the requesting agent. Sellers MUST return this code |
| 162 | + uniformly for any ``media_buy_id`` not owned by the calling |
| 163 | + account — never distinguish "exists in another tenant" from |
| 164 | + "does not exist". |
| 165 | + """ |
| 166 | + |
| 167 | + def __init__( |
| 168 | + self, |
| 169 | + *, |
| 170 | + media_buy_id: str | None = None, |
| 171 | + message: str | None = None, |
| 172 | + field: str | None = None, |
| 173 | + **details: Any, |
| 174 | + ) -> None: |
| 175 | + merged: dict[str, Any] = dict(details) |
| 176 | + if media_buy_id is not None: |
| 177 | + merged["media_buy_id"] = media_buy_id |
| 178 | + super().__init__( |
| 179 | + "MEDIA_BUY_NOT_FOUND", |
| 180 | + message=message or "Media buy not found.", |
| 181 | + recovery="correctable", |
| 182 | + field=field, |
| 183 | + details=merged or None, |
| 184 | + ) |
| 185 | + |
| 186 | + |
| 187 | +class AccountNotFoundError(AdcpError): |
| 188 | + """Spec ``ACCOUNT_NOT_FOUND`` (``recovery='terminal'``). |
| 189 | +
|
| 190 | + Raised when the account reference cannot be resolved. The buyer |
| 191 | + verifies the account via ``list_accounts`` or contacts the seller. |
| 192 | + """ |
| 193 | + |
| 194 | + def __init__( |
| 195 | + self, |
| 196 | + *, |
| 197 | + message: str | None = None, |
| 198 | + field: str | None = None, |
| 199 | + **details: Any, |
| 200 | + ) -> None: |
| 201 | + super().__init__( |
| 202 | + "ACCOUNT_NOT_FOUND", |
| 203 | + message=message or "Account not found.", |
| 204 | + recovery="terminal", |
| 205 | + field=field, |
| 206 | + details=dict(details) or None, |
| 207 | + ) |
| 208 | + |
| 209 | + |
| 210 | +class BillingNotPermittedForAgentError(AdcpError): |
| 211 | + """Spec ``BILLING_NOT_PERMITTED_FOR_AGENT`` (``recovery='correctable'``). |
| 212 | +
|
| 213 | + Raised when the seller's ``supported_billing`` capability accepts |
| 214 | + the requested billing model, but the calling buyer agent's |
| 215 | + commercial relationship with the seller does not. The recovery |
| 216 | + shape is deliberately minimal — ``error.details`` MUST conform to |
| 217 | + ``error-details/billing-not-permitted-for-agent.json`` |
| 218 | + (``rejected_billing`` plus an optional single ``suggested_billing`` |
| 219 | + retry value, typically ``'operator'``). |
| 220 | +
|
| 221 | + :param rejected_billing: The billing values the agent attempted that |
| 222 | + are not permitted (echoed in ``error.details.rejected_billing``). |
| 223 | + :param suggested_billing: Optional single retry value the seller |
| 224 | + recommends (echoed in ``error.details.suggested_billing``). |
| 225 | + """ |
| 226 | + |
| 227 | + def __init__( |
| 228 | + self, |
| 229 | + *, |
| 230 | + rejected_billing: list[str], |
| 231 | + suggested_billing: list[str] | None = None, |
| 232 | + message: str | None = None, |
| 233 | + ) -> None: |
| 234 | + merged: dict[str, Any] = {"rejected_billing": list(rejected_billing)} |
| 235 | + if suggested_billing is not None: |
| 236 | + merged["suggested_billing"] = list(suggested_billing) |
| 237 | + super().__init__( |
| 238 | + "BILLING_NOT_PERMITTED_FOR_AGENT", |
| 239 | + message=( |
| 240 | + message or "Calling agent is not permitted to use the requested billing value." |
| 241 | + ), |
| 242 | + recovery="correctable", |
| 243 | + details=merged, |
| 244 | + ) |
| 245 | + |
| 246 | + |
| 247 | +class ValidationError(AdcpError): |
| 248 | + """Spec ``VALIDATION_ERROR`` (``recovery='correctable'``). |
| 249 | +
|
| 250 | + Raised when a request contains invalid field values or violates |
| 251 | + business rules beyond schema validation. ``field`` SHOULD identify |
| 252 | + the offending path so buyers can highlight the input. |
| 253 | + """ |
| 254 | + |
| 255 | + def __init__( |
| 256 | + self, |
| 257 | + *, |
| 258 | + message: str | None = None, |
| 259 | + field: str | None = None, |
| 260 | + suggestion: str | None = None, |
| 261 | + **details: Any, |
| 262 | + ) -> None: |
| 263 | + super().__init__( |
| 264 | + "VALIDATION_ERROR", |
| 265 | + message=message or "Request failed validation.", |
| 266 | + recovery="correctable", |
| 267 | + field=field, |
| 268 | + suggestion=suggestion, |
| 269 | + details=dict(details) or None, |
| 270 | + ) |
| 271 | + |
| 272 | + |
| 273 | +class UnsupportedFeatureError(AdcpError): |
| 274 | + """Spec ``UNSUPPORTED_FEATURE`` (``recovery='correctable'``). |
| 275 | +
|
| 276 | + Raised when a requested feature or field is not supported by this |
| 277 | + seller. The buyer checks ``get_adcp_capabilities`` and removes the |
| 278 | + unsupported field. |
| 279 | + """ |
| 280 | + |
| 281 | + def __init__( |
| 282 | + self, |
| 283 | + *, |
| 284 | + message: str | None = None, |
| 285 | + field: str | None = None, |
| 286 | + **details: Any, |
| 287 | + ) -> None: |
| 288 | + super().__init__( |
| 289 | + "UNSUPPORTED_FEATURE", |
| 290 | + message=message or "Requested feature is not supported by this seller.", |
| 291 | + recovery="correctable", |
| 292 | + field=field, |
| 293 | + details=dict(details) or None, |
| 294 | + ) |
0 commit comments