koala73/worldmonitor · error

Invalid Telegram channel preview

Error message

Invalid Telegram channel preview

What it means

parseTelegramChannelPreview validates the shape of a channel-preview payload. If the value is not a plain object (asRecord returns null for non-objects), the payload doesn't match the expected preview schema and the function throws 'Invalid Telegram channel preview'. It guards against edge/upstream responses that silently changed shape.

Source

Thrown at src/services/telegram-intel.ts:147

  }

  let errorMessage = `${response.status}`;
  try {
    const errorJson = await response.json() as { error?: string };
    errorMessage = errorJson.error || errorMessage;
  } catch {
    errorMessage = `${response.status}`;
  }
  throw new TelegramLookupError(errorMessage, response.status, parseRetryAfterMs(response));
}

function asRecord(value: unknown): Record<string, unknown> | null {
  return value && typeof value === 'object' ? value as Record<string, unknown> : null;
}

function parseTelegramChannelPreview(value: unknown): TelegramChannelPreview {
  const parsed = asRecord(value);
  if (!parsed) throw new Error('Invalid Telegram channel preview');
  const username = normalizeTelegramUsername(String(parsed.username || ''));
  if (!username) throw new Error('Invalid Telegram channel preview');
  const memberCount = parsed.memberCount == null ? null : Number(parsed.memberCount);
  return {
    username,
    title: typeof parsed.title === 'string' && parsed.title.trim() ? parsed.title.trim() : username,
    memberCount: memberCount != null && Number.isFinite(memberCount) && memberCount >= 0
      ? Math.floor(memberCount)
      : null,
    url: `https://t.me/${username}`,
  };
}

function parseTelegramItem(value: unknown): TelegramItem | null {
  const parsed = asRecord(value);
  if (!parsed) return null;
  const required = ['id', 'channel', 'channelTitle', 'url', 'ts', 'text', 'topic'] as const;
  for (const key of required) {

View on GitHub (pinned to 9361220cc0)

Solutions

  1. Check response.ok and content-type before parsing so HTML/error bodies don't reach the parser.
  2. Log the raw payload when this throws to identify which side (edge vs upstream) broke the contract.
  3. Redeploy/align the edge Telegram preview endpoint with the client's expected TelegramChannelPreview schema.
  4. Wrap preview() in try/catch and show a 'preview unavailable' state instead of crashing the caller.

Example fix

// before
const preview = await previewChannel(username); // throws on malformed body
// after
let preview = null;
try {
  preview = await previewChannel(username);
} catch (e) {
  console.warn('Telegram preview unavailable', e);
}
if (isTelegramChannelPreview(preview)) { render(preview); }
Defensive patterns

Strategy: type-guard

Validate before calling

// check before trusting the payload
const body = await res.json();
if (body == null || typeof body !== 'object' || Array.isArray(body)) {
  throw new Error('Unexpected preview payload');
}

Type guard

function isTelegramChannelPreview(v: unknown): v is TelegramChannelPreview {
  if (typeof v !== 'object' || v === null) return false;
  const r = v as Record<string, unknown>;
  return typeof r.username === 'string' && r.username.trim().length > 0;
}

Try / catch

try {
  const preview = parseTelegramChannelPreview(body);
} catch {
  showPreviewUnavailable(); // fall back to disabled preview state
}

Prevention

When it happens

Trigger: Calling preview() where the fetch resolves with a body that is not an object — e.g. an HTML error page parsed leniently, a null/undefined body, an array, or an edge response whose JSON is a string or number.

Common situations: Edge function returning an error page or empty body with 200; API contract change between client and deployed edge version; caches serving stale/malformed payloads; proxy stripping or rewriting response bodies.

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 koala73/worldmonitor@9361220cc0 (2026-09-01). Data as JSON: /api/errors/a699bef61ac788ff. Report an issue: GitHub.