can1357/oh-my-pi · error · Error
Cannot choose from an empty header profile list
Error message
Cannot choose from an empty header profile list
What it means
pick() selects a random element from a profile list using the injected RNG. Despite clamping the index to [0, length-1], an empty array yields values[0] === undefined, so it throws this Error. It is an internal invariant guard: callers must never pass an empty candidate list (empty browsers, operatingSystems, or unknown browser with no VERSIONS entry).
Source
Thrown at packages/utils/src/headers.ts:61
browserListQuery: "",
operatingSystems: ["windows", "macos", "linux"],
devices: ["desktop"],
locales: ["en-US", "en"],
httpVersion: "2",
strict: false,
};
const VERSIONS: Readonly<Record<BrowserName, readonly number[]>> = {
chrome: [149, 150, 151],
firefox: [147, 148, 149],
safari: [26, 26.1, 26.2],
};
function pick<T>(values: readonly T[], rng: () => number): T {
const random = rng();
const index = Math.min(values.length - 1, Math.max(0, Math.floor(random * values.length)));
const value = values[index];
if (value === undefined) throw new Error("Cannot choose from an empty header profile list");
return value;
}
function formatLocales(locales: readonly string[]): string {
return locales
.slice(0, 10)
.map((locale, index) => {
if (index === 0) return locale;
const quality = Math.max(0.1, 1 - index / 10).toFixed(1);
return `${locale};q=${quality}`;
})
.join(",");
}
function makeUserAgent(browser: BrowserName, operatingSystem: OperatingSystem, version: number): string {
if (browser === "safari") {
return `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/${version.toFixed(1)} Safari/605.1.15`;
}View on GitHub (pinned to 9690622007)
Solutions
- Ensure options.browsers is non-empty when constructing/calling
- Check that custom rng implementations return finite values in [0,1)
- Verify VERSIONS covers any browser name you pass (only chrome/firefox/safari supported)
- Catch and log to detect misconfigured option propagation
Example fix
// before
new HeaderGenerator({ browsers: [] }).getHeaders();
// after
new HeaderGenerator({ browsers: ["chrome"] }).getHeaders(); // or omit to use defaults Defensive patterns
Strategy: validation
Validate before calling
function assertNonEmpty<T>(values: readonly T[], label: string): readonly T[] {
if (values.length === 0) throw new Error(`${label} must not be empty`);
return values;
}
const browsers = assertNonEmpty(options.browsers ?? ["chrome"], "browsers"); Type guard
function isNonEmpty<T>(values: readonly T[] | undefined): values is readonly [T, ...T[]] {
return Array.isArray(values) && values.length > 0;
} Try / catch
try {
return generator.getHeaders();
} catch (err) {
if (err instanceof Error && err.message.includes("empty header profile list")) {
return fallbackHeaders; // or re-construct with default options
}
throw err;
} Prevention
- Never pass empty browsers/operatingSystems arrays
- Only use the three supported browsers (chrome/firefox/safari)
- Validate custom rng implementations return finite values in [0,1)
- Validate option-bearing config files before constructing HeaderGenerator
When it happens
Trigger: Calling getHeaders with an empty candidates array reached via makeProfile in non-strict mode... practically: passing browsers: [] with a candidate list that ends up empty, or an empty locales/versions list reaching pick; also a buggy custom rng returning NaN (index becomes NaN → values[NaN] undefined).
Common situations: Constructing HeaderGenerator with browsers: [] and then calling getHeaders in a path where the chrome/windows fallback does not apply; passing a broken rng (returns NaN or out-of-range values) in tests.
Related errors
- `ops` must include at least one op entry
- No coherent browser profile matches the requested options
- Only desktop browser profiles are available
- At least one locale is required
- Unsupported language '{value}'. Supported: {}
AI-assisted analysis of can1357/oh-my-pi@9690622007 (2026-08-31).
Data as JSON: /api/errors/eac2bd0c6241735a.
Report an issue: GitHub.