farion1231/cc-switch · warning · Error

OPENCLAW_ENV_INVALID_JSON

OPENCLAW_ENV_INVALID_JSON

Error message

OPENCLAW_ENV_INVALID_JSON

What it means

Thrown when parseOpenClawEnvEditorValue receives non-empty text that JSON.parse rejects. It marks a syntax error in the OpenClaw env JSON blob, distinct from the empty-input and wrong-shape cases, so the UI can point the user at malformed JSON.

Source

Thrown at src/components/openclaw/utils.ts:26

  "minimal",
  "coding",
  "messaging",
  "full",
];

export const OPENCLAW_UNSUPPORTED_PROFILE = "__unsupported_profile__";
export const OPENCLAW_UNSET_PROFILE = "__unset_profile__";

export function parseOpenClawEnvEditorValue(raw: string): OpenClawEnvConfig {
  if (!raw.trim()) {
    throw new Error("OPENCLAW_ENV_EMPTY");
  }

  let parsed: unknown;
  try {
    parsed = JSON.parse(raw);
  } catch {
    throw new Error("OPENCLAW_ENV_INVALID_JSON");
  }

  if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
    throw new Error("OPENCLAW_ENV_OBJECT_REQUIRED");
  }
  return parsed as OpenClawEnvConfig;
}

export function isOpenClawToolsProfile(
  profile?: string,
): profile is OpenClawToolsProfile {
  return (
    typeof profile === "string" &&
    OPENCLAW_TOOL_PROFILES.includes(profile as OpenClawToolsProfile)
  );
}

export function getOpenClawToolsProfileSelectValue(profile?: string): string {

View on GitHub (pinned to a2e22f3302)

Solutions

  1. Validate with JSON.parse in a debounced change handler and show the syntax error inline before save
  2. Use a JSON-aware editor component that flags errors as the user types
  3. If comments must be allowed, strip them (or use jsonc) before calling the parser

Example fix

// before
const config = parseOpenClawEnvEditorValue(raw); // throws OPENCLAW_ENV_INVALID_JSON on typo

// after
try {
  JSON.parse(raw);
  setJsonError(null);
} catch (e) {
  setJsonError(e instanceof Error ? e.message : String(e));
}
Defensive patterns

Strategy: try-catch

Validate before calling

function tryParseJson(raw: string): { ok: true; value: unknown } | { ok: false; error: string } {
  try {
    return { ok: true, value: JSON.parse(raw) };
  } catch (e) {
    return { ok: false, error: e instanceof Error ? e.message : String(e) };
  }
}

const pre = tryParseJson(raw);
if (!pre.ok) {
  setEditorError(pre.error); // show inline before calling the real parser
}

Try / catch

try {
  const config = parseOpenClawEnvEditorValue(raw);
} catch (e) {
  if (e instanceof Error && e.message === "OPENCLAW_ENV_INVALID_JSON") {
    setEditorError("Fix the JSON syntax, then save again");
  } else {
    throw e;
  }
}

Prevention

When it happens

Trigger: parseOpenClawEnvEditorValue('{ "key": }'), ('foo'), ('{a: 1}') - unquoted keys, trailing commas, stray characters, or non-JSON config pasted in all make JSON.parse throw.

Common situations: Hand-editing the env JSON and breaking syntax; pasting TOML or YAML into a JSON-only editor; missing quotes around keys or values.

Related errors


AI-assisted analysis of farion1231/cc-switch@a2e22f3302 (2026-08-16). Data as JSON: /api/errors/8409e3f4a594fc6e. Report an issue: GitHub.