hcengineering/platform · error · Error

Invalid workspace: '${user.workspace}'

Error message

Invalid workspace: '${user.workspace}'

What it means

thrown by removeUser when getTarget() fails to resolve a worker for the user's workspace, meaning the workspace name on the User record does not match any registered workspace worker. It guards against calling removeUser on a worker that does not exist.

Source

Thrown at services/telegram/pod-telegram/src/platform.ts:64

    await wsWorker.addUser(tgUser)
  }

  async getTarget ({ workspace, email }: User): Promise<[UserRecord, WorkspaceWorker | undefined]> {
    const res = await this.storage.findOne({ email, workspace })

    if (res === null) {
      throw Error('User is not signed in')
    }

    return [res, this.clientMap.get(workspace)]
  }

  async removeUser (user: User): Promise<void> {
    const [res, wsWorker] = await this.getTarget(user)

    if (wsWorker === undefined) {
      throw Error(`Invalid workspace: '${user.workspace}'`)
    }

    await wsWorker.removeUser({ phone: res.phone })
  }

  async getUserRecord ({ workspace, phone }: Pick<TgUser, 'workspace' | 'phone'>): Promise<UserRecord | undefined> {
    return (await this.storage.findOne({ phone, workspace })) ?? undefined
  }

  static async createStorages (): Promise<
  [Collection<UserRecord>, Collection<LastMsgRecord>, Collection<WorkspaceChannel>]
  > {
    const db = await getDB()
    const userStorage = db.collection<UserRecord>('integrations')
    const lastMsgStorage = db.collection<LastMsgRecord>('last-msgs')
    const channelStorage = db.collection<WorkspaceChannel>('channels')

    await userStorage.createIndex({ phone: 1, workspace: 1 }, { unique: true })

View on GitHub (pinned to 63e28dc964)

Solutions

  1. Verify user.workspace matches an existing registered workspace before calling removeUser
  2. Look up the workspace list and correct or migrate the user record's workspace field
  3. If the workspace was intentionally deleted, clean up or reassign the stale user records
  4. Add a pre-check that resolves the workspace and returns a friendly error to the caller

Example fix

// before
await pod.removeUser(user) // throws if workspace unknown
// after
const ws = workspaces.find(w => w.name === user.workspace)
if (ws === undefined) throw new Error(`Unknown workspace: ${user.workspace}`)
await pod.removeUser(user)
Defensive patterns

Strategy: validation

Validate before calling

const known = new Set(workspaces.map(w => w.name))
if (!known.has(user.workspace)) throw new Error(`Unknown workspace: ${user.workspace}`)

Type guard

function hasWorkspace<T extends { workspace: string }>(user: T, known: Set<string>): boolean {
  return known.has(user.workspace)
}

Try / catch

try {
  await pod.removeUser(user)
} catch (err) {
  if (err.message.startsWith('Invalid workspace:')) {
    console.warn(`Skipping user in unknown workspace ${user.workspace}`)
    return
  }
  throw err
}

Prevention

When it happens

Trigger: removeUser(user) is called with a User whose user.workspace string is not a known/registered workspace (typo, deleted workspace, or user record from a different environment).

Common situations: Workspaces renamed or removed while user records still reference the old name; cross-environment data (staging user in production); typo in workspace identifier.

Related errors


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