Errors
The error the SDK reports, its codes, and which ones are worth retrying.
Every SDK call can fail, and every failure is an EngineError with the
server's code. This page lists the codes and shows how to handle them with
the promise and the Effect forms.
Telling errors apart
An EngineError has a code, a message, and the HTTP status it was
answered with (0 when no answer arrived, or when the SDK refused the call
before sending it). Act on the code; the message is for people.
import { EngineError } from "@voidhash/mimic-server/engine";
try {
await boards.get(documentId);
} catch (error) {
if (error instanceof EngineError && error.code === "not_found") {
return undefined;
}
throw error;
}With EngineClient, the error is in the effect's error channel, tagged
EngineError:
import * as Effect from "effect/Effect";
const board = boards.effect.get(documentId).pipe(
Effect.catchIf(
(error) => error.code === "not_found",
() => Effect.succeed(undefined),
),
);Codes
Each code belongs to a category, which says whether the call can succeed if it is sent again:
| Category | Codes | Retry |
|---|---|---|
invalid_argument | invalid_argument, invalid_value, invalid_path, invalid_order_key, limit_exceeded, invalid_artifact | Never: the same request fails again |
failed_precondition | type_mismatch, missing_field, missing_element, incarnation_mismatch, revision_mismatch, schema_mismatch, and others | After reading the current state again |
validation_failed | validation_failed, migration_blocked | Never as it is |
conflict | already_exists, transaction_id_reused | Never |
not_found | not_found | Never |
unauthenticated | unauthenticated | With a new credential |
permission_denied | permission_denied | Never: fix the credential or its permission |
retry_horizon_exceeded | retry_horizon_exceeded | Never |
transaction_id_ahead | transaction_id_ahead | Once the server's clock catches up |
resource_exhausted | resource_exhausted | With backoff: a limit is reached |
unavailable | unavailable | With backoff |
data_corruption | data_corruption, migration_failed | Never: an operator must act |
internal | internal | With backoff |
Two of these deserve care:
not_founddoes not reveal existence. A document, collection or outcome the credential may not see isnot_found, the same as one that does not exist; a database it holds no permission on ispermission_deniedwhether or not it exists.migration_failedis not a deletion. The document's data is intact and becomes readable again once an operator repairs it. Do not treat it as "not found".
The SDK reports errors it makes itself with the same class: invalid_argument
or validation_failed for input it refused before sending (with status
0), and unavailable with status 0 for a call that got no answer in
time.
Retrying
The SDK already retries what is safe to retry. A read, and a write, which
carries its transaction id, are sent again when they are answered without a
verdict (no answer, unavailable, resource_exhausted or internal), up to
maxAttempts times with backoff. A write is evaluated at most once however
often it is sent: a resend is answered with the outcome recorded the first
time. Minting a token is not retried.
When the SDK gives up, the write's outcome is unknown. Send it again with the
same transaction id, or read its outcome with outcome(document, id); see
Transactions from a backend.
Rejected writes are not errors
A write the server evaluated and refused resolves with an outcome whose
result.kind is rejected, with a code such as revision_mismatch or
validation_failed. What fails is the call itself: the document does not
exist (not_found), the credential may not write (permission_denied), the
request is malformed, or no answer arrived. See
Transactions from a backend.
Releasing the SDK
Calls made after sdk.dispose() fail. Dispose the SDK only when the process
is shutting down.