{"record":{"id":"dd127ffb058b6fea","repo":"Budibase/budibase","slug":"invalid-environment-query","errorCode":null,"errorMessage":"Invalid environment query","messagePattern":"Invalid environment query","errorType":"http","errorClass":"HTTPError","httpStatus":400,"severity":"error","filePath":"packages/server/src/api/controllers/ai/agentLogs.ts","lineNumber":89,"sourceCode":"\n  const parsedDate = new Date(normalizedValue)\n  if (!Number.isFinite(parsedDate.getTime())) {\n    throw new HTTPError(`Invalid ${queryName} query`, 400)\n  }\n\n  return parsedDate.toISOString()\n}\n\nfunction sanitizeEnvironmentQuery(environment?: string): AgentLogEnvironment {\n  const normalizedEnvironment = environment?.trim()\n  if (\n    normalizedEnvironment === \"development\" ||\n    normalizedEnvironment === \"production\"\n  ) {\n    return normalizedEnvironment\n  }\n\n  throw new HTTPError(\"Invalid environment query\", 400)\n}\n\nfunction getComparableDate(value: string, mode: \"start\" | \"end\"): number {\n  const comparableValue = DATE_ONLY_REGEX.test(value)\n    ? `${value}T${mode === \"start\" ? \"00:00:00.000\" : \"23:59:59.999\"}Z`\n    : value\n\n  return new Date(comparableValue).getTime()\n}\n\nexport async function fetchAgentLogs(\n  ctx: UserCtx<void, FetchAgentLogsResponse>\n) {\n  const { agentId } = ctx.params\n  const { startDate, endDate, bookmark, limit, statusFilter, triggerFilter } =\n    ctx.query as Record<string, string>\n  const defaults = getDefaultLogRange()\n  const sanitizedStartDate =","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/Budibase/budibase/blob/a81a902e9a8fe55b467d106765f6638f12e35c49/packages/server/src/api/controllers/ai/agentLogs.ts#L71-L107","documentation":"This error is thrown by the agent logs controller when the `environment` query parameter supplied to the agent log session endpoints is not one of the accepted values. The sanitizeEnvironmentQuery helper normalizes the input and only accepts 'development' or 'production'; anything else is rejected with a 400 HTTP error.","triggerScenarios":"Calling GET on the agent logs endpoints (via fetchAgentLogSession) with ?environment=staging, ?environment=dev, ?environment=Production (uppercase is rejected if normalization does not lowercase first) or any other string not exactly matching 'development' or 'production' after normalization.","commonSituations":"CI scripts passing a branch or stage name (e.g. 'staging') as environment; frontend code mapping an app environment selector that includes more values than the API supports; typos like 'prod' or 'production ' with trailing whitespace that survives normalization.","solutions":["Change the query to use exactly 'development' or 'production'","Remove the environment query parameter entirely if the endpoint allows a default","Check for typos and trim whitespace in the caller","Update calling code that passes environment names from a broader enum (e.g. staging) to map them onto the two supported values"],"exampleFix":"// before\nGET /api/ai/agents/123/logs?environment=staging\n// after\nGET /api/ai/agents/123/logs?environment=development","handlingStrategy":"validation","validationCode":"const ENVIRONMENTS = [\"development\", \"production\"]\nfunction validateEnvironment(env) {\n  if (env !== undefined && !ENVIRONMENTS.includes(env)) {\n    throw new Error(`environment must be one of ${ENVIRONMENTS.join(\" or \")}`)\n  }\n  return env\n}\nvalidateEnvironment(searchParams.get(\"environment\"))","typeGuard":"function isEnvironment(v) {\n  return v === \"development\" || v === \"production\"\n}","tryCatchPattern":"try {\n  const logs = await fetchAgentLogSession(agentId, session, { environment: env })\n} catch (err) {\n  if (err.status === 400 && err.message === \"Invalid environment query\") {\n    throw new Error(`\"${env}\" is not supported; use \"development\" or \"production\"`)\n  }\n  throw err\n}","preventionTips":["Constrain UI environment selectors to exactly development/production","Trim and lowercase user input before sending","Omit the environment param when you do not need to filter","Share a single constants module for allowed environments between client and server"],"tags":["validation","http-400","query-params","ai-agents"],"backgroundTag":"invalid-query-parameter","analyzedSha":"a81a902e9a8fe55b467d106765f6638f12e35c49","analyzedAt":"2026-08-29T01:03:10.972Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}