Skip to content

Error Reference

StateMesh exports 16 typed error classes. Each has name, code, cause, metadata, and timestamp.

Error Classes

ClassCodeUsed By
StateMeshErrorSTATEMESH_ERRORBase class for all errors
ProviderErrorSTATEMESH_PROVIDER_ERRORMissing or invalid StateMeshProvider
SelectorErrorSTATEMESH_SELECTOR_ERRORSelector threw during evaluation
ComputedErrorSTATEMESH_COMPUTED_ERRORComputed function threw
ActionErrorSTATEMESH_ACTION_ERRORAction handler threw
DuplicateRegistrationErrorSTATEMESH_DUPLICATE_REGISTRATIONDuplicate action/transaction/computed/form name
TransactionErrorSTATEMESH_TRANSACTION_ERRORTransaction failed (all retries exhausted)
TransactionRollbackErrorSTATEMESH_TRANSACTION_ROLLBACK_ERRORRollback handler threw
ResourceErrorSTATEMESH_RESOURCE_ERRORResource fetch failed
MutationErrorSTATEMESH_MUTATION_ERRORMutation failed
ApiClientErrorSTATEMESH_API_CLIENT_ERRORAPI request failed (has status)
GuardErrorSTATEMESH_GUARD_ERROROperation blocked by guard
PersistenceErrorSTATEMESH_PERSISTENCE_ERRORPersistence read/write failed
UrlStateErrorSTATEMESH_URL_STATE_ERRORURL state serialization failed
FormErrorSTATEMESH_FORM_ERRORForm validation or submission failed
SyncErrorSTATEMESH_SYNC_ERRORCross-tab sync failed

Error Properties

PropertyTypeDescription
namestringError class name
codestringMachine-readable error code
messagestringHuman-readable message
causeError | undefinedThe original error
metadataobjectContext-specific data
timestampnumberDate.now() when the error was created

Helper Functions

ts
import {
  getErrorMessage,
  getErrorMetadata,
  getErrorStatus,
  isStateMeshError,
  isApiClientError
} from "statemesh-core";

// Safe message extraction
getErrorMessage(error);       // string

// Metadata extraction
getErrorMetadata(error);      // object

// HTTP status (ApiClientError only)
getErrorStatus(error);        // number | undefined

// Type guards
isStateMeshError(error);      // true if StateMeshError
isApiClientError(error);      // true if ApiClientError

Usage

ts
import { TransactionError, ApiClientError, isApiClientError } from "statemesh-core";

try {
  await checkout.run();
} catch (error) {
  if (error instanceof TransactionError) {
    console.log(error.code);       // "STATEMESH_TRANSACTION_ERROR"
    console.log(error.metadata);   // { transaction: "cart.checkout", payload: ... }
    console.log(error.cause);      // Original error
  }
  if (isApiClientError(error)) {
    console.log(error.status);     // 401
  }
}

Important Notes

TIP

All errors extend StateMeshError, which extends Error. Use instanceof StateMeshError to catch any StateMesh error.

TIP

Error metadata includes context-specific data. TransactionError includes the transaction name and payload. ApiClientError includes the HTTP status and response headers.

Released under the MIT License.