Budibase/budibase · error · HTTPError
Invalid environment query
Error message
Invalid environment query
What it means
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).
Source
Thrown at packages/server/src/sdk/workspace/ai/agentLogs/shared.ts:297
if (!Array.isArray(parsed)) {
throw new Error("Expected indexed request IDs to be an array")
}
if (parsed.some(item => typeof item !== "string")) {
throw new Error("Expected indexed request IDs to contain only strings")
}
return new Set(parsed)
}
export function getWorkspaceDbForEnvironment(environment: AgentLogEnvironment) {
if (environment === "development") {
return context.getDevWorkspaceDB()
}
if (environment === "production") {
return context.getProdWorkspaceDB()
}
throw new HTTPError("Invalid environment query", 400)
}
export function getLiteLLMRequestUser(
data: LiteLLMRequestDetail | AgentLogSessionIndexDoc
): string | undefined {
if ("proxy_server_request" in data || "end_user" in data || "user" in data) {
return data.proxy_server_request?.user || data.end_user || data.user
}
return undefined
}
export function validateLiteLLMRequestOwnership(
agentId: string,
data: LiteLLMRequestDetail
) {
if (getLiteLLMRequestUser(data) !== getExpectedEndUser(agentId)) {
throw new HTTPError("Agent log detail not found", 404)
}View on GitHub (pinned to a81a902e9a)
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
Example fix
// before const db = getWorkspaceDbForEnvironment(req.query.environment) // "prod" → 400 // after const env = req.query.environment === "prod" ? "production" : req.query.environment const db = getWorkspaceDbForEnvironment(env)
Defensive patterns
Strategy: validation
Validate before calling
const AGENT_LOG_ENVIRONMENTS = ["development", "production"] as const
function isValidAgentLogEnvironment(value: unknown): value is "development" | "production" {
return typeof value === "string" && (AGENT_LOG_ENVIRONMENTS as readonly string[]).includes(value)
}
if (!isValidAgentLogEnvironment(environment)) {
throw new HTTPError("environment must be 'development' or 'production'", 400)
} Type guard
function isValidAgentLogEnvironment(value: unknown): value is "development" | "production" {
return value === "development" || value === "production"
} Try / catch
try {
const db = getWorkspaceDbForEnvironment(environment)
} catch (err) {
if (err instanceof HTTPError && err.status === 400 && err.message === "Invalid environment query") {
// default to development or return a 400 with allowed values
}
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Bookmark query exceeds maximum scan window of ${MAX_SESSION_
- Template name is required
- Template description is required
- Invalid cursor
- Invalid ${label}: ${value}
AI-assisted analysis of Budibase/budibase@a81a902e9a (2026-08-29).
Data as JSON: /api/errors/58655a8ab31f1330.
Report an issue: GitHub.