Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK documentation on error handling #122

Merged
merged 2 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ description: ""

# Examples

Do you want to see Restate in action? Here are some read-to-run examples for you.
Do you want to see Restate in action? Here are some ready-to-run examples for you.
Pick an example of the list below and follow the instructions in the README to run it.

- [Ticket reservation system](https://github.com/restatedev/example-ticket-reservation-system): a subset of a ticket booking system to illustrate how Restate's keyed-sharding and concurrency guarantees simplify microservice architectures.
Expand Down
32 changes: 30 additions & 2 deletions docs/services/sdk/error-handling.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
---
sidebar_position: 8
description: "Restate's retry strategy and how to manage it from the SDK."
draft: true
---

# Error handling
# Error handling

Restate takes care of retries of failed invocations.
It does this for all kinds of errors, such as transient network errors and errors thrown inside handlers.
By default, Restate does infinite retries with an exponential backoff strategy.

For situations where you do not want the invocation to be retried, but instead you want the error message
to be propagated back to the caller, you can use a `terminal error`.

```typescript
import { TerminalError } from "@restatedev/restate-sdk";

// Inside your handler throw terminal errors via:
throw new TerminalError("Something went wrong.", { errorCode: 13, cause: "Something caused this." })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we expose the error codes as an enum from the SDK?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, we indeed expose those 👍 I will adapt it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm the only ones we expose are: internal, journal_mismatch and protocol_violation... Maybe that is not really worth exposing or documenting? Since journal mismatch and protocol violations are not something the user code detects

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we shouldn't expose journal_mismatch or protocol_violation to the user. Maybe at some point we want to expose something like an UserErrorCode that is aligned with https://github.com/restatedev/restate/blob/ebf084cdfa2639e192911d90394fdcc0168eeefe/src/types/src/errors.rs#L21 in case users want to use error codes across SDKs. Then let's leave it as is for now.

```

The `errorCode` and `cause` are optional. You can throw a terminal error anywhere in your handler.
Restate will propagate the error code and message back to the caller and will consider this invocation to be finished.

## Errors during side effect execution

Throwing errors inside a side effect has the same semantics as anywhere else in your handler code.
By default, Restate infinitely retries the invocation, and therefore the side effect.
Errors that should not be retried, should be thrown as terminal errors, as described earlier.

Invocation retries are triggered by Restate, not by the SDKs.
For some use cases, you may want to do the side effect retries from within the invocation,
because this is faster and more efficient in certain scenarios (e.g. you have a connection set up to an external system or database).
Have a look at the [side effect documentation](https://docs.restate.dev/services/sdk/side-effects#retrying-on-failure)
on how to set this up.