{"record":{"id":"ca5f98b4753a19d5","repo":"linshenkx/prompt-optimizer","slug":"prompt-content-cannot-be-empty","errorCode":null,"errorMessage":"Prompt content cannot be empty","messagePattern":"Prompt content cannot be empty","errorType":"validation","errorClass":"FavoriteValidationError","httpStatus":null,"severity":"warning","filePath":"packages/core/src/services/favorite/manager.ts","lineNumber":266,"sourceCode":"            color: category.color,\n            sortOrder: i\n          });\n        }\n\n        // ✅ 标记已初始化\n        await this.storageProvider.setItem('favorite_categories_initialized', 'true');\n      }\n    } catch (error) {\n      console.warn('[FavoriteManager] Failed to ensure default categories:', error);\n    }\n  }\n\n  async addFavorite(favorite: Omit<FavoritePrompt, 'id' | 'createdAt' | 'updatedAt' | 'useCount'>): Promise<string> {\n    await this.ensureInitialized();\n\n    // 验证输入\n    if (!favorite.content?.trim()) {\n      throw new FavoriteValidationError('Prompt content cannot be empty');\n    }\n\n    // 验证 functionMode 必填\n    if (!favorite.functionMode) {\n      throw new FavoriteValidationError('Function mode (functionMode) cannot be empty');\n    }\n\n    // 验证功能模式分类的完整性\n    if (favorite.functionMode === 'basic' || favorite.functionMode === 'context') {\n      if (!favorite.optimizationMode) {\n        throw new FavoriteValidationError(`${favorite.functionMode} mode must specify optimizationMode`);\n      }\n    }\n\n    if (favorite.functionMode === 'image') {\n      if (!favorite.imageSubMode) {\n        throw new FavoriteValidationError('Image mode must specify imageSubMode');\n      }","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/core/src/services/favorite/manager.ts#L248-L284","documentation":"FavoriteManager.addFavorite rejects with FavoriteValidationError when favorite.content is missing or whitespace-only. Content is the mandatory core payload of a favorite prompt.","triggerScenarios":"await manager.addFavorite({ content: '' , ... }) or content undefined/only spaces, on an initialized manager (local implementation, not the proxy).","commonSituations":"Saving before the user types anything; form state cleared by reset while dialog open; programmatic save of an empty selection; whitespace-only pasted content.","solutions":["Require non-empty content in the UI before enabling save","Trim and validate content before calling addFavorite","Catch FavoriteValidationError and show a field-level message"],"exampleFix":"// before\nawait manager.addFavorite({ ...fav, content: text });\n// after\nconst content = text?.trim();\nif (!content) throw new Error('content required');\nawait manager.addFavorite({ ...fav, content });","handlingStrategy":"validation","validationCode":"const content = input.content?.trim() ?? '';\nif (!content) throw new RangeError('content required before addFavorite');","typeGuard":"const hasValidContent = (f: { content?: string }): f is { content: string } => !!f.content?.trim();","tryCatchPattern":"try { await manager.addFavorite(fav); } catch (e) { if (e instanceof FavoriteValidationError && /cannot be empty/.test(e.message)) { highlightField('content'); return; } throw e; }","preventionTips":["Disable save until content is non-empty after trim","Trim user input before submission","Reset forms only after dialogs close"],"tags":["favorite","validation","empty-input"],"backgroundTag":"input-validation-failed","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}