abhigyanpatwari/GitNexus · error

Prepare failed: ${errMsg}

Error message

Prepare failed: ${errMsg}

What it means

Thrown by `executePrepared` when `conn.prepare(cypher)` returns a non-success result (`stmt.isSuccess()` is false). The engine's error text is fetched via `stmt.getErrorMessage()` and appended. This is a prepare-time failure — the Cypher query itself could not be compiled (syntax error, unknown label/property, schema mismatch). Runs under the connection lock (`withConnLock`) so no WAL checkpoint can interleave.

Source

Thrown at gitnexus/src/core/lbug/lbug-adapter.ts:1798

/**
 * Execute a single parameterized query (prepare/execute pattern).
 * Prevents Cypher injection by binding values as parameters.
 */
export const executePrepared = async (
  cypher: string,
  params: Record<string, any>,
): Promise<any[]> => {
  // A `.length` compare on text we already hold; never throws (#2915).
  warnIfQueryTextUnbounded(cypher, 'executePrepared', (message) => logger.warn(message));
  const c = conn;
  if (!c) {
    throw new Error('LadybugDB not initialized. Call initLbug first.');
  }
  return withConnLock(async () => {
    const stmt = await c.prepare(cypher);
    if (!stmt.isSuccess()) {
      const errMsg = await stmt.getErrorMessage();
      throw new Error(`Prepare failed: ${errMsg}`);
    }
    const queryResult = await c.execute(stmt, params);
    return await readQueryRows(queryResult);
  });
};

export const executeWithReusedStatement = async (
  cypher: string,
  paramsList: Array<Record<string, any>>,
): Promise<void> => {
  const c = conn;
  if (!c) {
    throw new Error('LadybugDB not initialized. Call initLbug first.');
  }
  if (paramsList.length === 0) return;

  const SUB_BATCH_SIZE = 4;
  for (const [subBatchIndex, subBatch] of chunk(paramsList, SUB_BATCH_SIZE).entries()) {

View on GitHub (pinned to d540b00184)

Solutions

  1. Inspect the `errMsg` in the thrown message — it is the engine's compile error and names the exact syntax/schema problem.
  2. Validate the label/rel-type/property names in the query against the current schema before calling executePrepared.
  3. Run the Cypher in a query console against the same DB to reproduce the prepare error in isolation.
  4. If the query is dynamic, add a Cypher-syntax validation/lint step before execution.

Example fix

// before — typo in label name
await executePrepared('MATCH (s:Symbl) RETURN s', {});
// → Prepare failed: ... Symbl ...

// after — correct label
await executePrepared('MATCH (s:Symbol) RETURN s', {});
Defensive patterns

Strategy: validation

Validate before calling

// Validate the query compiles against a known schema before batching it in prod.
// Cheapest check: run a 0-row prepare in a dev/test DB; if it throws, fix the query.
async function assertPrepareOk(cypher) {
  try {
    const stmt = await conn.prepare(cypher);
    if (!stmt.isSuccess()) {
      throw new Error(`Query would fail to prepare: ${await stmt.getErrorMessage()}`);
    }
  } finally { /* PreparedStatement needs no close */ }
}
await assertPrepareOk(cypher);
await executePrepared(cypher, params);

Type guard

function isPrepareFailed(err): boolean {
  return err instanceof Error && err.message.startsWith('Prepare failed:');
}

Try / catch

try {
  return await executePrepared(cypher, params);
} catch (err) {
  if (err instanceof Error && err.message.startsWith('Prepare failed:')) {
    // Compile-time query error — do NOT retry; fix the query/schema.
    throw new Error(`Bad Cypher (prepare failed): ${err.message} [query: ${cypher}]`, { cause: err });
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling `executePrepared(cypher, params)` with a Cypher string that fails to prepare: a syntax error, a reference to a node label/rel type or property that does not exist in the schema, or a malformed parameterized query.

Common situations: A dynamically-built Cypher string with a typo or wrong label name; a schema change that renamed/removed a label; a query written against a different (older/newer) index schema; an unescaped user input producing invalid Cypher.

Related errors


AI-assisted analysis of abhigyanpatwari/GitNexus@d540b00184 (2026-08-12). Data as JSON: /api/errors/dc19b1705f1dbc79. Report an issue: GitHub.