Budibase/budibase · error · HTTPError
Preview role not found
Error message
Preview role not found
What it means
Preview chat allows impersonating a role via body.previewRoleId. The server fetches that role with roles.getRole; if the role doesn't exist (deleted or wrong workspace) the lookup yields no _id and the server throws HTTP 400 'Preview role not found'.
Source
Thrown at packages/server/src/api/controllers/ai/chatConversations.ts:402
)
if (chat.isPreview !== true) {
throw new HTTPError("Preview mode is required", 400)
}
if (!isBuilderOrAdmin) {
throw new HTTPError("Forbidden", 403)
}
if (!isDevWorkspaceID(workspaceId)) {
throw new HTTPError("Preview mode requires a development workspace", 400)
}
let user = ctx.user
if (chat.previewRoleId) {
const previewRole = await roles.getRole(chat.previewRoleId)
if (!previewRole?._id) {
throw new HTTPError("Preview role not found", 400)
}
user = {
...ctx.user,
roleId: previewRole._id,
}
}
const agentId = chat.agentId
if (!agentId) {
throw new HTTPError("agentId is required", 400)
}
return {
agentId,
chat,
userId,
user,
}View on GitHub (pinned to a81a902e9a)
Solutions
- Fetch the current role list for the workspace and use a valid _id for previewRoleId
- Omit previewRoleId to chat under the caller's own role
- Clear cached client state after role changes
Example fix
// before
{ "previewRoleId": "role_deleted_123", ... }
// after
{ "previewRoleId": null, ... } // or a valid role _id Defensive patterns
Strategy: validation
Validate before calling
const role = await fetchRole(appId, previewRoleId)
if (!role?._id) throw new Error("previewRoleId does not exist in this workspace") Type guard
const isValidRole = (r: { _id?: string } | null | undefined): r is { _id: string } =>
typeof r?._id === "string" && r._id.length > 0 Try / catch
try {
await streamChat(body)
} catch (e) {
if (e.status === 400 && String(e.message).includes("Preview role not found")) {
// drop previewRoleId or pick a valid role, then retry
}
} Prevention
- Refresh cached role ids whenever roles are edited
- Validate previewRoleId against the workspace role list before sending
- Omit previewRoleId when role impersonation isn't needed
When it happens
Trigger: POST chat stream with previewRoleId referencing a role id that is absent in the current dev workspace — e.g. a role copied from another app or deleted after the body was cached.
Common situations: Stale previewRoleId cached in the client after roles changed; copying request payloads between apps with different role ids; typos in role ids.
Related errors
- Invalid bookmark query
- Invalid limit query
- Limit query must be between 1 and 100
- Invalid ${queryName} query
- Invalid environment query
AI-assisted analysis of Budibase/budibase@a81a902e9a (2026-08-29).
Data as JSON: /api/errors/8a1ffb4f680a281e.
Report an issue: GitHub.