Skip to main content
All API errors are thrown as TriqaiError instances. Common HTTP statuses are mapped to typed subclasses so you can handle specific failure modes precisely.

Basic Usage

import Triqai, {
  TriqaiError,
  AuthenticationError,
  ValidationError,
  RateLimitError,
  InsufficientCreditsError,
  NotFoundError,
} from "triqai";

try {
  await triqai.transactions.enrich({
    title: "",
    country: "US",
    type: "expense",
  });
} catch (err) {
  if (err instanceof ValidationError) {
    console.log("Field errors:", err.fieldErrors);
    // { title: ["Title cannot be empty"] }
  } else if (err instanceof RateLimitError) {
    console.log("Retry after:", err.rateLimitInfo.retryAfter, "seconds");
  } else if (err instanceof AuthenticationError) {
    console.log("Check your API key");
  } else if (err instanceof InsufficientCreditsError) {
    console.log("Top up credits at https://triqai.com/dashboard");
  } else if (err instanceof NotFoundError) {
    console.log("Resource not found");
  } else if (err instanceof TriqaiError) {
    console.log(`API error ${err.statusCode}: ${err.message} [${err.code}]`);
    console.log("Request ID:", err.requestId);
  }
}

Error Classes

Every error extends TriqaiError and includes statusCode, message, code, and requestId properties.
ClassStatusWhen
AuthenticationError401Invalid or missing API key
InsufficientCreditsError402No credits remaining
AuthorizationError403Key valid but not authorized for this resource
NotFoundError404Resource does not exist
DuplicateRequestError409Idempotency key reused with different parameters
ValidationError422Request body validation failed
RateLimitError429Rate limit exceeded
ClientDisconnectedError499Client disconnected before response completed
InternalServerError500Server error (retried automatically)
ServiceUnavailableError503Service temporarily down (retried automatically)
GatewayTimeoutError504Upstream timeout
ConnectionErrorNetwork or DNS failure
TimeoutErrorRequest exceeded configured timeout

Validation Errors

ValidationError includes a fieldErrors property with per-field error messages:
try {
  await triqai.transactions.enrich({
    title: "",
    country: "INVALID",
    type: "expense",
  });
} catch (err) {
  if (err instanceof ValidationError) {
    console.log(err.fieldErrors);
    // { title: ["Title cannot be empty"], country: ["Invalid country code"] }
  }
}

Rate Limit Errors

RateLimitError includes a rateLimitInfo property with retry timing:
try {
  await triqai.transactions.enrich({ ... });
} catch (err) {
  if (err instanceof RateLimitError) {
    console.log(err.rateLimitInfo.retryAfter);    // seconds until reset
    console.log(err.rateLimitInfo.limit);          // max tokens
    console.log(err.rateLimitInfo.remaining);      // tokens left
  }
}