hcengineering/platform · error · Error

Failed to find connection

Error message

Failed to find connection

What it means

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.

Source

Thrown at services/telegram/pod-telegram/src/workspace.ts:309

      console.log('Signout', this.workspace, phone)
      await txOp.remove(integration)
    } else {
      console.log('Disable', this.workspace, phone)
      await txOp.update(integration, { disabled: true })
    }
  }

  // #endregion

  // #region Messages

  async sendMsg (msg: NewTelegramMessage): Promise<void> {
    const rec = await this.userStorage.findOne({ userId: msg.modifiedBy })
    if (rec === undefined || rec?.phone === undefined) return
    const client = this.clients.get(rec.phone)

    if (client === undefined) {
      throw Error('Failed to find connection')
    }

    const channel = this.channelsById.get(msg.attachedTo as Ref<Channel>)
    if (channel === undefined) return
    const target = normalizeValue(channel.value)

    const importRequired = await client.conn.isContactImportRequired(target)

    if (importRequired) {
      const contact = await this.client.findOne<PContact>(channel.attachedToClass, {
        _id: channel.attachedTo as Ref<PContact>
      })
      if (contact === undefined) {
        throw new Error("Couldn't find contact by id" + channel.attachedTo)
      }

      await client.conn.addContact({
        lastName: getLastName(contact.name),

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Ensure the user completes Telegram connection for their phone before they can send messages to linked channels
  2. Check this.clients.has(rec.phone) before dispatching and skip/queue the message instead of throwing
  3. Reconnect or re-register the client if it was dropped after a restart
  4. Clean up stale phone values in user records that no longer have clients

Example fix

// before
const client = this.clients.get(rec.phone)
if (client === undefined) throw Error('Failed to find connection')
// after
const client = this.clients.get(rec.phone)
if (client === undefined) {
  console.warn(`No Telegram client for ${rec.phone}; skipping msg`)
  return
}
Defensive patterns

Strategy: validation

Validate before calling

const rec = await userStorage.findOne({ userId: msg.modifiedBy })
if (rec?.phone !== undefined && !clients.has(rec.phone)) {
  console.warn(`No Telegram client for ${rec.phone}; skipping msg`)
  return
}

Try / catch

try {
  await workspace.sendMsg(msg)
} catch (err) {
  if (err.message === 'Failed to find connection') {
    queueForRetry(msg) // retry once the user's client reconnects
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of hcengineering/platform@63e28dc964 (2026-08-29). Data as JSON: /api/errors/86c03b4bcc4afcd0. Report an issue: GitHub.