RocketChat/Rocket.Chat · error · Error

error-invalid-user

Error message

error-invalid-user

What it means

Thrown by setSLAToInquiry when the user referenced by userId cannot be found or has no username. SLA assignment records the acting user (needs _id, name, username); a user without a username cannot be attributed. Plain Error, code 'error-invalid-user'.

Source

Thrown at apps/meteor/ee/server/api/v1/omnichannel/lib/inquiries.ts:18

import { LivechatInquiry, Users, OmnichannelServiceLevelAgreements } from '@rocket.chat/models';

import { updateRoomSLA } from './sla';

export async function setSLAToInquiry({ userId, roomId, sla }: { userId: string; roomId: string; sla?: string }): Promise<void> {
	const inquiry = await LivechatInquiry.findOneByRoomId(roomId, { projection: { status: 1 } });
	if (!inquiry || inquiry.status !== 'queued') {
		throw new Error('error-invalid-inquiry');
	}

	const slaData = sla && (await OmnichannelServiceLevelAgreements.findOneByIdOrName(sla));
	if (!slaData) {
		throw new Error('error-invalid-sla');
	}

	const user = await Users.findOneById(userId, { projection: { _id: 1, username: 1, name: 1 } });
	if (!user?.username) {
		throw new Error('error-invalid-user');
	}

	await updateRoomSLA(
		roomId,
		{
			_id: user._id,
			name: user.name || '',
			username: user.username,
		},
		slaData,
	);
}

View on GitHub (pinned to f9d3ec372b)

Solutions

  1. Confirm the user exists and has a username via Users.findOneById before calling.
  2. Backfill a username on the acting service account.
  3. Use an actual agent userId (with a username) as the actor for SLA assignment.
  4. Validate userId is a non-empty string matching an existing user.

Example fix

// before: system actor without username
await setSLAToInquiry({ userId: systemAccountId, roomId, sla });

// after: ensure the actor has a username
const actor = await Users.findOneById(systemAccountId, { projection: { username: 1 } });
if (!actor?.username) throw new Error('Actor must have a username');
await setSLAToInquiry({ userId: actor._id, roomId, sla });
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the acting user has a username
import { Users } from '@rocket.chat/models';
const actor = await Users.findOneById(userId, { projection: { username: 1 } });
if (!actor?.username) {
  throw new Error('Actor user must exist and have a username');
}
await setSLAToInquiry({ userId: actor._id, roomId, sla });

Type guard

function hasUsername(u: { username?: string } | null | undefined): u is { username: string } {
  return !!u && typeof u.username === 'string' && u.username.length > 0;
}

Try / catch

try {
  await setSLAToInquiry({ userId, roomId, sla });
} catch (e) {
  if (e.message === 'error-invalid-user') {
    // backfill username or switch to a real agent actor
    return;
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling setSLAToInquiry with a userId that does not exist in Users, or whose document has no username field; calling with a system/bot user that was created without a username; userId from a deleted account.

Common situations: Bot/service account created without a username; user deleted but userId still referenced in a queue job; cross-DB inconsistency; userId passed as the wrong type.

Related errors


AI-assisted analysis of RocketChat/Rocket.Chat@f9d3ec372b (2026-08-12). Data as JSON: /api/errors/0526d67a8fddddef. Report an issue: GitHub.