Skip to content

Commit 707e3f1

Browse files
committed
fix: replace plain Error throws with QueryModeError in hnsw.ts + pg-wire/protocol.ts
- hnsw.ts: dimension mismatch → SCHEMA_MISMATCH, non-finite vector → QUERY_FAILED, non-sequential ID → QUERY_FAILED, buffer length → SCHEMA_MISMATCH, bad magic → INVALID_FORMAT - pg-wire/protocol.ts: unsupported protocol version → INVALID_FORMAT - Zero remaining plain Error throws in production code
1 parent af924e2 commit 707e3f1

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/hnsw.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* using Hierarchical Navigable Small World graphs", 2016.
1010
*/
1111

12+
import { QueryModeError } from "./errors.js";
13+
1214
// ---------------------------------------------------------------------------
1315
// Distance functions
1416
// ---------------------------------------------------------------------------
@@ -50,11 +52,11 @@ export function dotDistance(a: Float32Array, b: Float32Array): number {
5052
/** Validate that a vector contains only finite values. */
5153
function validateVector(vec: Float32Array, dim: number, label: string): void {
5254
if (vec.length !== dim) {
53-
throw new Error(`${label}: expected dimension ${dim}, got ${vec.length}`);
55+
throw new QueryModeError("SCHEMA_MISMATCH", `${label}: expected dimension ${dim}, got ${vec.length}`);
5456
}
5557
for (let i = 0; i < vec.length; i++) {
5658
if (!Number.isFinite(vec[i])) {
57-
throw new Error(`${label} contains non-finite value at index ${i}: ${vec[i]}`);
59+
throw new QueryModeError("QUERY_FAILED", `${label} contains non-finite value at index ${i}: ${vec[i]}`);
5860
}
5961
}
6062
}
@@ -370,7 +372,7 @@ export class HnswIndex {
370372

371373
// Store vector
372374
if (id !== this._size) {
373-
throw new Error(`IDs must be sequential. Expected ${this._size}, got ${id}`);
375+
throw new QueryModeError("QUERY_FAILED", `IDs must be sequential. Expected ${this._size}, got ${id}`);
374376
}
375377
this.vectors.push(vector);
376378

@@ -442,7 +444,7 @@ export class HnswIndex {
442444
/** Batch add vectors from a contiguous Float32Array. */
443445
addBatch(vectors: Float32Array, dim: number): void {
444446
if (vectors.length % dim !== 0) {
445-
throw new Error(`Vector buffer length ${vectors.length} is not divisible by dim ${dim}`);
447+
throw new QueryModeError("SCHEMA_MISMATCH", `Vector buffer length ${vectors.length} is not divisible by dim ${dim}`);
446448
}
447449
const count = vectors.length / dim;
448450
const startId = this._size;
@@ -590,7 +592,7 @@ export class HnswIndex {
590592
// Header
591593
const magic = view.getUint32(offset, true); offset += 4;
592594
if (magic !== HNSW_MAGIC) {
593-
throw new Error(`Invalid HNSW magic: 0x${magic.toString(16)}, expected 0x${HNSW_MAGIC.toString(16)}`);
595+
throw new QueryModeError("INVALID_FORMAT", `Invalid HNSW magic: 0x${magic.toString(16)}, expected 0x${HNSW_MAGIC.toString(16)}`);
594596
}
595597

596598
const dim = view.getUint32(offset, true); offset += 4;

src/pg-wire/protocol.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
* Reference: https://www.postgresql.org/docs/current/protocol-message-formats.html
1010
*/
1111

12+
import { QueryModeError } from "../errors.js";
13+
1214
// ── Frontend (client → server) messages ─────────────────────────────────
1315

1416
export interface StartupMessage {
@@ -58,7 +60,7 @@ export function parseStartupMessage(buf: Uint8Array): FrontendMessage | null {
5860
}
5961

6062
if (code !== PROTOCOL_VERSION_3) {
61-
throw new Error(`Unsupported protocol version: ${code}`);
63+
throw new QueryModeError("INVALID_FORMAT", `Unsupported Postgres protocol version: ${code}`);
6264
}
6365

6466
const params = new Map<string, string>();

0 commit comments

Comments
 (0)