linshenkx/prompt-optimizer · warning · FavoriteValidationError

Prompt content cannot be empty

Error message

Prompt content cannot be empty

What it means

FavoriteManager.addFavorite rejects with FavoriteValidationError when favorite.content is missing or whitespace-only. Content is the mandatory core payload of a favorite prompt.

Source

Thrown at packages/core/src/services/favorite/manager.ts:266

            color: category.color,
            sortOrder: i
          });
        }

        // ✅ 标记已初始化
        await this.storageProvider.setItem('favorite_categories_initialized', 'true');
      }
    } catch (error) {
      console.warn('[FavoriteManager] Failed to ensure default categories:', error);
    }
  }

  async addFavorite(favorite: Omit<FavoritePrompt, 'id' | 'createdAt' | 'updatedAt' | 'useCount'>): Promise<string> {
    await this.ensureInitialized();

    // 验证输入
    if (!favorite.content?.trim()) {
      throw new FavoriteValidationError('Prompt content cannot be empty');
    }

    // 验证 functionMode 必填
    if (!favorite.functionMode) {
      throw new FavoriteValidationError('Function mode (functionMode) cannot be empty');
    }

    // 验证功能模式分类的完整性
    if (favorite.functionMode === 'basic' || favorite.functionMode === 'context') {
      if (!favorite.optimizationMode) {
        throw new FavoriteValidationError(`${favorite.functionMode} mode must specify optimizationMode`);
      }
    }

    if (favorite.functionMode === 'image') {
      if (!favorite.imageSubMode) {
        throw new FavoriteValidationError('Image mode must specify imageSubMode');
      }

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Require non-empty content in the UI before enabling save
  2. Trim and validate content before calling addFavorite
  3. Catch FavoriteValidationError and show a field-level message

Example fix

// before
await manager.addFavorite({ ...fav, content: text });
// after
const content = text?.trim();
if (!content) throw new Error('content required');
await manager.addFavorite({ ...fav, content });
Defensive patterns

Strategy: validation

Validate before calling

const content = input.content?.trim() ?? '';
if (!content) throw new RangeError('content required before addFavorite');

Type guard

const hasValidContent = (f: { content?: string }): f is { content: string } => !!f.content?.trim();

Try / catch

try { await manager.addFavorite(fav); } catch (e) { if (e instanceof FavoriteValidationError && /cannot be empty/.test(e.message)) { highlightField('content'); return; } throw e; }

Prevention

When it happens

Trigger: await manager.addFavorite({ content: '' , ... }) or content undefined/only spaces, on an initialized manager (local implementation, not the proxy).

Common situations: Saving before the user types anything; form state cleared by reset while dialog open; programmatic save of an empty selection; whitespace-only pasted content.

Related errors


AI-assisted analysis of linshenkx/prompt-optimizer@3e677b1d9f (2026-08-27). Data as JSON: /api/errors/ca5f98b4753a19d5. Report an issue: GitHub.