{"record":{"id":"dc19b1705f1dbc79","repo":"abhigyanpatwari/GitNexus","slug":"prepare-failed-errmsg","errorCode":null,"errorMessage":"Prepare failed: ${errMsg}","messagePattern":"Prepare failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gitnexus/src/core/lbug/lbug-adapter.ts","lineNumber":1798,"sourceCode":"/**\n * Execute a single parameterized query (prepare/execute pattern).\n * Prevents Cypher injection by binding values as parameters.\n */\nexport const executePrepared = async (\n  cypher: string,\n  params: Record<string, any>,\n): Promise<any[]> => {\n  // A `.length` compare on text we already hold; never throws (#2915).\n  warnIfQueryTextUnbounded(cypher, 'executePrepared', (message) => logger.warn(message));\n  const c = conn;\n  if (!c) {\n    throw new Error('LadybugDB not initialized. Call initLbug first.');\n  }\n  return withConnLock(async () => {\n    const stmt = await c.prepare(cypher);\n    if (!stmt.isSuccess()) {\n      const errMsg = await stmt.getErrorMessage();\n      throw new Error(`Prepare failed: ${errMsg}`);\n    }\n    const queryResult = await c.execute(stmt, params);\n    return await readQueryRows(queryResult);\n  });\n};\n\nexport const executeWithReusedStatement = async (\n  cypher: string,\n  paramsList: Array<Record<string, any>>,\n): Promise<void> => {\n  const c = conn;\n  if (!c) {\n    throw new Error('LadybugDB not initialized. Call initLbug first.');\n  }\n  if (paramsList.length === 0) return;\n\n  const SUB_BATCH_SIZE = 4;\n  for (const [subBatchIndex, subBatch] of chunk(paramsList, SUB_BATCH_SIZE).entries()) {","sourceCodeStart":1780,"sourceCodeEnd":1816,"githubUrl":"https://github.com/abhigyanpatwari/GitNexus/blob/d540b00184d71a896261ee02670da9a92d59d8f7/gitnexus/src/core/lbug/lbug-adapter.ts#L1780-L1816","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Inspect the `errMsg` in the thrown message — it is the engine's compile error and names the exact syntax/schema problem.","Validate the label/rel-type/property names in the query against the current schema before calling executePrepared.","Run the Cypher in a query console against the same DB to reproduce the prepare error in isolation.","If the query is dynamic, add a Cypher-syntax validation/lint step before execution."],"exampleFix":"// before — typo in label name\nawait executePrepared('MATCH (s:Symbl) RETURN s', {});\n// → Prepare failed: ... Symbl ...\n\n// after — correct label\nawait executePrepared('MATCH (s:Symbol) RETURN s', {});","handlingStrategy":"validation","validationCode":"// Validate the query compiles against a known schema before batching it in prod.\n// Cheapest check: run a 0-row prepare in a dev/test DB; if it throws, fix the query.\nasync function assertPrepareOk(cypher) {\n  try {\n    const stmt = await conn.prepare(cypher);\n    if (!stmt.isSuccess()) {\n      throw new Error(`Query would fail to prepare: ${await stmt.getErrorMessage()}`);\n    }\n  } finally { /* PreparedStatement needs no close */ }\n}\nawait assertPrepareOk(cypher);\nawait executePrepared(cypher, params);","typeGuard":"function isPrepareFailed(err): boolean {\n  return err instanceof Error && err.message.startsWith('Prepare failed:');\n}","tryCatchPattern":"try {\n  return await executePrepared(cypher, params);\n} catch (err) {\n  if (err instanceof Error && err.message.startsWith('Prepare failed:')) {\n    // Compile-time query error — do NOT retry; fix the query/schema.\n    throw new Error(`Bad Cypher (prepare failed): ${err.message} [query: ${cypher}]`, { cause: err });\n  }\n  throw err;\n}","preventionTips":["Static-check Cypher label/property names against the schema before runtime.","Keep a query test suite that exercises each Cypher template against a fresh index.","After a schema change, re-validate stored queries before deploying them."],"tags":["ladybugdb","cypher","query","prepare","schema"],"backgroundTag":null,"analyzedSha":"d540b00184d71a896261ee02670da9a92d59d8f7","analyzedAt":"2026-08-12T19:50:25.132Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}