{"record":{"id":"a5284a5b922fe4b6","repo":"jackwener/OpenCLI","slug":"unexpected-result-from-reddit-whoami-json-strin","errorCode":null,"errorMessage":"Unexpected result from reddit whoami: ${JSON.stringify(result)}","messagePattern":"Unexpected result from reddit whoami: (.+?)","errorType":"exception","errorClass":"CommandExecutionError","httpStatus":null,"severity":"error","filePath":"clis/reddit/whoami.js","lineNumber":55,"sourceCode":"          return { kind: 'auth', detail: 'Not logged in to reddit.com (no identity in /api/me.json)' };\n        }\n        return { kind: 'ok', identity: me };\n      } catch (e) {\n        return { kind: 'exception', detail: String(e && e.message || e) };\n      }\n    })()`);\n\n        if (result?.kind === 'auth') {\n            throw new AuthRequiredError('reddit.com', result.detail);\n        }\n        if (result?.kind === 'http') {\n            throw new CommandExecutionError(`HTTP ${result.httpStatus} from ${result.where}`);\n        }\n        if (result?.kind === 'exception') {\n            throw new CommandExecutionError(`whoami failed: ${result.detail}`);\n        }\n        if (result?.kind !== 'ok') {\n            throw new CommandExecutionError(`Unexpected result from reddit whoami: ${JSON.stringify(result)}`);\n        }\n\n        const u = result.identity;\n        const created = u.created_utc\n            ? new Date(u.created_utc * 1000).toISOString().split('T')[0]\n            : null;\n        const linkKarma = typeof u.link_karma === 'number' ? u.link_karma : null;\n        const commentKarma = typeof u.comment_karma === 'number' ? u.comment_karma : null;\n        const totalKarma = typeof u.total_karma === 'number'\n            ? u.total_karma\n            : (linkKarma != null && commentKarma != null ? linkKarma + commentKarma : null);\n        const inboxCount = typeof u.inbox_count === 'number' ? u.inbox_count : null;\n\n        return [\n            { field: 'Username', value: 'u/' + u.name },\n            { field: 'ID', value: u.id ? 't2_' + u.id : null },\n            { field: 'Post Karma', value: linkKarma != null ? String(linkKarma) : null },\n            { field: 'Comment Karma', value: commentKarma != null ? String(commentKarma) : null },","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/jackwener/OpenCLI/blob/49907e53dc3ade5c223ff0c4c2c2785687cec4e6/clis/reddit/whoami.js#L37-L73","documentation":"This CommandExecutionError is the fallback branch of the reddit whoami result handling: it fires when the result object's kind is none of 'auth', 'http', 'exception', or 'ok' (including kind === undefined when result itself is null/undefined). The CLI stringifies the whole result into the message so developers can see the unexpected shape. It is a defensive invariant check signaling that the whoami wrapper returned something outside its documented result contract.","triggerScenarios":"The whoami evaluation returns null/undefined, or an object whose kind field is missing or has an unrecognized value (e.g., a partial { kind: undefined, ... } result from a failed or truncated evaluate call).","commonSituations":"Automation layer returning null because the page closed before the script resolved; an older/newer version of the whoami snippet emitting a result shape this CLI doesn't know; serialization drops the kind field; race conditions where the eval promise resolves with undefined.","solutions":["Read the JSON.stringify(result) payload in the message to see what shape actually came back.","Re-run the command; a null/undefined result is often a transient page-context race.","Check versions: align the CLI and the embedded whoami snippet/library so both agree on the result contract ({ kind: 'ok'|'auth'|'http'|'exception', ... }).","If result is consistently undefined, verify the automation driver's evaluate() is returning values (not fire-and-forget) and that the page stays open until resolution.","Treat this as a bug if reproducible with a fixed input; file/inspect the wrapper that produces the result object."],"exampleFix":"// before: assuming result always exists\nconst u = result.identity;\n\n// after (library-side hardening)\nif (result?.kind !== 'ok') {\n  throw new CommandExecutionError(`Unexpected result: ${JSON.stringify(result)}`);\n}\nconst u = result.identity;","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"function isOkWhoamiResult(result) {\n  return !!result && typeof result === 'object' &&\n    ['ok', 'auth', 'http', 'exception'].includes(result.kind);\n}","tryCatchPattern":"try {\n  await whoami();\n} catch (e) {\n  if (e instanceof CommandExecutionError && e.message.startsWith('Unexpected result from reddit whoami:')) {\n    const raw = e.message.match(/\\{.*\\}/s)?.[0];\n    console.error('unexpected whoami shape:', raw);\n    // retry once on a fresh context, else report as a bug\n  } else throw e;\n}","preventionTips":["Never assume the eval result exists — the library's fallback branch exists precisely for null/undefined results.","Keep the CLI and its embedded whoami snippet on matching versions so the result contract is stable.","Ensure evaluate() calls are awaited and the page isn't closed before the promise resolves.","If reproducible, capture the stringified result from the message and file a bug with it."],"tags":["unexpected-shape","invariant-violation","reddit","cli"],"backgroundTag":"unexpected-response-shape","analyzedSha":"49907e53dc3ade5c223ff0c4c2c2785687cec4e6","analyzedAt":"2026-08-29T08:14:47.543Z","schemaVersion":2},"datasetVersion":"2026-08-29T12:17:43.993Z"}