> ## Documentation Index
> Fetch the complete documentation index at: https://docs.restate.dev/llms.txt
> Use this file to discover all available pages before exploring further.

<AgentInstructions>

## Submitting Feedback

If you encounter incorrect, outdated, or confusing documentation on this page, submit feedback:

POST https://docs.restate.dev/feedback

```json
{
  "path": "/develop/ts/error-handling",
  "feedback": "Description of the issue"
}
```

Only submit feedback when you have something specific and actionable to report.

</AgentInstructions>

# Error Handling

> Stop infinite retries with Terminal Errors.

Restate handles retries for failed invocations.

<Info>
  Check out the [Error Handling guide](/guides/error-handling) to learn more about how Restate handles transient errors, terminal errors, retries, and timeouts.
</Info>

## Retry strategies

By default, Restate does infinite retries with an exponential backoff strategy.

Check out the [error handling guide](/guides/error-handling) to learn how to customize this.

## Terminal Errors

For failures for which you do not want retries, but instead want the invocation to end and the error message
to be propagated back to the caller, you can throw a **terminal error**.

You can throw a `TerminalError` with an optional HTTP status code and a message anywhere in your handler, as follows:

```typescript {"CODE_LOAD::ts/src/develop/error_handling.ts#terminal"}  theme={null}
throw new TerminalError("Something went wrong.", { errorCode: 500 });
```

You can catch terminal errors, and build your control flow around it.

<Info>
  When you throw a terminal error, you might need to undo the actions you did earlier in your handler to make sure that your system remains in a consistent state.
  Have a look at our [sagas guide](/guides/sagas) to learn more.
</Info>

## Retryable errors with custom delay

Use `RetryableError` to signal that Restate should retry with a specific delay.
This is useful when interacting with external APIs that return a `Retry-After` header.

`RetryableError` is primarily designed for use inside `ctx.run` blocks:

```typescript {"CODE_LOAD::ts/src/develop/error_handling.ts#retryable"}  theme={null}
await ctx.run("call API", async () => {
    const res = await fetch("https://api.example.com/data");
    if (!res.ok) {
        const retryAfter = res.headers.get("Retry-After");
        throw new RetryableError("Rate limited", {
            retryAfter: { seconds: Number(retryAfter ?? 30) },
        });
    }
    return res.json();
}, { maxRetryAttempts: 10 });
```

You can also wrap an existing error using the `RetryableError.from` helper:

```typescript {"CODE_LOAD::ts/src/develop/error_handling.ts#retryable_from"}  theme={null}
throw RetryableError.from(cause, {
    retryAfter: { seconds: 30 },
});
```

<Note>
  Unlike `TerminalError` which stops retries permanently, `RetryableError` tells Restate to retry after the specified delay. You can combine it with `maxRetryAttempts` and `maxRetryDuration` in the run options.
</Note>

## Mapping errors to `TerminalError`

If you're using external libraries (e.g., for validation), you might want to automatically convert certain error types into terminal errors.

You can do this using the `asTerminalError` option in your [service configuration](/services/configuration).

For example, to fail with `TerminalError` for each `MyValidationError`, do the following:

```typescript {"CODE_LOAD::ts/src/develop/error_handling.ts#as_terminal"}  theme={null}
class MyValidationError extends Error {}

const greeter = restate.service({
  name: "greeter",
  handlers: {
    greet: async (ctx: restate.Context, name: string) => {
      if (name.length === 0) {
        throw new MyValidationError("Length too short");
      }
      return `Hello ${name}`;
    },
  },
  options: {
    asTerminalError: (err) => {
      if (err instanceof MyValidationError) {
        // My validation error is terminal
        return new restate.TerminalError(err.message, { errorCode: 400 });
      }
    },
  },
});
```
