calcom/cal.diy · error · NotFoundException
User not found
Error message
User not found
What it means
Thrown by CalendarsService.getCalendars (calendars.service.ts:67) as NotFoundException (HTTP 404) when usersRepository.findByIdWithCalendars(userId) returns null — no User row exists for the given id. The auth guard usually guarantees a live user, so this points to a deleted user, a stale token, or an internal caller passing a wrong identifier.
Source
Thrown at apps/api/v2/src/platform/calendars/services/calendars.service.ts:67
delegatedToId: null,
delegationCredentialId: null,
}))
.filter((credential) => !!credential);
}
async getCalendars(
userId: number,
ensureDefaultSelectedCalendars = false
): Promise<ConnectedDestinationCalendars> {
const cachedResult = await this.calendarsCacheService.getConnectedAndDestinationCalendarsCache(userId);
if (cachedResult && !ensureDefaultSelectedCalendars) {
return cachedResult;
}
const userWithCalendars = await this.usersRepository.findByIdWithCalendars(userId);
if (!userWithCalendars) {
throw new NotFoundException("User not found");
}
const result = await getConnectedDestinationCalendarsAndEnsureDefaultsInDb({
user: {
...userWithCalendars,
allSelectedCalendars: userWithCalendars.selectedCalendars,
userLevelSelectedCalendars: userWithCalendars.selectedCalendars.filter(
(calendar) => !calendar.eventTypeId
),
},
onboarding: ensureDefaultSelectedCalendars,
eventTypeId: null,
prisma: this.dbWrite.prisma as unknown as PrismaClient,
});
await this.calendarsCacheService.setConnectedAndDestinationCalendarsCache(userId, result);
return result;
}
View on GitHub (pinned to 176037d0af)
Solutions
- Confirm the user exists: SELECT id FROM users WHERE id=<id>.
- Check the caller is passing the authenticated user's id, not 0/null/a teamId.
- Re-seed test data or recreate the user if intentionally present.
Example fix
// before: internal call with an unverified id
await calendarsService.getCalendars(maybeUserId); // 404 if null
// after: guard at the boundary
if (!maybeUserId || !(await usersRepo.exists(maybeUserId))) {
throw new Error(`refusing to call getCalendars for unknown user ${maybeUserId}`);
}
await calendarsService.getCalendars(maybeUserId); Defensive patterns
Strategy: validation
Validate before calling
// Before calling getCalendars with an internal userId, confirm the user exists
const user = await usersRepository.findByIdWithCalendars(userId);
if (!user) throw new NotFoundError(`User ${userId} does not exist`); Type guard
function isKnownUserId(id) {
return Number.isInteger(id) && id > 0;
} Try / catch
try {
await calendarsService.getCalendars(userId);
} catch (e) {
if (e instanceof NotFoundException && /user not found/i.test(e.message)) {
// user was deleted or the id is wrong — do not retry; surface to auth layer
throw new AuthorizationError('User does not exist');
}
throw e;
} Prevention
- Never pass 0/null/undefined as userId to internal services.
- Invalidate access tokens promptly when a user is deleted.
- Validate that internal callers receive userId from the authenticated principal, not from request params.
When it happens
Trigger: User was deleted but their access token is still accepted briefly; an internal service call passes 0/undefined/a teamId as userId; test fixture created credentials but not the User row.
Common situations: Post-deletion race window; cross-service call mis-mapping userId; seeded test data missing the user; misrouted request after an account merge.
Related errors
- ApiAuthStrategy - access token - User associated with the ac
- ApiAuthStrategy - next auth - User associated with the authe
- ApiAuthStrategy - third-party token - No user or team owner
- NextAuthStrategy - User associated with the authentication t
- User with username ${body.username} not found
AI-assisted analysis of calcom/cal.diy@176037d0af (2026-08-12).
Data as JSON: /api/errors/e737006ef3d6ede6.
Report an issue: GitHub.