Zap Studio

RetryError

Terminal error class thrown when retries are exhausted.

RetryError represents exhausted retry attempts with structured context.

Import

import { RetryError } from "@zap-studio/retry/error";

Properties

PropertyTypeDescription
namestringAlways "RetryError"
messagestringHuman-readable terminal message
attemptsnumberTotal attempts performed
lastErrorunknownLast captured error
lastDataunknownLast captured data value

Default Behavior

BaseRetryPolicy.onExhausted(...) returns a RetryError by default.

const policy = new ExponentialBackoff({
  maxAttempts: 3,
  baseDelayMs: 100,
  maxDelayMs: 500,
});

When retries are exhausted, policy.run(...) throws the value returned by onExhausted(...).

Catching RetryError

try {
  await policy.run(async () => {
    return await doWork();
  });
} catch (error) {
  if (error instanceof RetryError) {
    console.error("Attempts:", error.attempts);
    console.error("Last error:", error.lastError);
  } else {
    throw error;
  }
}

If you run with throwOnExhausted: false, exhausted runs return a result object instead of throwing.

Graceful Error Handling

const result = await policy.run(
  async () => {
    return await doWork();
  },
  { throwOnExhausted: false },
);

if (!result.ok) {
  console.error("Exhausted after attempts:", result.attempts);
  console.error("Last error:", result.error.lastError);
} else {
  console.log(result.value);
}

Custom Terminal Errors

You can override onExhausted(...) in a custom policy to throw your own error type while keeping the same retry orchestration flow.

Edit on GitHub

Last updated on

On this page