mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
feat(node): surface Rust errors as typed JavaScript errors (#4901)
## Summary The Node SDK now throws typed `LanceError` subclasses instead of one generic `Error` with an indented `Caused by` string. Fixes #222 ## Before / after ```ts try { await db.openTable(\"missing\"); } catch (error) { if (error instanceof TableNotFoundError) { error.code; // \"TableNotFound\" error.tableName; // \"missing\" error.message; // \"Table 'missing' was not found\" error.cause; // underlying dataset error } } ``` ## Changes - `nodejs/src/error.rs` — map each `lancedb::Error` variant to a one-line header + `error.cause` - `nodejs/src/query.rs`, `nodejs/src/merge.rs` — pass errors through instead of stringifying - `nodejs/lancedb/errors.ts`, `errors_install.ts` — real subclasses when the package loads - Tests + generated API docs under `docs/src/js/` ## Test plan - [x] Native debug build, `tsc`, Biome, `cargo clippy -p lancedb-nodejs` - [x] Jest: new error tests, remote retry, connection/query/table (594 passed) ## Notes Node-API cannot construct a custom `Error` subclass from an async Rust rejection, so translation happens in JS when `@lancedb/lancedb` loads. Calling the raw native addon without that entrypoint still shows the header line. --------- Signed-off-by: ZYZ666-RGB <1991039819@qq.com> Signed-off-by: yuzhou <1991039819@qq.com> Co-authored-by: Xuanwo <github@xuanwo.io>
This commit is contained in:
@@ -0,0 +1,386 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-FileCopyrightText: Copyright The LanceDB Authors
|
||||
|
||||
import * as vm from "node:vm";
|
||||
import * as tmp from "tmp";
|
||||
|
||||
import {
|
||||
ColumnNotFoundError,
|
||||
FieldNotFoundError,
|
||||
HttpError,
|
||||
InvalidInputError,
|
||||
InvalidTableNameError,
|
||||
JobFailedError,
|
||||
LanceError,
|
||||
SqlError,
|
||||
TableAlreadyExistsError,
|
||||
TableNotFoundError,
|
||||
connect,
|
||||
isAlreadyExistsError,
|
||||
isLanceError,
|
||||
} from "../lancedb";
|
||||
import { LANCE_ERROR_MARK, materializeLanceError } from "../lancedb/errors";
|
||||
|
||||
type LanceDbModule = typeof import("../lancedb");
|
||||
type ErrorsModule = typeof import("../lancedb/errors");
|
||||
|
||||
describe("materializeLanceError", () => {
|
||||
it("rebuilds a native header as a typed error and keeps the cause chain", () => {
|
||||
const cause = new Error(
|
||||
'LanceError {"code":"Http","requestId":"req-1","statusCode":404}\nHttp error: (request_id=req-1) missing',
|
||||
);
|
||||
const raw = new Error(
|
||||
'LanceError {"code":"TableNotFound","tableName":"missing"}\nTable \'missing\' was not found',
|
||||
);
|
||||
raw.cause = cause;
|
||||
|
||||
const error = materializeLanceError(raw);
|
||||
|
||||
expect(error).toBeInstanceOf(TableNotFoundError);
|
||||
expect(error).toBeInstanceOf(LanceError);
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
if (!(error instanceof TableNotFoundError)) {
|
||||
throw error;
|
||||
}
|
||||
expect(error.code).toBe("TableNotFound");
|
||||
expect(error.name).toBe("TableNotFoundError");
|
||||
expect(error.message).toBe("Table 'missing' was not found");
|
||||
expect(error.message).not.toContain("Caused by:");
|
||||
expect(error.message).not.toContain("LanceError");
|
||||
expect(error.tableName).toBe("missing");
|
||||
expect(error.cause).toBeInstanceOf(HttpError);
|
||||
if (!(error.cause instanceof HttpError)) {
|
||||
throw error.cause;
|
||||
}
|
||||
expect(error.cause.code).toBe("Http");
|
||||
expect(error.cause.requestId).toBe("req-1");
|
||||
expect(error.cause.statusCode).toBe(404);
|
||||
expect(error.cause.message).toBe("Http error: (request_id=req-1) missing");
|
||||
expect(error.stack).toContain(
|
||||
"TableNotFoundError: Table 'missing' was not found",
|
||||
);
|
||||
});
|
||||
|
||||
it("keeps the SQL error of a failed isolated job as its cause", () => {
|
||||
const raw = new Error(
|
||||
'LanceError {"code":"JobFailed","jobId":"sql-job","sqlJobId":"sql-job"}\nJob sql-job failed',
|
||||
);
|
||||
raw.cause = new Error(
|
||||
'LanceError {"code":"Sql","requestId":"worker-request","grpcCode":5}\nmissing (request_id=worker-request)',
|
||||
);
|
||||
|
||||
const error = materializeLanceError(raw);
|
||||
|
||||
expect(error).toBeInstanceOf(JobFailedError);
|
||||
if (!(error instanceof JobFailedError)) {
|
||||
throw error;
|
||||
}
|
||||
expect(error.cause).toBeInstanceOf(SqlError);
|
||||
if (!(error.cause instanceof SqlError)) {
|
||||
throw error.cause;
|
||||
}
|
||||
expect(error.cause.requestId).toBe("worker-request");
|
||||
expect(error.cause.grpcCode).toBe(5);
|
||||
});
|
||||
|
||||
it("maps field misses onto ColumnNotFoundError and preserves the specific code", () => {
|
||||
const error = materializeLanceError(
|
||||
new Error(
|
||||
'LanceError {"code":"FieldNotFound","columnName":"vector"}\nNo field named vector',
|
||||
),
|
||||
);
|
||||
|
||||
expect(error).toBeInstanceOf(FieldNotFoundError);
|
||||
expect(error).toBeInstanceOf(ColumnNotFoundError);
|
||||
if (!(error instanceof FieldNotFoundError)) {
|
||||
throw error;
|
||||
}
|
||||
expect(error.code).toBe("FieldNotFound");
|
||||
expect(error.columnName).toBe("vector");
|
||||
});
|
||||
|
||||
it("keeps an unknown Rust kind as LanceError with that code", () => {
|
||||
const error = materializeLanceError(
|
||||
new Error('LanceError {"code":"FutureKind"}\nnot classified yet'),
|
||||
);
|
||||
|
||||
expect(error).toBeInstanceOf(LanceError);
|
||||
expect(error).not.toBeInstanceOf(TableNotFoundError);
|
||||
if (!(error instanceof LanceError)) {
|
||||
throw error;
|
||||
}
|
||||
expect(error.code).toBe("FutureKind");
|
||||
expect(error.message).toBe("not classified yet");
|
||||
});
|
||||
|
||||
it("leaves TypeScript errors that are not from Rust untouched", () => {
|
||||
const error = new Error("Table some_table is closed");
|
||||
expect(materializeLanceError(error)).toBe(error);
|
||||
expect(isLanceError(error)).toBe(false);
|
||||
});
|
||||
|
||||
it("rebuilds an error already translated by another module realm", () => {
|
||||
const cause = foreignLanceError({
|
||||
name: "HttpError",
|
||||
code: "Http",
|
||||
message: "Http error: (request_id=req-1) missing",
|
||||
requestId: "req-1",
|
||||
statusCode: 404,
|
||||
});
|
||||
const foreign = foreignLanceError({
|
||||
name: "TableNotFoundError",
|
||||
code: "TableNotFound",
|
||||
message: "Table 'missing' was not found",
|
||||
tableName: "missing",
|
||||
cause,
|
||||
});
|
||||
|
||||
const error = materializeLanceError(foreign);
|
||||
|
||||
expect(error).toBeInstanceOf(TableNotFoundError);
|
||||
expect(error).not.toBe(foreign);
|
||||
if (!(error instanceof TableNotFoundError)) {
|
||||
throw error;
|
||||
}
|
||||
expect(error.code).toBe("TableNotFound");
|
||||
expect(error.tableName).toBe("missing");
|
||||
expect(error.message).toBe("Table 'missing' was not found");
|
||||
expect(error.message).not.toContain("LanceError {");
|
||||
expect(error.cause).toBeInstanceOf(HttpError);
|
||||
if (!(error.cause instanceof HttpError)) {
|
||||
throw error.cause;
|
||||
}
|
||||
expect(error.cause.code).toBe("Http");
|
||||
expect(error.cause.requestId).toBe("req-1");
|
||||
expect(error.cause.statusCode).toBe(404);
|
||||
expect(error.cause.message).toBe("Http error: (request_id=req-1) missing");
|
||||
});
|
||||
|
||||
it("does not treat an unmarked foreign error as a LanceError", () => {
|
||||
const foreign = vm.runInNewContext(
|
||||
`(() => {
|
||||
const error = new Error("Table 'missing' was not found");
|
||||
error.code = "TableNotFound";
|
||||
error.tableName = "missing";
|
||||
return error;
|
||||
})()`,
|
||||
);
|
||||
|
||||
const error = materializeLanceError(foreign);
|
||||
|
||||
expect(error).not.toBeInstanceOf(LanceError);
|
||||
expect(error).not.toBeInstanceOf(TableNotFoundError);
|
||||
});
|
||||
|
||||
it("treats invalid table names as invalid input", () => {
|
||||
const error = new InvalidTableNameError("bad name");
|
||||
expect(error).toBeInstanceOf(InvalidInputError);
|
||||
expect(error.code).toBe("InvalidTableName");
|
||||
expect(isLanceError(error)).toBe(true);
|
||||
expect(isAlreadyExistsError(error)).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("native LanceDB errors", () => {
|
||||
let tmpDir: tmp.DirResult;
|
||||
|
||||
beforeEach(() => {
|
||||
tmpDir = tmp.dirSync({ unsafeCleanup: true });
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
tmpDir.removeCallback();
|
||||
});
|
||||
|
||||
it("throws TableNotFoundError when opening a missing table", async () => {
|
||||
const db = await connect(tmpDir.name);
|
||||
|
||||
await expect(db.openTable("missing")).rejects.toBeInstanceOf(
|
||||
TableNotFoundError,
|
||||
);
|
||||
|
||||
try {
|
||||
await db.openTable("missing");
|
||||
throw new Error("expected openTable to fail");
|
||||
} catch (error) {
|
||||
expect(isLanceError(error)).toBe(true);
|
||||
if (!(error instanceof TableNotFoundError)) {
|
||||
throw error;
|
||||
}
|
||||
expect(error.code).toBe("TableNotFound");
|
||||
expect(error.tableName).toBe("missing");
|
||||
expect(error.message).toContain("missing");
|
||||
expect(error.message).not.toContain("LanceError {");
|
||||
expect(error.message).not.toContain("Caused by:");
|
||||
}
|
||||
});
|
||||
|
||||
it("throws TableAlreadyExistsError when creating a table twice", async () => {
|
||||
const db = await connect(tmpDir.name);
|
||||
await db.createTable("docs", [{ id: 1 }]);
|
||||
|
||||
await expect(db.createTable("docs", [{ id: 2 }])).rejects.toBeInstanceOf(
|
||||
TableAlreadyExistsError,
|
||||
);
|
||||
|
||||
try {
|
||||
await db.createTable("docs", [{ id: 3 }]);
|
||||
throw new Error("expected createTable to fail");
|
||||
} catch (error) {
|
||||
expect(isAlreadyExistsError(error)).toBe(true);
|
||||
if (!(error instanceof TableAlreadyExistsError)) {
|
||||
throw error;
|
||||
}
|
||||
expect(error.code).toBe("TableAlreadyExists");
|
||||
expect(error.tableName).toBe("docs");
|
||||
expect(error.message).toContain("already exists");
|
||||
}
|
||||
});
|
||||
|
||||
it("throws ColumnNotFoundError when a filter names a missing column", async () => {
|
||||
const db = await connect(tmpDir.name);
|
||||
const table = await db.createTable("docs", [{ id: 1, vector: [0.1, 0.2] }]);
|
||||
|
||||
await expect(
|
||||
table.query().where("missing > 1").toArray(),
|
||||
).rejects.toBeInstanceOf(ColumnNotFoundError);
|
||||
});
|
||||
|
||||
it("keeps each SDK load's subclasses after another load has translated an error", async () => {
|
||||
const firstDir = tmp.dirSync({ unsafeCleanup: true });
|
||||
const secondDir = tmp.dirSync({ unsafeCleanup: true });
|
||||
try {
|
||||
let openMissing!: () => Promise<TableNotFoundError>;
|
||||
let firstTableNotFound!: LanceDbModule["TableNotFoundError"];
|
||||
jest.isolateModules(() => {
|
||||
const lancedb = require("../lancedb") as LanceDbModule;
|
||||
firstTableNotFound = lancedb.TableNotFoundError;
|
||||
openMissing = async () => {
|
||||
const db = await lancedb.connect(firstDir.name);
|
||||
try {
|
||||
await db.openTable("missing");
|
||||
throw new Error("expected openTable to fail");
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(lancedb.TableNotFoundError);
|
||||
if (!(error instanceof lancedb.TableNotFoundError)) {
|
||||
throw error;
|
||||
}
|
||||
expect(error.code).toBe("TableNotFound");
|
||||
expect(error.tableName).toBe("missing");
|
||||
expect(error.message).not.toContain("LanceError {");
|
||||
expect(error.message).not.toContain("Caused by:");
|
||||
return error;
|
||||
}
|
||||
};
|
||||
});
|
||||
const firstError = await openMissing();
|
||||
|
||||
let checkLaterLoad!: (previous: unknown) => Promise<void>;
|
||||
let laterTableNotFound!: LanceDbModule["TableNotFoundError"];
|
||||
jest.isolateModules(() => {
|
||||
const lancedb = require("../lancedb") as LanceDbModule;
|
||||
laterTableNotFound = lancedb.TableNotFoundError;
|
||||
const { materializeLanceError: materialize } =
|
||||
require("../lancedb/errors") as ErrorsModule;
|
||||
checkLaterLoad = async (previous: unknown) => {
|
||||
const rebuilt = materialize(previous);
|
||||
expect(rebuilt).toBeInstanceOf(lancedb.TableNotFoundError);
|
||||
expect(rebuilt).not.toBe(previous);
|
||||
if (!(rebuilt instanceof lancedb.TableNotFoundError)) {
|
||||
throw rebuilt;
|
||||
}
|
||||
expect(rebuilt.code).toBe("TableNotFound");
|
||||
expect(rebuilt.tableName).toBe("missing");
|
||||
expect(rebuilt.message).toBe(firstError.message);
|
||||
|
||||
const db = await lancedb.connect(secondDir.name);
|
||||
try {
|
||||
await db.openTable("missing");
|
||||
throw new Error("expected openTable to fail");
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(lancedb.TableNotFoundError);
|
||||
if (!(error instanceof lancedb.TableNotFoundError)) {
|
||||
throw error;
|
||||
}
|
||||
expect(error.code).toBe("TableNotFound");
|
||||
expect(error.tableName).toBe("missing");
|
||||
expect(error.message).not.toContain("LanceError {");
|
||||
}
|
||||
|
||||
await db.createTable("docs", [{ id: 1 }]);
|
||||
try {
|
||||
await db.createTable("docs", [{ id: 2 }]);
|
||||
throw new Error("expected createTable to fail");
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(lancedb.TableAlreadyExistsError);
|
||||
if (!(error instanceof lancedb.TableAlreadyExistsError)) {
|
||||
throw error;
|
||||
}
|
||||
expect(error.code).toBe("TableAlreadyExists");
|
||||
expect(error.tableName).toBe("docs");
|
||||
}
|
||||
|
||||
const table = await db.openTable("docs");
|
||||
try {
|
||||
await table.query().where("missing > 1").toArray();
|
||||
throw new Error("expected query to fail");
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(lancedb.LanceError);
|
||||
expect(error).toBeInstanceOf(lancedb.ColumnNotFoundError);
|
||||
if (!(error instanceof lancedb.ColumnNotFoundError)) {
|
||||
throw error;
|
||||
}
|
||||
expect(error.message).not.toContain("LanceError {");
|
||||
}
|
||||
};
|
||||
});
|
||||
await checkLaterLoad(firstError);
|
||||
|
||||
const stillFirst = await openMissing();
|
||||
expect(stillFirst).toBeInstanceOf(firstTableNotFound);
|
||||
expect(stillFirst.tableName).toBe("missing");
|
||||
expect(stillFirst).not.toBeInstanceOf(laterTableNotFound);
|
||||
expect(stillFirst).not.toBeInstanceOf(TableNotFoundError);
|
||||
} finally {
|
||||
firstDir.removeCallback();
|
||||
secondDir.removeCallback();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
function foreignLanceError(fields: {
|
||||
name: string;
|
||||
code: string;
|
||||
message: string;
|
||||
cause?: unknown;
|
||||
requestId?: string;
|
||||
statusCode?: number;
|
||||
tableName?: string;
|
||||
}): Error {
|
||||
return vm.runInNewContext(
|
||||
`(() => {
|
||||
class ForeignError extends Error {
|
||||
constructor(message, cause) {
|
||||
super(message, { cause });
|
||||
this.name = name;
|
||||
this.code = code;
|
||||
if (requestId !== undefined) this.requestId = requestId;
|
||||
if (statusCode !== undefined) this.statusCode = statusCode;
|
||||
if (tableName !== undefined) this.tableName = tableName;
|
||||
Object.defineProperty(this, mark, { value: true });
|
||||
}
|
||||
}
|
||||
return new ForeignError(message, cause);
|
||||
})()`,
|
||||
{
|
||||
name: fields.name,
|
||||
code: fields.code,
|
||||
message: fields.message,
|
||||
cause: fields.cause,
|
||||
requestId: fields.requestId,
|
||||
statusCode: fields.statusCode,
|
||||
tableName: fields.tableName,
|
||||
mark: LANCE_ERROR_MARK,
|
||||
},
|
||||
) as Error;
|
||||
}
|
||||
@@ -6,7 +6,12 @@ import * as http from "http";
|
||||
import { execFileSync } from "node:child_process";
|
||||
import * as os from "os";
|
||||
import * as path from "path";
|
||||
import { OAuthConfig, OAuthFlowType, OAuthSession } from "../lancedb/oauth";
|
||||
import {
|
||||
InvalidInputError,
|
||||
OAuthConfig,
|
||||
OAuthFlowType,
|
||||
OAuthSession,
|
||||
} from "../lancedb";
|
||||
|
||||
function tempCacheDir(): string {
|
||||
return fs.mkdtempSync(path.join(os.tmpdir(), "lancedb-oauth-cache-"));
|
||||
@@ -60,7 +65,20 @@ describe("OAuthSession", () => {
|
||||
scopes: ["openid"],
|
||||
flow: OAuthFlowType.DeviceCode,
|
||||
};
|
||||
expect(() => new OAuthSession(config)).toThrow(/token/);
|
||||
let thrown: unknown;
|
||||
try {
|
||||
new OAuthSession(config);
|
||||
} catch (error) {
|
||||
thrown = error;
|
||||
}
|
||||
expect(thrown).toBeInstanceOf(InvalidInputError);
|
||||
if (!(thrown instanceof InvalidInputError)) {
|
||||
throw thrown;
|
||||
}
|
||||
expect(thrown.code).toBe("InvalidInput");
|
||||
expect(thrown.message).toBe(
|
||||
"Invalid input, OAuthSession requires OAuthConfig.token_cache to be set",
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects azure managed identity persistence", () => {
|
||||
|
||||
@@ -10,8 +10,10 @@ import {
|
||||
ClientConfig,
|
||||
Connection,
|
||||
ConnectionOptions,
|
||||
HttpError,
|
||||
OAuthConfig,
|
||||
OAuthFlowType,
|
||||
RetryError,
|
||||
TlsConfig,
|
||||
blob,
|
||||
connect,
|
||||
@@ -792,11 +794,21 @@ describe("remote connection", () => {
|
||||
try {
|
||||
await db.tableNames();
|
||||
fail("expected an error");
|
||||
// biome-ignore lint/suspicious/noExplicitAny: skip
|
||||
} catch (e: any) {
|
||||
} catch (e: unknown) {
|
||||
expect(e).toBeInstanceOf(RetryError);
|
||||
if (!(e instanceof RetryError)) {
|
||||
throw e;
|
||||
}
|
||||
expect(e.code).toBe("Retry");
|
||||
expect(e.message).toContain("Hit retry limit for request_id=");
|
||||
expect(e.message).toContain("Caused by: Http error");
|
||||
expect(e.message).toContain("500 Internal Server Error");
|
||||
expect(e.message).not.toContain("Caused by:");
|
||||
expect(e.requestId).toEqual(expect.any(String));
|
||||
expect(e.cause).toBeInstanceOf(HttpError);
|
||||
if (!(e.cause instanceof HttpError)) {
|
||||
throw e.cause;
|
||||
}
|
||||
expect(e.cause.message).toContain("Http error");
|
||||
expect(`${e.message}\n${e.cause.message}`).toContain("500");
|
||||
}
|
||||
},
|
||||
{
|
||||
@@ -807,6 +819,34 @@ describe("remote connection", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("reports resource identity supplied only by x-lancedb-resource", async () => {
|
||||
const resource = Buffer.from(
|
||||
// biome-ignore lint/style/useNamingConvention: Flight resource wire format
|
||||
JSON.stringify({ kind: "table", name: "docs", namespace_path: ["ns"] }),
|
||||
).toString("base64url");
|
||||
await withMockDatabase(
|
||||
(_req, res) => {
|
||||
res
|
||||
.writeHead(400, { "x-lancedb-resource": resource })
|
||||
.end("bad request");
|
||||
},
|
||||
async (db) => {
|
||||
const error = await db.tableNames().then(
|
||||
() => undefined,
|
||||
(e: unknown) => e,
|
||||
);
|
||||
expect(error).toBeInstanceOf(HttpError);
|
||||
if (!(error instanceof HttpError)) {
|
||||
throw error;
|
||||
}
|
||||
expect(error.statusCode).toBe(400);
|
||||
expect(error.resourceKind).toBe("table");
|
||||
expect(error.resourceName).toBe("docs");
|
||||
expect(error.namespacePath).toEqual(["ns"]);
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it("lists the rows a Function refresh skipped", async () => {
|
||||
const bodies: unknown[] = [];
|
||||
await withMockDatabase(
|
||||
|
||||
Reference in New Issue
Block a user