Skip to main content
SDK दो error classes प्रदान करता है: HTTP और validation failures के लिए YoukaRequestError, और async tasks के लिए YoukaTaskError जो non-successful state में समाप्त हुई हों। दोनों Error को extend करते हैं और structured fields रखते हैं ताकि आप code, status, और retryability के आधार पर branch कर सकें।

YoukaRequestError

HTTP errors, request validation failures, और malformed responses के लिए throw किया जाता है।
import { YoukaRequestError } from "@youka/sdk";

try {
  await client.projects.create(body);
} catch (error) {
  if (error instanceof YoukaRequestError) {
    console.error({
      code: error.code,
      message: error.message,
      status: error.status,
      retryable: error.retryable,
      details: error.details,
    });
  } else {
    throw error;
  }
}

Fields

code
string
Machine-readable error code, उदाहरण के लिए INVALID_REQUEST, UNAUTHORIZED, UPLOAD_FAILED.
message
string
Human-readable विवरण।
status
number
HTTP status code, यदि उपलब्ध हो।
retryable
boolean
true यदि SDK इस error को retry करने लायक मानता है (rate limits, transient server errors, idempotent replay in progress).
details
unknown
Server द्वारा प्रदान किए गए details, आमतौर पर validation errors के लिए Zod issue list।

Common codes

CodeकारणRetryable?
INVALID_REQUESTभेजने से पहले request body schema validation में fail हुई।No
UNAUTHORIZEDAPI key missing या invalid।No
NOT_FOUNDResource मौजूद नहीं है या आपके पास access नहीं है।No
CONFLICTVersion conflict (HTTP 409)।Yes
TOO_MANY_REQUESTSRate limit hit (HTTP 429)।Yes
INTERNAL_SERVER_ERRORTransient server failure (HTTP 500)।Yes
IDEMPOTENT_REPLAY_IN_PROGRESSOriginal request उसी idempotency key के तहत अभी भी चल रही है (HTTP 202)।Yes
INVALID_RESPONSEServer ने ऐसा body लौटाया जो expected schema से match नहीं हुआ।No
UPLOAD_FAILEDSigned URL upload ने non-2xx लौटाया।Depends on status

YoukaTaskError

client.tasks.wait(...), client.projects.wait(...), और client.exports.wait(...) से throw किया जाता है जब underlying task या export failed, cancelled, या timed-out में समाप्त होता है।
import { YoukaTaskError } from "@youka/sdk";

try {
  await client.projects.wait(created);
} catch (error) {
  if (error instanceof YoukaTaskError) {
    console.error({
      code: error.code,
      message: error.message,
      status: error.status,
      task: error.task,
    });
  } else {
    throw error;
  }
}

Fields

code
'TASK_FAILED' | 'TASK_CANCELLED' | 'TASK_TIMED_OUT'
Task की terminal status से सीधे map होता है।
message
string
या तो server द्वारा प्रदान किया गया task error message, या एक generated fallback।
status
TaskStatus
Terminal task status।
task
RestTask
Failure के क्षण में पूरा task payload। Logging और user-facing error messages के लिए उपयोगी।

Retry pattern

retryable को idempotency key के साथ मिलाकर एक सुरक्षित retry loop बनाएं:
import { YoukaRequestError } from "@youka/sdk";

async function createWithRetry<T>(fn: () => Promise<T>, maxAttempts = 3) {
  let lastError: unknown;

  for (let attempt = 1; attempt <= maxAttempts; attempt++) {
    try {
      return await fn();
    } catch (error) {
      lastError = error;
      if (
        error instanceof YoukaRequestError &&
        error.retryable &&
        attempt < maxAttempts
      ) {
        await new Promise((r) => setTimeout(r, 2 ** attempt * 1_000));
        continue;
      }
      throw error;
    }
  }

  throw lastError;
}

await createWithRetry(() =>
  client.projects.create(body, {
    idempotencyKey: "import-2026-04-08-song-001",
  }),
);
Retries के दौरान हमेशा वही idempotency key reuse करें। वरना server retry को एक नया request मानता है और आप duplicates के साथ समाप्त हो सकते हैं।

Abort and cancellation

किसी request को abort करने पर standard AbortError throw होता है — YoukaRequestError नहीं। इसे explicit रूप से check करें:
try {
  await client.projects.wait(created, { signal: controller.signal });
} catch (error) {
  if (error instanceof Error && error.name === "AbortError") {
    console.log("User द्वारा cancelled");
    return;
  }
  throw error;
}

आगे क्या

  • Tasks — wait helpers और advanced task polling
  • Authentication — constructor options और signals
  • API errors — raw HTTP में वही codes