ruLog in to Senler

SDK Errors

Handle errors around SDK client calls.

Errors

The SDK exports three main error classes:

  • ResponseError means the API returned an unsuccessful HTTP status; the original Response is available as error.response;
  • FetchError means the request did not receive a response, for example because of a network error;
  • RequiredError means a required method parameter was omitted.

Handle the Error

import {
  FetchError,
  RequiredError,
  ResponseError,
} from "@senlerio/api";

try {
  await client.projects.getMe();
} catch (error) {
  if (error instanceof ResponseError) {
    const details = await error.response.clone().json().catch(() => null);
    console.error(error.response.status, details);
  } else if (error instanceof RequiredError) {
    console.error("Missing parameter:", error.field);
  } else if (error instanceof FetchError) {
    console.error("Network error:", error.cause);
  } else {
    throw error;
  }
}

For a 401 response, check whether the token is current. For 403, check token permissions and user permissions. For an entity error, verify projectId, the resource ID, and the token scope.

A 429 response with error.name = api.daily_quota_exceeded means the project's daily API quota is exhausted. Do not retry immediately: wait for the quota reset indicated by Retry-After, or increase the tariff limit. In the SDK, read the header with error.response.headers.get("Retry-After").

A 503 response with error.name = api.quota_unavailable means quota accounting is temporarily unavailable. The action was not executed, so the request can be retried after a delay. This applies to that specific error, not every 503 response.

A network failure does not by itself prove that the server did not execute the action. Before creating a resource, sending a message or starting a process again, check the result of the previous request to avoid doing it twice.

For a server-side OAuth integration, you can enable automatic token refresh.

Retrying a Message

If the response to a send request is lost, repeat the same request with the key you saved beforehand. How you pass it depends on the endpoint:

  • POST /api/dialogs/DIALOG_ID/send: the idempotency_key field in the JSON body.
  • POST /api/dialogs/direct-message: the Idempotency-Key header for channels other than Email. Direct email sending does not support this header: a request with it returns 400.

For example, the body for sending to a known dialog:

{
  "content": "Your order is ready",
  "content_format": "plain_text",
  "idempotency_key": "order-ready:123"
}

The key must be a non-empty string of up to 200 characters. Generate and save it before the first attempt. Reuse the same key and data for retries of one message; use a new key for a different message.

Deduplication applies within one dialog: a retry returns the previously created message instead of sending it again. direct-message selects the lead's latest private dialog in the channel, so the same key in a different dialog does not prevent a new send. To target one specific dialog, use /dialogs/DIALOG_ID/send.

The key does not repair a delivery failure for an existing message or confirm that the platform delivered it. Check the final status. Text format and send example.