AykutSarac/jsoncrack.com · warning

The content was unable to be converted, so it was cleared in

Error message

The content was unable to be converted, so it was cleared instead.

What it means

console.warn emitted by useFile.setFormat when converting the editor content between formats fails. setFormat reads the current contents (prevFormat) via contentToJson, re-serializes to JSON, then converts to the target format via jsonToContent. On any failure it calls get().clear(), wiping the editor and the JSON store, and warns to the console.

Source

Thrown at apps/www/src/store/useFile.ts:97

    set({ fileData, format: fileData.format || FileFormat.JSON });
    get().setContents({ contents: fileData.content, hasChanges: false });
    gaEvent("set_content", { label: fileData.format });
  },
  getContents: () => get().contents,
  getFormat: () => get().format,
  getHasChanges: () => get().hasChanges,
  setFormat: async format => {
    try {
      const prevFormat = get().format;

      set({ format });
      const contentJson = await contentToJson(get().contents, prevFormat);
      const jsonContent = await jsonToContent(JSON.stringify(contentJson, null, 2), format);

      get().setContents({ contents: jsonContent });
    } catch {
      get().clear();
      console.warn("The content was unable to be converted, so it was cleared instead.");
    }
  },
  setContents: async ({ contents, hasChanges = true, skipUpdate = false, format }) => {
    try {
      set({
        ...(contents && { contents }),
        error: null,
        hasChanges,
        format: format ?? get().format,
      });

      const isFetchURL = window.location.href.includes("?");
      const json = await contentToJson(get().contents, get().format);

      if (!useConfig.getState().liveTransformEnabled && skipUpdate) return;

      if (get().hasChanges && contents && contents.length < 80_000 && !isIframe() && !isFetchURL) {
        sessionStorage.setItem("content", contents);

View on GitHub (pinned to 3c9af69e23)

Solutions

  1. Do not call get().clear() on conversion failure — keep the original content so users can fix it.
  2. Validate the content parses in the current format before allowing a format switch.
  3. Log the conversion error (currently only a generic warn) with the format pair and error message.
  4. Offer a 'convert a copy' mode that does not destroy the source on failure.

Example fix

// before
} catch {
  get().clear();
  console.warn("The content was unable to be converted, so it was cleared instead.");
}

// after — preserve content, surface the cause
} catch (err) {
  console.warn(`Cannot convert ${prevFormat} -> ${format}:`, err);
  set({ format: prevFormat }); // revert the format selector
  toast.error(`Conversion failed: ${err?.message ?? "invalid content"}`);
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate current content parses in its format before switching
export async function canConvert(contents: string, from: string, to: string): Promise<boolean> {
  try {
    const json = await contentToJson(contents, from);
    await jsonToContent(JSON.stringify(json, null, 2), to);
    return true;
  } catch { return false; }
}

Type guard

// Detect contentToJson/jsonToContent failures (no thrown primitives)
export function isConversionError(error: unknown): error is Error {
  return error instanceof Error;
}

Try / catch

// Revert format, keep content, surface the cause
try { /* conversion */ }
catch (err) {
  set({ format: prevFormat });
  console.warn(`Cannot convert ${prevFormat} -> ${format}:`, err);
  toast.error(`Conversion failed: ${err?.message ?? "invalid content"}`);
}

Prevention

When it happens

Trigger: Switching format when the editor content cannot be parsed in the previous format (e.g. malformed YAML, CSV, XML) — contentToJson throws; or the JSON cannot be expressed in the target format — jsonToContent throws. The catch then destructively clears everything.

Common situations: Mid-edit content that is temporarily invalid when the user switches format; a CSV/XML payload that doesn't round-trip; selecting a format mismatched to the data.

Related errors


AI-assisted analysis of AykutSarac/jsoncrack.com@3c9af69e23 (2026-08-12). Data as JSON: /api/errors/d3ae3e81f8c4cd21. Report an issue: GitHub.