{"record":{"id":"6fc5c76ec7d735ef","repo":"jackwener/OpenCLI","slug":"context-returned-a-malformed-error","errorCode":null,"errorMessage":"${context} returned a malformed error.","messagePattern":"(.+?) returned a malformed error\\.","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/twitter/post.js","lineNumber":53,"sourceCode":"    });\n}\n\nfunction isUnsupportedInsertTextError(err) {\n    const msg = err instanceof Error ? err.message : String(err);\n    const lower = msg.toLowerCase();\n    return lower.includes('unknown action') || lower.includes('not supported') || lower.includes('inserttext returned no inserted flag');\n}\n\nfunction requirePostActionResult(value, context) {\n    const result = unwrapBrowserResult(value);\n    if (!result || typeof result !== 'object' || Array.isArray(result) || typeof result.ok !== 'boolean') {\n        throw new CommandExecutionError(`${context} returned a malformed result.`);\n    }\n    if (Object.prototype.hasOwnProperty.call(result, 'message') && result.message != null && typeof result.message !== 'string') {\n        throw new CommandExecutionError(`${context} returned a malformed message.`);\n    }\n    if (Object.prototype.hasOwnProperty.call(result, 'error') && result.error != null && typeof result.error !== 'string') {\n        throw new CommandExecutionError(`${context} returned a malformed error.`);\n    }\n    return result;\n}\n\nfunction validateSubmitStatusPair(result) {\n    if ((result.id && !result.url) || (!result.id && result.url)) {\n        throw new CommandExecutionError('Twitter post completion returned only one of id/url.');\n    }\n    if (!result.id && !result.url) return;\n    if (typeof result.id !== 'string' || !/^\\d+$/.test(result.id)) {\n        throw new CommandExecutionError('Twitter post completion returned a malformed status id.');\n    }\n    if (typeof result.url !== 'string') {\n        throw new CommandExecutionError('Twitter post completion returned a malformed status url.');\n    }\n    let url;\n    try {\n        url = new URL(result.url);","sourceCodeStart":35,"sourceCodeEnd":71,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/twitter/post.js#L35-L71","documentation":"requirePostActionResult validates the optional `error` field the same way it validates `message`: if `error` is present and non-null it must be a string, otherwise this CommandExecutionError is thrown with the failing context. This keeps failure diagnostics printable and consistent for the CLI's message/error columns.","triggerScenarios":"A browser-side script from focusComposer, verifyComposerText, insertComposerText, waitForImageUpload, upload, or clickResult returns { ok: false, error: <non-string> } — for example error set to a caught Error object, a thrown DOMException, or a nested object instead of a string.","commonSituations":"In-page catch blocks doing `catch (e) { return { ok: false, error: e } }` instead of `error: e.message`; bundled/minified page changes turning error payloads into objects; forks that attach structured error codes into the error field.","solutions":["Change the in-page script's catch block to return `error: e && e.message ? e.message : String(e)`","Locate the context named in the error message and verify what its evaluate script assigns to `error`","If the page is erroring out, fix the underlying DOM failure so the error path is not hit at all"],"exampleFix":"// before (in-page script)\ntry { ... } catch (e) { return { ok: false, error: e }; }\n// after\ntry { ... } catch (e) { return { ok: false, error: (e && e.message) ? e.message : String(e) }; }","handlingStrategy":"validation","validationCode":"function hasValidError(r) { return !('error' in (r || {})) || r.error == null || typeof r.error === 'string'; }\nif (raw && !hasValidError(raw)) throw new Error('error field must be a string');","typeGuard":"function hasStringError(v) { return v == null || typeof v !== 'object' || !('error' in v) || v.error == null || typeof v.error === 'string'; }","tryCatchPattern":"try {\n  return requirePostActionResult(await page.evaluate(script), context);\n} catch (e) {\n  if (String(e.message).endsWith('returned a malformed error.')) console.warn('in-page catch returned non-string error; use e.message');\n  throw e;\n}","preventionTips":["In catch blocks return error: e.message || String(e), never the raw exception","Keep error payloads flat strings for CLI column output","Test error paths of in-page scripts, not just success paths"],"tags":["browser-automation","type-validation","x-twitter"],"backgroundTag":"malformed-browser-evaluate-result","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}