{"record":{"id":"58655a8ab31f1330","repo":"Budibase/budibase","slug":"invalid-environment-query-58655a","errorCode":null,"errorMessage":"Invalid environment query","messagePattern":"Invalid environment query","errorType":"validation","errorClass":"HTTPError","httpStatus":400,"severity":"error","filePath":"packages/server/src/sdk/workspace/ai/agentLogs/shared.ts","lineNumber":297,"sourceCode":"  if (!Array.isArray(parsed)) {\n    throw new Error(\"Expected indexed request IDs to be an array\")\n  }\n  if (parsed.some(item => typeof item !== \"string\")) {\n    throw new Error(\"Expected indexed request IDs to contain only strings\")\n  }\n\n  return new Set(parsed)\n}\n\nexport function getWorkspaceDbForEnvironment(environment: AgentLogEnvironment) {\n  if (environment === \"development\") {\n    return context.getDevWorkspaceDB()\n  }\n  if (environment === \"production\") {\n    return context.getProdWorkspaceDB()\n  }\n\n  throw new HTTPError(\"Invalid environment query\", 400)\n}\n\nexport function getLiteLLMRequestUser(\n  data: LiteLLMRequestDetail | AgentLogSessionIndexDoc\n): string | undefined {\n  if (\"proxy_server_request\" in data || \"end_user\" in data || \"user\" in data) {\n    return data.proxy_server_request?.user || data.end_user || data.user\n  }\n  return undefined\n}\n\nexport function validateLiteLLMRequestOwnership(\n  agentId: string,\n  data: LiteLLMRequestDetail\n) {\n  if (getLiteLLMRequestUser(data) !== getExpectedEndUser(agentId)) {\n    throw new HTTPError(\"Agent log detail not found\", 404)\n  }","sourceCodeStart":279,"sourceCodeEnd":315,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/sdk/workspace/ai/agentLogs/shared.ts#L279-L315","documentation":"getWorkspaceDbForEnvironment selects the dev or prod workspace database for agent log queries based on an AgentLogEnvironment string. Only \"development\" and \"production\" are recognized; any other value throws HTTPError(\"Invalid environment query\", 400).","triggerScenarios":"Calling getWorkspaceDbForEnvironment (directly or via sessionSummaryDb / agent log endpoints) with an environment value other than \"development\" or \"production\", e.g. \"dev\", \"prod\", \"\", or undefined from an unparsed query parameter.","commonSituations":"A client passes ?environment=prod instead of production; the query param is missing so it arrives undefined; a wrapper adds a default like \"all\" that the function does not accept.","solutions":["Pass exactly \"development\" or \"production\" as the environment value","Normalize/validate the environment parameter before calling (map \"prod\"→\"production\")","Catch the HTTPError at the API boundary and return a clear 400 message listing allowed values"],"exampleFix":"// before\nconst db = getWorkspaceDbForEnvironment(req.query.environment) // \"prod\" → 400\n// after\nconst env = req.query.environment === \"prod\" ? \"production\" : req.query.environment\nconst db = getWorkspaceDbForEnvironment(env)","handlingStrategy":"validation","validationCode":"const AGENT_LOG_ENVIRONMENTS = [\"development\", \"production\"] as const\nfunction isValidAgentLogEnvironment(value: unknown): value is \"development\" | \"production\" {\n  return typeof value === \"string\" && (AGENT_LOG_ENVIRONMENTS as readonly string[]).includes(value)\n}\nif (!isValidAgentLogEnvironment(environment)) {\n  throw new HTTPError(\"environment must be 'development' or 'production'\", 400)\n}","typeGuard":"function isValidAgentLogEnvironment(value: unknown): value is \"development\" | \"production\" {\n  return value === \"development\" || value === \"production\"\n}","tryCatchPattern":"try {\n  const db = getWorkspaceDbForEnvironment(environment)\n} catch (err) {\n  if (err instanceof HTTPError && err.status === 400 && err.message === \"Invalid environment query\") {\n    // default to development or return a 400 with allowed values\n  }\n}","preventionTips":["Validate/normalize query params (map \"prod\"→\"production\") before calling","Never let the environment param be undefined; default it explicitly","Use the AgentLogEnvironment type so TS rejects invalid literals"],"tags":["http-400","input-validation","environment","agent-logs"],"backgroundTag":"invalid-parameter-value","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}