linshenkx/prompt-optimizer · error · Error

variables.importer.errors.unsupportedFormat

Error message

variables.importer.errors.unsupportedFormat

What it means

This error is thrown by parseVariables when the requested import format is neither 'csv' nor 'txt'. With the union type 'csv' | 'txt' this is effectively unreachable in correct TypeScript code; it fires only when the format value comes from untyped input (e.g. a file extension parsed at runtime) or the type assertion is bypassed.

Source

Thrown at packages/ui/src/components/variable/VariableImporter.vue:246

  }
  return placeholders[textFormat.value] || placeholders.csv
}

const getTextInputHelp = (): string => {
  const helps = {
    csv: t('variables.importer.csvTextHelp'),
    txt: t('variables.importer.txtTextHelp')
  }
  return helps[textFormat.value] || helps.csv
}

const parseVariables = (data: unknown, format: 'csv' | 'txt' = 'csv'): Record<string, string> => {
  if (format === 'csv') {
    return parseCsvVariables(data as string)
  } else if (format === 'txt') {
    return parseTxtVariables(data as string)
  }
  throw new Error(t('variables.importer.errors.unsupportedFormat'))
}

const parseCsvVariables = (content: string): Record<string, string> => {
  const lines = content.trim().split('\n')
  if (lines.length < 2) {
    throw new Error(t('variables.importer.errors.csvMinRows'))
  }
  
  const headers = lines[0].split(',').map(h => h.trim().replace(/"/g, ''))
  const nameIndex = headers.findIndex(h => ['name', 'key', 'variable'].includes(h.toLowerCase()))
  const valueIndex = headers.findIndex(h => ['value', 'val'].includes(h.toLowerCase()))
  
  if (nameIndex === -1 || valueIndex === -1) {
    throw new Error(t('variables.importer.errors.csvRequiredColumns'))
  }
  
  const variables: Record<string, string> = {}
  

View on GitHub (pinned to 3e677b1d9f)

Solutions

  1. Restrict the upstream file picker/accept attribute to .csv/.txt and normalize the extension (lowercase, strip dot) before mapping to a format.
  2. If new formats are actually needed, add a parser branch (e.g. 'json') instead of throwing.
  3. Make the format parameter a const-typed literal at call sites so the compiler rejects invalid values.

Example fix

// before
const format = file.name.split('.').pop() as 'csv' | 'txt'
parseVariables(data, format)
// after
const ext = file.name.split('.').pop()?.toLowerCase()
if (ext !== 'csv' && ext !== 'txt') {
  notify(t('variables.importer.errors.unsupportedFormat'))
  return
}
parseVariables(data, ext)
Defensive patterns

Strategy: validation

Validate before calling

const ext = file.name.split('.').pop()?.toLowerCase().replace('.', '')
if (ext !== 'csv' && ext !== 'txt') {
  showError(t('variables.importer.errors.unsupportedFormat'))
  return
}
await parseVariables(await file.text(), ext as 'csv' | 'txt')

Type guard

const isImportFormat = (v: unknown): v is 'csv' | 'txt' =>
  v === 'csv' || v === 'txt'

Try / catch

try { parseVariables(data, format) } catch (e) { if (e instanceof Error && e.message.includes('unsupportedFormat')) { /* prompt user to pick .csv/.txt */ } else throw e }

Prevention

When it happens

Trigger: Calling parseVariables(data, format) with any string other than 'csv' or 'txt' — typically a file extension like 'json' or 'xlsx' derived from the uploaded filename, or a value forced through `as any`.

Common situations: UI lets users pick arbitrary files; the extension-to-format mapping (e.g. .csv -> 'csv', .txt -> 'txt') misses a case like '.tsv' or uppercase '.CSV', or JS callers ignore the union type.

Related errors


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