farion1231/cc-switch · error · Error
OPENCLAW_ENV_OBJECT_REQUIRED
OPENCLAW_ENV_OBJECT_REQUIRED
Error message
OPENCLAW_ENV_OBJECT_REQUIRED
What it means
Thrown when the env editor string parses as valid JSON but the result is not a plain object — null, a number, a string, a boolean, or an array are all rejected before the value is cast via 'parsed as OpenClawEnvConfig'. This guard protects that downstream cast from receiving a non-indexable value. It is the last of three gates, after OPENCLAW_ENV_EMPTY and OPENCLAW_ENV_INVALID_JSON.
Solutions
- Wrap the intended key/value pairs in an object literal { ... }
- Pre-check the parsed value with a plain-object type guard before calling the parser
- Show a live preview of the parsed top-level type in the editor UI
Example fix
// before
const env = parseOpenClawEnvEditorValue(raw); // raw = '["coding"]' throws
// after
const value: unknown = JSON.parse(raw);
if (!isPlainObject(value)) {
showEditorError('Env config must be a JSON object');
return;
}
const env = parseOpenClawEnvEditorValue(raw); Defensive patterns
Strategy: type-guard
Validate before calling
const value: unknown = JSON.parse(raw);
if (!isPlainObject(value)) {
showEditorError('Env config must be a JSON object');
return;
}
const env = parseOpenClawEnvEditorValue(raw); Type guard
function isPlainObject(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
} Try / catch
try {
const env = parseOpenClawEnvEditorValue(raw);
} catch (error) {
if (error instanceof Error && error.message === 'OPENCLAW_ENV_OBJECT_REQUIRED') {
// hint: wrap the config in { ... }
} else {
throw error;
}
} Prevention
- Show a live preview of the parsed top-level type (object/array/scalar) in the editor
- Reject arrays and quoted strings visually before submit
- Reuse one isPlainObject guard for all JSON-object validations
When it happens
Trigger: Input such as 'null', '[1,2]', '"coding"', or '42' — valid JSON whose top-level value is not an object.
Common situations: User wraps the config in quotes or brackets; pasting a JSON array of profiles; deleting the outer { } while editing.
Understand the failure class
Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.
Related errors
- OPENCLAW_ENV_INVALID_JSON
- OPENCLAW_ENV_EMPTY
- Configuration must be a JSON object, not an array or other…
- 服务器配置必须是对象
AI-assisted analysis of farion1231/cc-switch@0b5da51016 (2026-08-20).
Data as JSON: /api/errors/0c24da1efa1ebf08.
Report an issue: GitHub.
Appendix: source
Thrown at src/components/openclaw/utils.ts:30
];
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 {
if (!profile) {
return OPENCLAW_UNSET_PROFILE;
}
return isOpenClawToolsProfile(profile)View on GitHub (pinned to 0b5da51016)