{"record":{"id":"be2b0f303a24242a","repo":"Mintplex-Labs/anything-llm","slug":"system-prompt-variable-not-found","errorCode":null,"errorMessage":"System prompt variable not found","messagePattern":"System prompt variable not found","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"server/models/systemPromptVariables.js","lineNumber":203,"sourceCode":"        description: description ? String(description) : null,\n        type: type ? String(type) : \"static\",\n        userId: userId ? Number(userId) : null,\n      },\n    });\n  },\n\n  /**\n   * Updates a system prompt variable by its unique database ID\n   * @param {number} id\n   * @param {{ key: string, value: string, description: string }} data\n   * @returns {Promise<SystemPromptVariable>}\n   */\n  update: async function (id, { key, value, description = null }) {\n    if (!id || !key || !value) return null;\n    const existingRecord = await prisma.system_prompt_variables.findFirst({\n      where: { id: Number(id) },\n    });\n    if (!existingRecord) throw new Error(\"System prompt variable not found\");\n    await this._checkVariableKey(key, false);\n\n    return await prisma.system_prompt_variables.update({\n      where: { id: existingRecord.id },\n      data: {\n        key: String(key),\n        value: String(value),\n        description: description ? String(description) : null,\n      },\n    });\n  },\n\n  /**\n   * Deletes a system prompt variable by its unique database ID\n   * @param {number} id\n   * @returns {Promise<boolean>}\n   */\n  delete: async function (id = null) {","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/models/systemPromptVariables.js#L185-L221","documentation":"Thrown by SystemPromptVariables.update(id, data) when prisma.system_prompt_variables.findFirst({ where: { id } }) returns null — the row being updated does not exist. Note the function returns null silently (no throw) if id, key, or value are missing, so reaching this throw means those were present but the id has no matching row.","triggerScenarios":"Updating a variable that was deleted, using an id from a stale list, or passing an id that failed Number() coercion to a non-existent row. Concurrent deletion between a list-and-edit UI flow.","commonSituations":"Admin UI held a stale variable list open while another admin/session deleted the row; the id was extracted from a URL and truncated; a test fixture was cleaned up mid-run.","solutions":["Re-fetch the variable list to confirm the id still exists before editing.","Verify id is a valid integer and matches an existing row.","Handle the not-found case in the UI with a clear 'variable no longer exists' message.","Use upsert or check existence then update to avoid race-driven 404s."],"exampleFix":"// before\nawait SystemPromptVariables.update(req.params.id, req.body);\n// after\nconst existing = await SystemPromptVariables.getById(req.params.id);\nif (!existing) return res.status(404).json({ error: 'Variable was deleted' });\nawait SystemPromptVariables.update(req.params.id, req.body);","handlingStrategy":"validation","validationCode":"const existing = await prisma.system_prompt_variables.findFirst({ where: { id: Number(id) } });\nif (!existing) return { status: 404, body: { error: 'Variable not found' } };","typeGuard":null,"tryCatchPattern":"try {\n  await SystemPromptVariables.update(id, data);\n} catch (e) {\n  if (e.message === 'System prompt variable not found') return res.status(404).json({ error: e.message });\n  throw e;\n}","preventionTips":["Re-fetch the variable list before editing to avoid stale ids.","Return a clear not-found response instead of propagating the throw.","Use existence checks to handle concurrent deletions gracefully."],"tags":["system-prompt-variables","not-found","database","crud"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}