santifer/career-ops · warning

gmail: failed to fetch message

Error message

gmail: failed to fetch message ${m.id} — ${err.message}

What it means

Per-message resilience warning in plugins/gmail/index.mjs's ingest: fetching full message details from the Gmail API for message m.id failed. That single message is skipped (continue) rather than aborting the batch — a deliberate design so one bad fetch doesn't kill the whole ingest.

Solutions

  1. Check err.message for 401/invalid credentials → refresh the OAuth token before ingest
  2. 404 → message was deleted; ignore, it's expected drift
  3. 429/403 → respect Gmail API quotas, add backoff/delay between fetches
  4. Re-run ingest; unprocessed messages (not in processedIds) are retried automatically

Example fix

// before
msg = await (await ctx.fetch(`${GMAIL_API}/messages/${m.id}?format=full`, { headers: auth })).json();
// after
const res = await ctx.fetch(`${GMAIL_API}/messages/${m.id}?format=full`, { headers: auth });
if (res.status === 429) { await sleep(2000); continue; } // retry later
msg = await res.json();
Defensive patterns

Strategy: retry

Validate before calling

const res = await ctx.fetch(`${GMAIL_API}/messages/${m.id}?format=full`, { headers: auth });
if (!res.ok) throw new Error(`Gmail API ${res.status} for ${m.id}`);

Type guard

function isRetryable(e) { return /429|5\d\d|timeout|ECONNRESET/i.test(String(e?.message)); }

Try / catch

try { msg = await fetchMessage(m.id); } catch (err) { if (isRetryable(err) && attempt < 3) { await backoff(attempt); continue retry; } console.warn(`skipped ${m.id}: ${err.message}`); }

Prevention

When it happens

Trigger: ctx.fetch of ${GMAIL_API}/messages/{id}?format=full throws or returns invalid JSON: access token expired/revoked (401), message deleted between list and fetch (404), rate limit (429/403), network failure, or .json() on an error body throws.

Common situations: OAuth token expired mid-run; user deleted a listed message from another client; Gmail API quota exceeded during a large mailbox sweep; transient network drop.

Understand the failure class

Background: 'Something went wrong' / 'Request failed (500)' / 'HTTP error! status: 404' — what failed HTTP requests actually mean and how to find the real cause — this error's family across 28 libraries.

Related errors


AI-assisted analysis of santifer/career-ops@aac998c7ed (2026-09-16). Data as JSON: /api/errors/7be53d4b0064c246. Report an issue: GitHub.

Appendix: source

Thrown at plugins/gmail/index.mjs:114

      let url = `${GMAIL_API}/messages?q=${encodeURIComponent(query)}`;
      if (pageToken) url += `&pageToken=${pageToken}`;
      const data = await (await ctx.fetch(url, { headers: auth })).json();
      if (data.messages) messages.push(...data.messages);
      pageToken = data.nextPageToken;
    } while (pageToken);

    const processedIds = loadProcessedIds();
    const seenUrls = new Set();
    const jobs = [];

    for (const m of messages) {
      if (processedIds.has(m.id)) continue;
      // Per-message resilience: a single bad detail fetch is skipped, not fatal.
      let msg;
      try {
        msg = await (await ctx.fetch(`${GMAIL_API}/messages/${m.id}?format=full`, { headers: auth })).json();
      } catch (err) {
        console.warn(`gmail: failed to fetch message ${m.id} — ${err.message}`);
        continue;
      }
      const headers = msg.payload?.headers || [];
      const subject = headers.find(h => h.name?.toLowerCase() === 'subject')?.value || '';

      // Fail-closed on spoofed mail (DMARC).
      if (!isAuthenticEmail(headers)) {
        console.warn(`gmail: skipping spoofed/unauthenticated email "${subject}"`);
        processedIds.add(m.id);
        continue;
      }

      const seed = parseRoleAtCompany(subject);
      const cleanUrls = extractUrls(getMessageBody(msg.payload)).filter(isCleanUrl);
      for (const url of cleanUrls) {
        if (seenUrls.has(url)) continue;
        seenUrls.add(url);
        jobs.push({

View on GitHub (pinned to aac998c7ed)