{"record":{"id":"c1519c517ac7da39","repo":"different-ai/openwork","slug":"organization-not-found","errorCode":"organization_not_found","errorMessage":"Organization not found for connector instance.","messagePattern":"Organization not found for connector instance\\.","errorType":"http","errorClass":"PluginArchRouteFailure","httpStatus":404,"severity":"error","filePath":"ee/apps/den-api/src/routes/org/plugin-system/store.ts","lineNumber":5857,"sourceCode":"    branch,\n    connectorAccount,\n    connectorInstance,\n    connectorTarget,\n    installationId,\n    ref,\n    repositoryFullName,\n  }\n}\n\nasync function buildConnectorAutomationContext(input: { connectorInstance: ConnectorInstanceRow }) {\n  const organizationRows = await db\n    .select()\n    .from(OrganizationTable)\n    .where(eq(OrganizationTable.id, input.connectorInstance.organizationId))\n    .limit(1)\n  const organization = organizationRows[0] as OrganizationRow | undefined\n  if (!organization) {\n    throw new PluginArchRouteFailure(404, \"organization_not_found\", \"Organization not found for connector instance.\")\n  }\n\n  const memberRows = await db\n    .select()\n    .from(MemberTable)\n    .where(and(\n      eq(MemberTable.organizationId, input.connectorInstance.organizationId),\n      eq(MemberTable.id, input.connectorInstance.createdByOrgMembershipId),\n      isNull(MemberTable.removedAt),\n    ))\n    .limit(1)\n  const member = memberRows[0] as MemberRow | undefined\n  if (!member) {\n    throw new PluginArchRouteFailure(404, \"member_not_found\", \"Connector creator member not found.\")\n  }\n\n  if (!member.userId) {\n    throw new PluginArchRouteFailure(404, \"member_not_joined\", \"Connector creator member has not joined the organization.\")","sourceCodeStart":5839,"sourceCodeEnd":5875,"githubUrl":"https://github.com/different-ai/openwork/blob/2b7df46e8ae1517d64c896c7793d2d52ec845669/ee/apps/den-api/src/routes/org/plugin-system/store.ts#L5839-L5875","documentation":"Thrown in the connector-instance resolution path of the plugin-system store when the OrganizationTable lookup for `input.connectorInstance.organizationId` returns no rows. The connector instance record references an organization id that no longer exists in the database. This is a 404 PluginArchRouteFailure raised before member/session resolution proceeds.","triggerScenarios":"Resolving a connector instance (e.g. GitHub connector automation/session resolution) whose `organizationId` points to a deleted or nonexistent row in the OrganizationTable; querying with a stale connectorInstance payload from another environment or database snapshot.","commonSituations":"Organization was hard-deleted (or cleaned up by a test fixture) while connector instance rows were left orphaned; cross-environment data restore where instances were copied without their parent orgs; hand-crafted API calls referencing a fabricated connector instance id.","solutions":["Verify the connector instance's organizationId exists: SELECT * FROM organization WHERE id = '<orgId>'.","Delete or re-parent the orphaned connector instance row, or restore the missing organization.","If triggered by a test, ensure fixtures create the organization before creating connector instances and clean up in dependency order."],"exampleFix":"// before\nconst instance = await getConnectorInstance(instanceId) // row exists, org deleted\nawait resolveConnectorSession(instance) // 404 organization_not_found\n// after\nconst org = await db.select().from(OrganizationTable).where(eq(OrganizationTable.id, instance.organizationId)).limit(1)\nif (!org[0]) await deleteConnectorInstance(instance.id) // clean orphan before resolving\nawait resolveConnectorSession(instance)","handlingStrategy":"validation","validationCode":"const org = await db.select().from(OrganizationTable).where(eq(OrganizationTable.id, instance.organizationId)).limit(1)\nif (!org[0]) throw new Error(`Org ${instance.organizationId} missing; clean up connector instance ${instance.id} first`)","typeGuard":"function orgExists(row: OrganizationRow | undefined): row is OrganizationRow { return row !== undefined }","tryCatchPattern":"try {\n  await resolveConnectorInstance(instanceId)\n} catch (e) {\n  if (e instanceof PluginArchRouteFailure && e.code === 'organization_not_found') {\n    await deleteConnectorInstance(instanceId) // remove orphan\n  } else throw e\n}","preventionTips":["Cascade-delete connector instances when organizations are removed","Restore data with parent orgs intact, in dependency order","Periodically scan for connector instances whose organizationId has no matching organization"],"tags":["database","orphaned-record","connector"],"backgroundTag":"orphaned-foreign-key-record","analyzedSha":"2b7df46e8ae1517d64c896c7793d2d52ec845669","analyzedAt":"2026-09-01T07:59:23.713Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}