mastra-ai/mastra · error · Error
Cannot favorite: ${entityType} with id ${entityId} does not
Error message
Cannot favorite: ${entityType} with id ${entityId} does not exist What it means
This error is thrown by the in-memory favorites storage's `requireEntity` helper when an agent or skill being favorited does not exist in the store. The library enforces that favorites can only reference real entities, so `favorite`/`unfavorite` operations call `requireEntity` first and abort with this message if the lookup map returns nothing. It prevents orphaned favorite records pointing at nonexistent agents or skills.
Source
Thrown at packages/core/src/storage/domains/favorites/inmemory.ts:147
if (entity && entity.favoriteCount) {
entity.favoriteCount = 0;
}
return removed;
}
/**
* Look up the parent entity record for counter maintenance. Throws if the
* entity does not exist — callers should validate existence (and access)
* before invoking favorite/unfavorite.
*/
private requireEntity(
entityType: StorageFavoriteEntityType,
entityId: string,
): { favoriteCount?: number; updatedAt: Date } {
const map = entityType === 'agent' ? this.db.agents : this.db.skills;
const entity = map.get(entityId);
if (!entity) {
throw new Error(`Cannot favorite: ${entityType} with id ${entityId} does not exist`);
}
return entity;
}
}
View on GitHub (pinned to 75dd419e61)
Solutions
- Create/upsert the agent or skill with the given id in the store before favoriting it.
- Verify the entityId against a list of existing entities (e.g. `listAgents()`/`listSkills()`) before calling favorite.
- Check that you are using the same storage instance where the entity was created — a new in-memory store starts empty.
- Confirm `entityType` is exactly 'agent' or 'skill' and matches how the entity was stored.
Example fix
// before
await storage.favorite('agent', 'agent-123'); // throws if not created
// after
await storage.createAgent({ id: 'agent-123', /* ... */ });
await storage.favorite('agent', 'agent-123'); Defensive patterns
Strategy: validation
Validate before calling
const exists = entityType === 'agent'
? await storage.listAgents().then(a => a.some(x => x.id === entityId))
: await storage.listSkills().then(s => s.some(x => x.id === entityId));
if (!exists) throw new Error(`${entityType} ${entityId} not created yet`);
await storage.favorite(entityType, entityId); Type guard
function isKnownEntity(entity: { id: string } | undefined, id: string): entity is { id: string } {
return entity !== undefined && entity.id === id;
} Try / catch
try {
await storage.favorite(entityType, entityId);
} catch (e) {
if (e instanceof Error && e.message.includes('does not exist')) {
// create the entity first or skip favoriting
return null;
}
throw e;
} Prevention
- Always create/upsert the entity before favoriting in the same storage instance.
- Share one storage instance across your app (DI) rather than constructing ad-hoc in-memory stores.
- Validate entityType is 'agent' or 'skill' with a union type at compile time.
- In tests, seed fixtures before exercising favorite logic.
When it happens
Trigger: Calling `favorite(entityType, entityId)` (or unfavorite variants that also call `requireEntity`) with an entityId that was never added to `this.db.agents` or `this.db.skills`, or with an id whose entry was deleted, or with an `entityType` that routes to the wrong map (only 'agent' and 'skill' are supported).
Common situations: Passing a hardcoded or stale test id; favoriting an entity in a fresh store that was never seeded via create/upsert; a race where the agent/skill is deleted between listing and favoriting; typos in id strings; using an id from a different store instance (e.g. per-request in-memory db instances).
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.
Related errors
- Agent with id ${agent.id} already exists
- Agent with id ${id} not found
- Dataset not found: ${args.id}
- Dataset not found: ${args.datasetId}
- Item not found: ${args.id}
AI-assisted analysis of mastra-ai/mastra@75dd419e61 (2026-08-30).
Data as JSON: /api/errors/f0cbfb39a3e16dd1.
Report an issue: GitHub.