LendWorksLendWorksDocs

Error Handling

Typed error classes, status codes, request IDs, and best practices for handling API errors.

Error Classes

The SDK provides typed error classes for every failure scenario. Every error instance includes a request ID for support correlation.

TypeScript
import {
  LendIQ,
  LendIQError,
  AuthenticationError,
  NotFoundError,
  ValidationError,
  RateLimitError,
} from "@lendworks/lendiq";

const client = new LendIQ({ apiKey: "liq_live_..." });

try {
  await client.deals.get(99999);
} catch (err) {
  if (err instanceof NotFoundError) {
    console.log("Deal not found");
    console.log(err.statusCode);  // 404
    console.log(err.requestId);   // "req_abc123"
    console.log(err.body);        // { error: "Deal not found" }
  } else if (err instanceof AuthenticationError) {
    console.log("Invalid credentials:", err.message);
  } else if (err instanceof ValidationError) {
    console.log("Validation failed:", err.body);
  } else if (err instanceof RateLimitError) {
    console.log(`Rate limited. Retry after ${err.retryAfter} seconds`);
  } else if (err instanceof LendIQError) {
    console.log(err.message, err.statusCode);
  }
}
Python
from lendiq import (
    LendIQ,
    LendIQError,
    AuthenticationError,
    NotFoundError,
    ValidationError,
    RateLimitError,
)

client = LendIQ(api_key="liq_live_...")

try:
    client.deals.get(99999)
except NotFoundError as e:
    print("Deal not found")
    print(e.status_code)   # 404
    print(e.request_id)    # "req_abc123"
except AuthenticationError as e:
    print("Invalid credentials:", e.message)
except ValidationError as e:
    print("Validation failed:", e.body)
except RateLimitError as e:
    print(f"Rate limited. Retry after {e.retry_after} seconds")
except LendIQError as e:
    print(e.message, e.status_code)

Error Types

ClassStatusWhen
AuthenticationError401Invalid or expired API key
ValidationError422Request body fails validation
NotFoundError404Resource does not exist
RateLimitError429Rate limit exceeded (check retryAfter)
LendIQErrorAnyBase class for all other API errors

Request ID Tracking

Every API response includes a request ID. You can access the last request ID on the client instance for logging and support correlation.

TypeScript
const deals = await client.deals.list();
console.log(client.lastRequestId);  // "req_xyz789"
Python
deals = client.deals.list()
print(client.last_request_id)  # "req_xyz789"