jackwener/OpenCLI · error · CommandExecutionError

WeChat image upload returned a malformed editor image payloa

Error message

WeChat image upload returned a malformed editor image payload.

What it means

Thrown by readCdnImageKeys when the in-page script that collects existing CDN image URLs (data-src/src attributes matching mmbiz or qpic.cn) from the article editor body does not return an array. The library expects evaluate() to yield an array of strings; anything else means the editor DOM or the evaluate bridge behaved unexpectedly.

Source

Thrown at clis/weixin/create-draft.js:108

        editor.focus();
        if (editor.querySelector('[contenteditable="false"]')) editor.innerHTML = '';
        document.execCommand('selectAll', false, null);
        document.execCommand('insertText', false, ${JSON.stringify(text)});
        editor.dispatchEvent(new InputEvent('input', { bubbles: true }));
        return { ok: true };
    })()`);
}

async function readCdnImageKeys(page) {
    const keys = await evaluate(page, `(() => {
        var editor = document.querySelector('#ueditor_0');
        if (!editor) return [];
        return Array.from(editor.querySelectorAll('img')).map(function(img) {
            return img.getAttribute('data-src') || img.getAttribute('src') || '';
        }).filter(function(src) { return /(?:mmbiz|qpic\\.cn)/i.test(src); });
    })()`);
    if (!Array.isArray(keys)) {
        throw new CommandExecutionError('WeChat image upload returned a malformed editor image payload.');
    }
    return keys.map(String);
}

async function injectImageFile(page, image) {
    if (typeof page.setFileInput === 'function') {
        try {
            await page.setFileInput([image.absPath], IMAGE_FILE_INPUT_SELECTOR);
            return;
        } catch (error) {
            if (!isRecoverableFileInputError(error)) {
                const message = error instanceof Error ? error.message : String(error);
                throw new CommandExecutionError(`WeChat image upload failed: ${message}`);
            }
        }
    }

    const base64 = fs.readFileSync(image.absPath).toString('base64');

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Confirm the script is running on the actual WeChat article editor page (textarea#title present) before calling readCdnImageKeys.
  2. Check for a WeChat editor DOM update and update the editor content selector used by the library.
  3. Retry the operation; transient page states can make the evaluate result undefined.
  4. Log the raw evaluate result to see what shape came back and adjust parsing accordingly.

Example fix

// before
const keys = await evaluate(page, `...`);
if (!Array.isArray(keys)) {
    throw new CommandExecutionError('WeChat image upload returned a malformed editor image payload.');
}
// after
const raw = await evaluate(page, `...`);
const keys = Array.isArray(raw) ? raw : (raw && Array.isArray(raw.keys) ? raw.keys : []);
if (!Array.isArray(raw)) console.warn('editor image payload malformed, treating as empty:', raw);
Defensive patterns

Strategy: validation

Validate before calling

await page.wait(2);
const editorReady = await evaluate(page, '!!document.querySelector("textarea#title") && !!document.querySelector(".edui-body-container, #edui1_contentplaceholder, [contenteditable]")');
if (editorReady !== true) throw new Error('editor not ready for image key read');

Type guard

function isStringArray(v) { return Array.isArray(v) && v.every(x => typeof x === 'string'); }

Try / catch

try {
    await createDraftCommand(opts);
} catch (e) {
    if (/malformed editor image payload/i.test(e.message)) {
        return createDraftCommand(opts); // editor DOM may have been momentarily unavailable
    }
    throw e;
}

Prevention

When it happens

Trigger: The evaluate call returns null/undefined because the editor container selector no longer matches, the page navigated away mid-operation, the in-page script threw and the evaluate bridge returned a non-array error shape, or the editor markup changed in a WeChat front-end update.

Common situations: WeChat updated the editor DOM classes/ids; the command ran against a page that is not the article editor; the session degraded so the editor content area is missing; evaluate serialization differences in the automation driver.

Understand the failure class

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/d6b05c67b02c6eeb. Report an issue: GitHub.