{"record":{"id":"da622c2374cdcc39","repo":"mastra-ai/mastra","slug":"could-not-create-the-session-message-try-agai","errorCode":null,"errorMessage":"Could not create the session: ${message}. Try again.","messagePattern":"Could not create the session: (.+?)\\. Try again\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"mastracode/factory-ui/src/ui/domains/chat/hooks/useCreateUserSessionFromDraft.ts","lineNumber":50,"sourceCode":"      // Activating the pack applies its models server-side, so only hand off a\n      // model when the draft explicitly deviated from the selected pack.\n      const modeKey =\n        activeModeId === 'build' || activeModeId === 'plan' || activeModeId === 'fast' ? activeModeId : undefined;\n      const packModelId =\n        draftModelPackId && modeKey\n          ? modelPacks.find(pack => pack.id === draftModelPackId)?.models[modeKey]\n          : undefined;\n      const handoffModelId = activeModelId === packModelId ? undefined : activeModelId;\n\n      try {\n        const session = await createUserSession(baseUrl, projectRepositoryId, {\n          sessionId: draftSessionId,\n          title: prompt,\n        });\n        return { session, prompt, factoryId, projectRepositoryId, activeModeId, handoffModelId, draftModelPackId };\n      } catch (error) {\n        const message = error instanceof Error ? error.message : 'Session creation failed';\n        throw new Error(`Could not create the session: ${message}. Try again.`, { cause: error });\n      }\n    },\n    onSuccess: ({\n      session,\n      prompt,\n      factoryId,\n      projectRepositoryId,\n      activeModeId,\n      handoffModelId,\n      draftModelPackId,\n    }) => {\n      queryClient.setQueryData(queryKeys.userSession(session.sessionId), session);\n      addCachedSession(queryClient, projectRepositoryId, session);\n      queryClient.setQueryData<MastraDBMessage[]>(\n        queryKeys.agentControllerThreadMessages(\n          AGENT_CONTROLLER_ID,\n          session.sessionId,\n          session.sessionId,","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/mastra-ai/mastra/blob/75dd419e613fe9c39f846ffc500716141b74fda6/mastracode/factory-ui/src/ui/domains/chat/hooks/useCreateUserSessionFromDraft.ts#L32-L68","documentation":"Wrapping error thrown in createDraftSessionMutation's catch: any failure from the underlying session-creation call is re-thrown as 'Could not create the session: <cause message>. Try again.' with the original attached as cause. It exists to give users a single actionable message while preserving the root-cause error for developers.","triggerScenarios":"Any exception inside the mutation body after the readiness checks — server 4xx/5xx from the create-session request, network failure, or a thrown error from activating the pack / creating the session server-side.","commonSituations":"Expired or missing auth cookie causing 401 on session create; server rejecting a handoffModelId not allowed by policy; transient network drop mid-request; draft session already consumed.","solutions":["Read error.cause to see the underlying message and fix that root issue first.","Retry the mutation as the message suggests; if it persists, check server logs for the create-session endpoint status.","Confirm the user session is authenticated (cookies/credentials: include) and the factoryId/projectRepositoryId are valid.","Verify handoffModelId (when set) is permitted by the active model pack/policy."],"exampleFix":"// before\ntry {\n  session = await client.createSession({ ... });\n} catch (error) {\n  const message = error instanceof Error ? error.message : 'Session creation failed';\n  throw new Error(`Could not create the session: ${message}. Try again.`, { cause: error });\n}\n// after\ntry {\n  session = await client.createSession({ ... });\n} catch (error) {\n  console.error('session create root cause:', error); // inspect cause server/client side\n  throw new Error(`Could not create the session: ${error instanceof Error ? error.message : 'Session creation failed'}. Try again.`, { cause: error });\n}","handlingStrategy":"try-catch","validationCode":"if (!draftSessionId || !factoryId || !factorySessionState?.projectRepositoryId) throw new Error('Could not create the session. Reload the page and try again.');","typeGuard":"function hasCause(e: unknown): e is Error & { cause: unknown } {\n  return e instanceof Error && 'cause' in e;\n}","tryCatchPattern":"try {\n  await createDraftSessionMutation.mutateAsync(prompt);\n} catch (error) {\n  const root = (error as { cause?: unknown })?.cause;\n  showToast(`Could not create the session: ${root instanceof Error ? root.message : String(error)}. Try again.`);\n  // optionally inspect root for status-specific handling (401 -> re-login)\n}","preventionTips":["Always log error.cause to find the root failure.","Ensure the user session is authenticated before entering the composer.","Validate handoffModelId against the active model policy before submitting."],"tags":["react-query","mutation","error-wrapping","network"],"backgroundTag":"request-failed-wrapped","analyzedSha":"75dd419e613fe9c39f846ffc500716141b74fda6","analyzedAt":"2026-08-30T00:15:31.844Z","schemaVersion":2},"datasetVersion":"2026-08-30T03:17:51.788Z"}