jackwener/OpenCLI · error · CommandExecutionError

Midjourney history endpoint returned a malformed payload

Error message

Midjourney history endpoint returned a malformed payload

What it means

fetchHistoryPage calls /api/imagine with user_id, page_size, and optional cursor, then validates the payload. It throws CommandExecutionError if the response is not an object with an Array 'data' field, since pagination cannot proceed without the expected rows/cursor structure.

Source

Thrown at clis/midjourney/utils.js:326

    throw new CommandExecutionError(
      'No Midjourney generation credits remain for this billing period.',
      `Check usage at ${MIDJOURNEY_URL}/account.`,
    );
  }
}

export async function fetchHistory(page, userId, limit = 20) {
  return (await fetchHistoryPage(page, userId, limit)).data;
}

export async function fetchHistoryPage(page, userId, limit = 20, cursor = null) {
  const cursorQuery = cursor ? `&cursor=${encodeURIComponent(cursor)}` : '';
  const payload = await midjourneyJson(
    page,
    `/api/imagine?user_id=${encodeURIComponent(userId)}&page_size=${encodeURIComponent(limit)}${cursorQuery}`,
  );
  if (!payload || typeof payload !== 'object' || !Array.isArray(payload.data)) {
    throw new CommandExecutionError('Midjourney history endpoint returned a malformed payload');
  }
  return {
    data: payload.data,
    cursor: stringOrNull(payload.cursor),
    checkpoint: stringOrNull(payload.checkpoint),
  };
}

export async function fetchJobStatuses(page, jobIds) {
  if (!Array.isArray(jobIds) || !jobIds.length) return [];
  const payload = await midjourneyJson(page, '/api/job-status', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: { jobIds, _frontend_source: 'opencli_adapter' },
  });
  if (!Array.isArray(payload)) {
    throw new CommandExecutionError('Midjourney job-status endpoint returned a malformed payload');
  }

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Log the raw payload from /api/imagine to inspect the actual shape.
  2. Re-authenticate in Chrome and retry to rule out an auth-related error envelope.
  3. Verify the user_id passed to fetchHistoryPage matches getMidjourneyAccount's user_id.
  4. If the API shape changed (e.g. data renamed), update the CLI's history parsing and response validation.

Example fix

// before
const page = await fetchHistoryPage(mjPage, userId, { limit: 20 });
// after
const raw = await midjourneyJson(mjPage, `/api/imagine?user_id=${userId}&page_size=20`);
if (!raw || !Array.isArray(raw.data)) console.error('unexpected history payload:', raw);
const page = await fetchHistoryPage(mjPage, userId, { limit: 20 });
Defensive patterns

Strategy: type-guard

Validate before calling

const payload = await midjourneyJson(page, `/api/imagine?user_id=${userId}&page_size=20`);
if (payload == null || typeof payload !== 'object' || !Array.isArray(payload.data)) console.error('unexpected history payload', payload);

Type guard

function isHistoryPayload(value) {
  return value != null && typeof value === 'object' && !Array.isArray(value) && Array.isArray(value.data);
}

Try / catch

try {
  const pageData = await fetchHistoryPage(page, userId, { limit: 20 });
} catch (err) {
  if (/history endpoint returned a malformed payload/.test(err.message)) {
    console.error('History API shape unexpected; re-authenticate or update the CLI parser.');
  } else throw err;
}

Prevention

When it happens

Trigger: The history endpoint returns null, a non-object, an object without data, or data is not an array — e.g. auth interstitial JSON, API contract change, empty error body, or wrong user_id parameter causing an error envelope.

Common situations: Midjourney renaming or nesting the history response (e.g. data moved under results), Cloudflare challenge JSON, expired session returning an error object without data, or passing a malformed userId.

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


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