MemorySync
SDKs · Node.js

Node.js Types & Errors

Use exported TypeScript types and six instanceof-checkable error classes to make every outcome explicit. Keep retries narrow and treat interrupted writes as uncertain.

Public result families

FamilyExamplesHow to consume it
Memory interfacesMemoryRecord, QueryResponse, BulkAddResponse, ComposeResponseUse camelCase properties.
Add unionMemoryRecord | AddSkippedResponseNarrow with the status discriminator.
Control-plane interfacesLoginResponse, TeamMember, Webhook, AuditEventListResponseUse exported types and tolerate documented null or optional fields.
Delete resultnumber[]Compare confirmed IDs with requested IDs.

Error classes

ClassTypical conditionDefault action
AuthError401 or 403Fix authentication, permission, or scope.
ValidationError400, 409, 422, or client validationCorrect the request; do not retry unchanged.
NotFoundError404Show an unavailable state without revealing other scopes.
RateLimitError429Use retryAfterSeconds and bound attempts.
ServerError5xxRetry safe reads with bounded backoff; reconcile writes.
MemorySyncErrorNetwork, timeout, or other SDK failureTreat mutation outcomes as uncertain.

Narrow errors with instanceof

handle-errors.ts
import {
AuthError,
MemorySyncError,
NotFoundError,
RateLimitError,
ServerError,
ValidationError,
} from "memorysync-sdk";
try {
const memory = await client.get(101);
console.log(memory.text);
} catch (error) {
if (error instanceof NotFoundError) {
console.log("Memory is unavailable in this scope");
} else if (error instanceof AuthError || error instanceof ValidationError) {
throw error;
} else if (error instanceof RateLimitError) {
console.log("Retry after", error.retryAfterSeconds);
throw error;
} else if (error instanceof ServerError) {
throw error;
} else if (error instanceof MemorySyncError) {
throw error;
} else {
throw error;
}
}

Retry reads narrowly

retry-read.ts
import { RateLimitError, ServerError } from "memorysync-sdk";
const sleep = (milliseconds: number) =>
new Promise((resolve) => setTimeout(resolve, milliseconds));
async function queryWithRetry(question: string, attempts = 3) {
for (let attempt = 0; attempt < attempts; attempt += 1) {
try {
return await client.query({ query: question, k: 5 });
} catch (error) {
if (attempt === attempts - 1) throw error;
if (error instanceof RateLimitError) {
await sleep((error.retryAfterSeconds || 1) * 1000);
} else if (error instanceof ServerError) {
await sleep(250 * 2 ** attempt);
} else {
throw error;
}
}
}
throw new Error("unreachable");
}

Use error context defensively

PropertyTypeAvailability
statusCodenumber | undefinedPresent when an HTTP response supplied a status.
responseunknownParsed response when one was available.
requestIdstring | undefinedPresent only when the server returned a request identifier.
retryAfterSecondsnumberAvailable on RateLimitError.

Log a safe support record

logging.ts
import { MemorySyncError } from "memorysync-sdk";
try {
await client.query({ query: "What preference applies?", k: 5 });
} catch (error) {
if (error instanceof MemorySyncError) {
console.warn("memory query failed", {
statusCode: error.statusCode,
requestId: error.requestId,
});
}
throw error;
}

Related contracts