jackwener/OpenCLI · error · CommandExecutionError

LinkedIn post analytics expected an array of posts

Error message

LinkedIn post analytics expected an array of posts

What it means

This CommandExecutionError is thrown by summarize() in post-analytics when the posts argument is not an array. The analytics pipeline expects an array of post objects to aggregate; anything else (null, undefined, an object) is treated as a malformed input rather than an empty result.

Source

Thrown at clis/linkedin/post-analytics.js:15

import { cli, Strategy } from '@jackwener/opencli/registry';
import { CommandExecutionError, EmptyResultError } from '@jackwener/opencli/errors';
import { parseLimit } from './shared.js';
import { collectPosts } from './posts-core.js';

const DEFAULT_LIMIT = 30;
const MAX_LIMIT = 100;

function sum(posts, field) {
  return posts.reduce((total, post) => total + (Number(post[field]) || 0), 0);
}

function summarize(posts) {
  if (!Array.isArray(posts)) {
    throw new CommandExecutionError('LinkedIn post analytics expected an array of posts');
  }
  if (posts.length === 0) {
    throw new EmptyResultError('linkedin post-analytics', 'No posts were available for analytics.');
  }
  const latest = posts[0] || {};
  return {
    posts_analyzed: posts.length,
    total_reactions: sum(posts, 'reactions'),
    total_comments: sum(posts, 'comments'),
    total_reposts: sum(posts, 'reposts'),
    total_impressions: sum(posts, 'impressions'),
    posts_with_media: posts.filter((post) => post.media).length,
    posts_with_urls: posts.filter((post) => post.url).length,
    latest_posted_at: latest.posted_at || '',
    latest_reactions: Number(latest.reactions) || 0,
    latest_comments: Number(latest.comments) || 0,
    latest_reposts: Number(latest.reposts) || 0,
    latest_impressions: Number(latest.impressions) || 0,

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Ensure the posts extraction step always returns an array (default to [] when nothing found)
  2. Pass an array to post-analytics, wrapping single posts in [post]
  3. Check whether a LinkedIn DOM/API change made the extractor return a non-array and update the library
  4. Log the actual value passed to summarize to identify the upstream source of the non-array

Example fix

// before
summarize(postsPayload); // postsPayload may be null
// after
summarize(Array.isArray(postsPayload) ? postsPayload : []);
Defensive patterns

Strategy: type-guard

Validate before calling

const posts = await collectPosts();
if (!Array.isArray(posts)) throw new Error('posts extractor returned non-array');

Type guard

function isPostArray(v) {
  return Array.isArray(v) && v.every((p) => p && typeof p === 'object');
}

Try / catch

try {
  const stats = await postAnalytics(posts);
} catch (e) {
  if (String(e.message).includes('expected an array of posts')) {
    return postAnalytics([]); // or surface input bug
  }
  throw e;
}

Prevention

When it happens

Trigger: Calling the post-analytics command (via out/summarize) when the upstream fetch produced a non-array value — e.g. the posts extraction returned undefined/null because the page had no posts container, or a refactor passed a single post object instead of an array.

Common situations: Scraping a profile with zero posts where the extractor returns null instead of []; API/DOM change turning the posts payload into an object wrapper; caller mistakenly passes one post object rather than an array.

Related errors


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