farion1231/cc-switch · error · Error
OPENCLAW_ENV_INVALID_JSON
OPENCLAW_ENV_INVALID_JSON
Error message
OPENCLAW_ENV_INVALID_JSON
What it means
Thrown when JSON.parse rejects the (non-blank) env editor string while parsing an OpenClaw env configuration. The original SyntaxError is deliberately swallowed and re-thrown as the sentinel OPENCLAW_ENV_INVALID_JSON so callers can branch on a stable code. It fires only after the empty-string gate has passed, so the input always contains visible characters that are not valid JSON.
Solutions
- Run JSON.parse in a scratch try/catch first and show the syntax error's line/column to the user
- Use a JSON-aware editor component that lints or repairs on paste
- Validate on blur so the user gets feedback before hitting save
Example fix
// before
const env = parseOpenClawEnvEditorValue(raw); // throws OPENCLAW_ENV_INVALID_JSON
// after
try {
JSON.parse(raw);
} catch (e) {
showEditorError(`JSON syntax: ${(e as Error).message}`);
return;
}
const env = parseOpenClawEnvEditorValue(raw); Defensive patterns
Strategy: validation
Validate before calling
try {
JSON.parse(raw);
} catch (e) {
showEditorError(`JSON syntax: ${(e as Error).message}`);
return;
}
const env = parseOpenClawEnvEditorValue(raw); Try / catch
try {
const env = parseOpenClawEnvEditorValue(raw);
} catch (error) {
if (error instanceof Error && error.message === 'OPENCLAW_ENV_INVALID_JSON') {
// point the user at the syntax problem; keep them in the editor
} else {
throw error;
}
} Prevention
- Use a JSON-linting editor component for the env field
- Validate on blur, not only on save
- Run scratch JSON.parse and surface its precise message instead of the sentinel when detail helps
When it happens
Trigger: parseOpenClawEnvEditorValue receives text like '{ "tools": ' (truncated paste), keys in single quotes, trailing commas, or a bare word such as none.
Common situations: Hand-typed JSON with a missing brace; pasting from a terminal that mangled quotes; mixing YAML ('key: value') into a JSON editor.
Understand the failure class
Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.
Related errors
- OPENCLAW_ENV_EMPTY
- OPENCLAW_ENV_OBJECT_REQUIRED
- Configuration must be a JSON object, not an array or other…
- ${error}
- Invalid URL
AI-assisted analysis of farion1231/cc-switch@0b5da51016 (2026-08-20).
Data as JSON: /api/errors/8409e3f4a594fc6e.
Report an issue: GitHub.
Appendix: 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 0b5da51016)