{"record":{"id":"f9ed6c291576250c","repo":"windmill-labs/windmill","slug":"error-setting-flow-user-state-at-key-e-body-f9ed6c","errorCode":null,"errorMessage":"Error setting flow user state at ${key}: ${e.body}","messagePattern":"Error setting flow user state at (.+?): (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"typescript-client/client.ts","lineNumber":719,"sourceCode":"export async function setFlowUserState(\n  key: string,\n  value: any,\n  errorIfNotPossible?: boolean\n): Promise<void> {\n  if (value === undefined) {\n    value = null;\n  }\n  const workspace = getWorkspace();\n  try {\n    await JobService.setFlowUserState({\n      workspace,\n      id: await getRootJobId(),\n      key,\n      requestBody: value,\n    });\n  } catch (e: any) {\n    if (errorIfNotPossible) {\n      throw Error(`Error setting flow user state at ${key}: ${e.body}`);\n    } else {\n      console.error(`Error setting flow user state at ${key}: ${e.body}`);\n    }\n  }\n}\n\n/**\n * Get a flow user state\n * @param path path of the variable\n\n */\nexport async function getFlowUserState(\n  key: string,\n  errorIfNotPossible?: boolean\n): Promise<any> {\n  const workspace = getWorkspace();\n  try {\n    return await JobService.getFlowUserState({","sourceCodeStart":701,"sourceCodeEnd":737,"githubUrl":"https://github.com/windmill-labs/windmill/blob/e474e8803ce2ff5c2df09a58dab51d45f5c922ca/typescript-client/client.ts#L701-L737","documentation":"setFlowUserState() wraps JobService.setFlowUserState, which writes a key/value into the user state of the root flow job. Any API failure (non-flow context, job not found, permissions, network) is caught and, only if errorIfNotPossible is true, rethrown with this message including the API error body; otherwise it is just logged. The original error object is discarded, so only e.body is visible.","triggerScenarios":"Calling setFlowUserState(key, value, true) when the code is not running inside a flow (getRootJobId has no root job), the flow job already finished/doesn't exist, the key/value is rejected by the API, or a transient network/auth failure occurs.","commonSituations":"Using flow user state in a standalone script (not a flow step); writing state after the flow run completed or was cancelled; worker connectivity problems; calling it from tests with a mocked/absent job id.","solutions":["Only call setFlowUserState from within a flow execution, or pass errorIfNotPossible=false to tolerate the failure.","Verify the flow job is still running when the call happens (state can't be set after completion).","Read the appended e.body in the message — it carries the server's actual reason (404/403/validation).","Check workspace and token permissions for JobService.setFlowUserState on the root job.","If thrown persistently due to network, add retry logic around the call."],"exampleFix":"// before (throws and aborts the step)\nawait setFlowUserState('progress', 42, true);\n// after\ntry {\n  await setFlowUserState('progress', 42, true);\n} catch (e) {\n  console.error('flow user state unavailable, continuing', e);\n}","handlingStrategy":"try-catch","validationCode":"const rootJobId = getEnv('WM_FLOW_JOB_ID');\nif (!rootJobId) {\n  console.warn('Not in a flow context; setFlowUserState will fail');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await setFlowUserState(key, value, true);\n} catch (e) {\n  // e.message embeds the API e.body; decide whether the step can proceed without state\n  console.error(`Could not set flow user state '${key}':`, e.message);\n}","preventionTips":["Call flow user state APIs only from within flow steps, never standalone scripts.","Omit errorIfNotPossible (or pass false) when state persistence is best-effort.","Set state while the flow is still running; don't write after completion steps.","Log and inspect the appended e.body to distinguish 404 (not a flow) from 403 (permissions)."],"tags":["windmill","flow-state","api","error-handling"],"backgroundTag":"api-request-failed","analyzedSha":"e474e8803ce2ff5c2df09a58dab51d45f5c922ca","analyzedAt":"2026-09-03T12:38:19.024Z","contentChangedAt":"2026-09-03T12:38:19.024Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}