linshenkx/prompt-optimizer · error · VariableError

IMPORT_ERROR

IMPORT_ERROR

Error message

Failed to import variables: ${errorMessage}

What it means

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.

Source

Thrown at packages/ui/src/services/VariableManager.ts:381

      const data = JSON.parse(jsonData);
      
      if (data.customVariables && typeof data.customVariables === 'object') {
        // 验证每个变量名
        for (const [name, value] of Object.entries(data.customVariables)) {
          if (typeof value === 'string' && this.validateVariableName(name)) {
            this.customVariables[name] = value;
          }
        }
      }

      if (typeof data.advancedModeEnabled === 'boolean') {
        this.advancedModeEnabled = data.advancedModeEnabled;
      }

      this.saveToStorage();
    } catch (error) {
      const errorMessage = error instanceof Error ? error.message : String(error);
      throw new VariableError(
        `Failed to import variables: ${errorMessage}`,
        undefined,
        undefined,
        'IMPORT_ERROR'
      );
    }
  }

  // 获取变量统计信息
  getStatistics(): {
    customVariableCount: number;
    predefinedVariableCount: number;
    totalVariableCount: number;
    advancedModeEnabled: boolean;
  } {
    return {
      customVariableCount: Object.keys(this.customVariables).length,
      predefinedVariableCount: PREDEFINED_VARIABLES.length,

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Validate/parse the JSON and check the export schema version before calling importVariables
  2. Catch VariableError with code IMPORT_ERROR and surface error.message which includes the underlying cause
  3. If storage is the issue, free localStorage space or persist to a backend instead
  4. Re-export from a matching app version and retry

Example fix

// before
await variableManager.importVariables(rawFileText)

// after
const data = JSON.parse(rawFileText) // fail early with clear JSON error
if (typeof data.customVariables !== 'object') throw new Error('Bad export format')
await variableManager.importVariables(rawFileText)
Defensive patterns

Strategy: try-catch

Validate before calling

const parsed = JSON.parse(json)
if (parsed == null || typeof parsed !== 'object') throw new Error('Invalid import payload')
if (!('customVariables' in parsed) && !('advancedModeEnabled' in parsed)) {
  throw new Error('Unrecognized export schema')
}

Type guard

const isVariableExport = (d: unknown): d is { customVariables?: Record<string, string>; advancedModeEnabled?: boolean } =>
  typeof d === 'object' && d !== null && ('customVariables' in d || 'advancedModeEnabled' in d)

Try / catch

try {
  vm.importVariables(json)
} catch (e) {
  if (e instanceof VariableError && e.code === 'IMPORT_ERROR') {
    showUserError('Import failed: ' + e.message)
    return
  }
  throw e
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


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