{"record":{"id":"86c03b4bcc4afcd0","repo":"hcengineering/platform","slug":"failed-to-find-connection","errorCode":null,"errorMessage":"Failed to find connection","messagePattern":"Failed to find connection","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"services/telegram/pod-telegram/src/workspace.ts","lineNumber":309,"sourceCode":"      console.log('Signout', this.workspace, phone)\n      await txOp.remove(integration)\n    } else {\n      console.log('Disable', this.workspace, phone)\n      await txOp.update(integration, { disabled: true })\n    }\n  }\n\n  // #endregion\n\n  // #region Messages\n\n  async sendMsg (msg: NewTelegramMessage): Promise<void> {\n    const rec = await this.userStorage.findOne({ userId: msg.modifiedBy })\n    if (rec === undefined || rec?.phone === undefined) return\n    const client = this.clients.get(rec.phone)\n\n    if (client === undefined) {\n      throw Error('Failed to find connection')\n    }\n\n    const channel = this.channelsById.get(msg.attachedTo as Ref<Channel>)\n    if (channel === undefined) return\n    const target = normalizeValue(channel.value)\n\n    const importRequired = await client.conn.isContactImportRequired(target)\n\n    if (importRequired) {\n      const contact = await this.client.findOne<PContact>(channel.attachedToClass, {\n        _id: channel.attachedTo as Ref<PContact>\n      })\n      if (contact === undefined) {\n        throw new Error(\"Couldn't find contact by id\" + channel.attachedTo)\n      }\n\n      await client.conn.addContact({\n        lastName: getLastName(contact.name),","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/hcengineering/platform/blob/63e28dc96483967b2fc21c881b3f1023c1de7718/services/telegram/pod-telegram/src/workspace.ts#L291-L327","documentation":"Workspace.sendMsg resolves the sending user's phone and then looks up a pre-connected Telegram client for that phone; if no client connection exists for the phone it throws 'Failed to find connection' instead of silently dropping the message.","triggerScenarios":"sendMsg is called (from txCreateDoc, res, or sendNewMsgs) for a message modified by a user whose phone has no entry in this.clients — the user never connected a Telegram account, or the client was disconnected.","commonSituations":"A user creates/messages in a channel linked to Telegram before linking their own Telegram account; clients map cleared after reconnect or restart; user record has a stale phone value.","solutions":["Ensure the user completes Telegram connection for their phone before they can send messages to linked channels","Check this.clients.has(rec.phone) before dispatching and skip/queue the message instead of throwing","Reconnect or re-register the client if it was dropped after a restart","Clean up stale phone values in user records that no longer have clients"],"exampleFix":"// before\nconst client = this.clients.get(rec.phone)\nif (client === undefined) throw Error('Failed to find connection')\n// after\nconst client = this.clients.get(rec.phone)\nif (client === undefined) {\n  console.warn(`No Telegram client for ${rec.phone}; skipping msg`)\n  return\n}","handlingStrategy":"validation","validationCode":"const rec = await userStorage.findOne({ userId: msg.modifiedBy })\nif (rec?.phone !== undefined && !clients.has(rec.phone)) {\n  console.warn(`No Telegram client for ${rec.phone}; skipping msg`)\n  return\n}","typeGuard":null,"tryCatchPattern":"try {\n  await workspace.sendMsg(msg)\n} catch (err) {\n  if (err.message === 'Failed to find connection') {\n    queueForRetry(msg) // retry once the user's client reconnects\n  }\n}","preventionTips":["Require users to link their Telegram account before messaging linked channels","Queue outgoing messages when no client is connected instead of throwing","Re-register clients after restarts and clean up stale phone records"],"tags":["telegram","connection","messaging"],"backgroundTag":"client-connection-missing","analyzedSha":"63e28dc96483967b2fc21c881b3f1023c1de7718","analyzedAt":"2026-08-29T15:21:27.377Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}