schollz/croc · warning · Error

Choose at least one file

Error message

Choose at least one file

What it means

prepareFiles throws immediately when the selected File array is empty. It is a precondition check before any hashing or preparation work begins, ensuring the sender always has at least one file to offer. Cheap and deterministic: it never depends on network or wasm state.

Source

Thrown at web/src/protocol/client.ts:270

  error: unknown,
) {
  if (!control || !key) return;
  try {
    await sendControl(control, {
      t: "error",
      m: errorMessage(error).slice(0, 500),
    }, key);
  } catch {
    // The connection may already be gone.
  }
}

export async function prepareFiles(
  selected: File[],
  callbacks: TransferCallbacks = {},
  signal?: AbortSignal,
) {
  if (selected.length === 0) throw new Error("Choose at least one file");
  const names = new Set<string>();
  const outgoingNames = selected.map((file) => normalizeOutgoingFileName(file.name));
  for (let index = 0; index < selected.length; index += 1) {
    const file = selected[index];
    const outgoingName = outgoingNames[index];
    if (names.has(outgoingName)) throw new Error(`Duplicate filename: ${outgoingName}`);
    if (!Number.isSafeInteger(file.size)) throw new Error(`File is too large: ${file.name}`);
    names.add(outgoingName);
  }

  const prepared: PreparedFile[] = [];
  const engine = wasm();
  for (let index = 0; index < selected.length; index += 1) {
    checkAbort(signal);
    const file = selected[index];
    callbacks.onStatus?.(`Hashing ${index + 1}/${selected.length}: ${file.name}`);
    const hashHandle = await engine.hashInit();
    const reader = file.stream().getReader();

View on GitHub (pinned to e25f1bdc04)

Solutions

  1. Guard the send action in the UI: disable it until at least one file is selected
  2. If the picker was cancelled, simply return instead of calling prepareFiles

Example fix

// before
await prepareFiles(fileInput.files /* empty after cancel */);

// after
const files = [...fileInput.files];
if (files.length === 0) return; // or show 'Choose at least one file' in the UI
Defensive patterns

Strategy: validation

Validate before calling

if (!(selected instanceof Array) || selected.length === 0) {
  throw new Error("Choose at least one file");
}

Prevention

When it happens

Trigger: Calling prepareFiles([]) because the file picker returned nothing (user cancelled), or a UI state bug clearing the selection before submit.

Common situations: Drag-and-drop handlers that register an empty drop; file input cleared after re-render; programmatic callers passing an unfiltered array.

Related errors


AI-assisted analysis of schollz/croc@e25f1bdc04 (2026-08-15). Data as JSON: /api/errors/86e6521ca53e3694. Report an issue: GitHub.