Skip to content

Affonso Errors

github-actions[bot] edited this page Dec 2, 2026 · 1 revision

Affonso Errors

Affonso throws one error class, AffonsoError, for configuration, request validation, vendor responses, transport failures and webhook verification — see errors/Base.ts and errors/AffonsoErrorCodes.ts.

import { AffonsoError } from '@tundraconnect/affonso/errors';

declare function callTheClient(): Promise<unknown>;

try {
  await callTheClient();
} catch (err) {
  if (err instanceof AffonsoError && err.transient) {
    // TIMEOUT, NETWORK_ERROR, SERVICE_UNAVAILABLE or RATE_LIMITED: retry later.
  } else {
    throw err;
  }
}

Branch on the readonly error.code, never on .message. error.transient is true exactly for the codes in AFFONSO_TRANSIENT_CODES.

Configuration codes

Code Meaning
CONFIG_INVALID_API_KEY auth is missing, isn't BEARER, or its token is blank.

Request / response codes

Code Meaning
REQUEST_VALIDATION_ERROR A request failed local validation; nothing was sent. reason says which rule.
RESPONSE_ERROR A success body did not match the expected schema, or a verified webhook body is not an Affonso event.
UNKNOWN_ERROR An unmapped status, or an unregistered code was passed to the constructor.

Vendor codes

Affonso answers a failure with an HTTP status and { success: false, error: { code, message } }. Classification follows the status; Affonso's own code is kept as vendorCode.

Code HTTP Typical vendor codes Transient
INVALID_REQUEST 400, 422 VALIDATION_ERROR no
AUTH_FAILED 401 MISSING_API_KEY, INVALID_API_KEY_FORMAT, INVALID_API_KEY, API_KEY_DISABLED, API_KEY_EXPIRED no
FORBIDDEN 403 INSUFFICIENT_PERMISSIONS no
NOT_FOUND 404 NOT_FOUND, CLICK_NOT_FOUND no
CONFLICT 409 DUPLICATE_ERROR (taken tracking id, referral email or coupon code) no
RATE_LIMITED 429 RATE_LIMIT_EXCEEDED yes
SERVICE_UNAVAILABLE 5xx INTERNAL_ERROR; a 502 is a payment-provider failure on coupon create/delete yes

A 2xx whose body says success: false is treated as a failure too.

Transport codes

Code Meaning Transient
TIMEOUT No answer within timeout seconds (timeoutSeconds context). yes
NETWORK_ERROR The request failed before any response (DNS, TLS, connection). yes

Webhook codes

Code Meaning
WEBHOOK_INVALID_HEADERS X-Affonso-Timestamp or X-Affonso-Signature is missing.
WEBHOOK_TIMESTAMP_INVALID The timestamp isn't whole Unix seconds, or is outside the tolerance (300 s).
WEBHOOK_SIGNATURE_INVALID The signature does not match — treat the request as forged.
WEBHOOK_INVALID_SECRET The signing secret is empty.

The most common cause of WEBHOOK_SIGNATURE_INVALID on a genuine delivery is a body that was parsed and re-serialized before verification: pass await req.text(). The second is hex-decoding the secret — use it as-is.

Rate limits

Affonso limits per minute and per second by plan (Launch 300/min and 10/s up to Enterprise 3,000/min and 100/s) and sends X-RateLimit-Limit, X-RateLimit-Remaining and X-RateLimit-Reset on every response, plus Retry-After on a 429. trackClick uses its own burst limit.

Diagnostic metadata

Read metadata with the public getContextValue(key).

Key Present on Description
vendor all Always 'Affonso'.
status vendor failures HTTP status code.
detail vendor failures Vendor message plus its code.
vendorCode documented-envelope failures Affonso's own error code.
issues some VALIDATION_ERRORs Per-field messages.
body vendor failures The parsed response body.
retryAfterSeconds RATE_LIMITED How long Affonso asked you to wait.
retried RATE_LIMITED with maxRetryWait Whether RESTler already waited once.
reason validation / webhook codes Which rule failed.
responseError validation codes The Guardian failure, as JSON.
timeoutSeconds TIMEOUT The deadline that was missed, in seconds.
originalCode UNKNOWN_ERROR fallback The unrecognized code passed in.

Neither the API key nor the webhook signing secret ever appears in an error's message or context.

Backing off after a 429

retryAfterSeconds is read by RESTler from Retry-After (or the other standard rate-limit headers); when a 429 carries no header hint, the retryAfter field of Affonso's error body is used instead. It is undefined when neither is present — never a guess.

Nothing is retried by default. Opt in with two client options (seconds, each 0–120):

  • maxRetryWait — when a 429 carries a hint no longer than this, RESTler waits that long and retries once; otherwise it throws RATE_LIMITED immediately, with retried: false. A retry that is throttled again throws RATE_LIMITED with retried: true.
  • defaultRetryWait — the wait used when a 429 carries no header hint. Only consulted when maxRetryWait is set, and still capped by it.

Writes are not all idempotent: createConversion is (by external_event_id), recordSignup is (by click_id), and createCommission dedupes on payment_intent_id, but createAffiliate, createReferral and createBonus are not — read before resending one that timed out.


← Back to Affonso

Clone this wiki locally