Errors
The driver throws HttpError for non-2xx responses.
Related page: Low-level HTTP client for HttpClient details and NDJSON streaming.
HttpError
- TypeScript
- JavaScript
class HttpError extends Error {
status: number;
data?: unknown;
}
class HttpError extends Error {
// status: number
// data?: unknown
}
In tooling and CLIs, you often want to log error.data if available (it may include server error details).
Example: log status + data
- TypeScript
- JavaScript
import { HttpError, LioranClient } from "@liorandb/driver";
const client = new LioranClient("http://localhost:4000");
try {
await client.login("admin", "wrong-password");
} catch (err) {
if (err instanceof HttpError) {
console.error("HTTP", err.status, err.data);
} else {
console.error(err);
}
}
import { HttpError, LioranClient } from "@liorandb/driver";
const client = new LioranClient("http://localhost:4000");
try {
await client.login("admin", "wrong-password");
} catch (err) {
if (err instanceof HttpError) {
console.error("HTTP", err.status, err.data);
} else {
console.error(err);
}
}
Example: handle 401 and re-authenticate
- TypeScript
- JavaScript
async function withRetry<T>(fn: () => Promise<T>) {
try {
return await fn();
} catch (err) {
if (err instanceof HttpError && err.status === 401) {
await client.login("admin", "admin");
return await fn();
}
throw err;
}
}
const db = await client.db("default");
const users = db.collection("users");
console.log(await withRetry(() => users.count()));
async function withRetry(fn) {
try {
return await fn();
} catch (err) {
if (err instanceof HttpError && err.status === 401) {
await client.login("admin", "admin");
return await fn();
}
throw err;
}
}
const db = await client.db("default");
const users = db.collection("users");
console.log(await withRetry(() => users.count()));