{"record":{"id":"826aca6fb5accf81","repo":"linshenkx/prompt-optimizer","slug":"import-error","errorCode":"IMPORT_ERROR","errorMessage":"Failed to import variables: ${errorMessage}","messagePattern":"Failed to import variables: (.+?)","errorType":"exception","errorClass":"VariableError","httpStatus":null,"severity":"error","filePath":"packages/ui/src/services/VariableManager.ts","lineNumber":381,"sourceCode":"      const data = JSON.parse(jsonData);\n      \n      if (data.customVariables && typeof data.customVariables === 'object') {\n        // 验证每个变量名\n        for (const [name, value] of Object.entries(data.customVariables)) {\n          if (typeof value === 'string' && this.validateVariableName(name)) {\n            this.customVariables[name] = value;\n          }\n        }\n      }\n\n      if (typeof data.advancedModeEnabled === 'boolean') {\n        this.advancedModeEnabled = data.advancedModeEnabled;\n      }\n\n      this.saveToStorage();\n    } catch (error) {\n      const errorMessage = error instanceof Error ? error.message : String(error);\n      throw new VariableError(\n        `Failed to import variables: ${errorMessage}`,\n        undefined,\n        undefined,\n        'IMPORT_ERROR'\n      );\n    }\n  }\n\n  // 获取变量统计信息\n  getStatistics(): {\n    customVariableCount: number;\n    predefinedVariableCount: number;\n    totalVariableCount: number;\n    advancedModeEnabled: boolean;\n  } {\n    return {\n      customVariableCount: Object.keys(this.customVariables).length,\n      predefinedVariableCount: PREDEFINED_VARIABLES.length,","sourceCodeStart":363,"sourceCodeEnd":399,"githubUrl":"https://github.com/linshenkx/prompt-optimizer/blob/3e677b1d9f7e0493c142c175560531e7ae786dce/packages/ui/src/services/VariableManager.ts#L363-L399","documentation":"VariableManager.importVariables wraps any failure during JSON parsing or data application in a VariableError with code IMPORT_ERROR, chaining the underlying message. The original error is caught in the try block that restores state (custom variables, advanced mode) and calls saveToStorage; the wrapper preserves context while normalizing the error type.","triggerScenarios":"Calling importVariables(json) with malformed JSON, a payload whose shape doesn't match the expected import schema (missing/invalid fields), or when saveToStorage fails (e.g. localStorage quota exceeded or unavailable).","commonSituations":"Importing a file exported from a different app version with a changed schema; corrupted export files; Safari private mode / disabled storage causing saveToStorage to throw; hand-edited JSON payloads.","solutions":["Validate/parse the JSON and check the export schema version before calling importVariables","Catch VariableError with code IMPORT_ERROR and surface error.message which includes the underlying cause","If storage is the issue, free localStorage space or persist to a backend instead","Re-export from a matching app version and retry"],"exampleFix":"// before\nawait variableManager.importVariables(rawFileText)\n\n// after\nconst data = JSON.parse(rawFileText) // fail early with clear JSON error\nif (typeof data.customVariables !== 'object') throw new Error('Bad export format')\nawait variableManager.importVariables(rawFileText)","handlingStrategy":"try-catch","validationCode":"const parsed = JSON.parse(json)\nif (parsed == null || typeof parsed !== 'object') throw new Error('Invalid import payload')\nif (!('customVariables' in parsed) && !('advancedModeEnabled' in parsed)) {\n  throw new Error('Unrecognized export schema')\n}","typeGuard":"const isVariableExport = (d: unknown): d is { customVariables?: Record<string, string>; advancedModeEnabled?: boolean } =>\n  typeof d === 'object' && d !== null && ('customVariables' in d || 'advancedModeEnabled' in d)","tryCatchPattern":"try {\n  vm.importVariables(json)\n} catch (e) {\n  if (e instanceof VariableError && e.code === 'IMPORT_ERROR') {\n    showUserError('Import failed: ' + e.message)\n    return\n  }\n  throw e\n}","preventionTips":["Parse and shape-check exports before importing","Keep export/import schema versioned and verify on import","Ensure localStorage is available (not in private mode) before importing"],"tags":["import","variables","json","storage"],"backgroundTag":"import-validation-failed","analyzedSha":"3e677b1d9f7e0493c142c175560531e7ae786dce","analyzedAt":"2026-08-27T21:29:16.709Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}