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:

CategoryCodesRetry
invalid_argumentinvalid_argument, invalid_value, invalid_path, invalid_order_key, limit_exceeded, invalid_artifactNever: the same request fails again
failed_preconditiontype_mismatch, missing_field, missing_element, incarnation_mismatch, revision_mismatch, schema_mismatch, and othersAfter reading the current state again
validation_failedvalidation_failed, migration_blockedNever as it is
conflictalready_exists, transaction_id_reusedNever
not_foundnot_foundNever
unauthenticatedunauthenticatedWith a new credential
permission_deniedpermission_deniedNever: fix the credential or its permission
retry_horizon_exceededretry_horizon_exceededNever
transaction_id_aheadtransaction_id_aheadOnce the server's clock catches up
resource_exhaustedresource_exhaustedWith backoff: a limit is reached
unavailableunavailableWith backoff
data_corruptiondata_corruption, migration_failedNever: an operator must act
internalinternalWith backoff

Two of these deserve care:

  • not_found does not reveal existence. A document, collection or outcome the credential may not see is not_found, the same as one that does not exist; a database it holds no permission on is permission_denied whether or not it exists.
  • migration_failed is 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.