wihaha commited on
Commit
efe3304
·
1 Parent(s): 4de6fe5

fix: keep core sync successful when enrichment fetch fails

Browse files
scripts/worldcup/sync-worldcup-data.mjs CHANGED
@@ -311,7 +311,13 @@ function closestHourlyIndex(times, kickoffIso) {
311
  }
312
 
313
  async function fetchJson(url, options = {}) {
314
- const response = await fetch(url, options);
 
 
 
 
 
 
315
  if (!response.ok) {
316
  const text = await response.text().catch(() => '');
317
  throw new Error(`HTTP ${response.status} ${response.statusText} for ${url}: ${text.slice(0, 300)}`);
@@ -319,6 +325,13 @@ async function fetchJson(url, options = {}) {
319
  return response.json();
320
  }
321
 
 
 
 
 
 
 
 
322
  async function fetchWeatherSnapshots(pool) {
323
  const sourceId = await ensureNamedSource(pool, {
324
  source_name: 'open-meteo',
@@ -695,22 +708,38 @@ async function main() {
695
  console.log(`[${item.key}] fetched=${result.fetched} inserted=${result.inserted} updated=${result.updated}`);
696
  }
697
 
698
- const weatherResult = await fetchWeatherSnapshots(pool);
699
- payload.recordsFetched.weather = weatherResult.fetched;
700
- payload.recordsUpserted.weather = {
701
- inserted: weatherResult.inserted,
702
- updated: weatherResult.updated,
703
- };
704
- console.log(`[weather] fetched=${weatherResult.fetched} inserted=${weatherResult.inserted} updated=${weatherResult.updated}`);
705
-
706
- const oddsResult = await fetchOddsSnapshots(pool);
707
- payload.recordsFetched.odds = oddsResult.fetched;
708
- payload.recordsUpserted.odds = {
709
- inserted: oddsResult.inserted,
710
- updated: oddsResult.updated,
711
- skipped: oddsResult.skipped || false,
712
- };
713
- console.log(`[odds] fetched=${oddsResult.fetched} inserted=${oddsResult.inserted} updated=${oddsResult.updated}${oddsResult.skipped ? ' skipped=true' : ''}`);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
714
 
715
  await finishRun(pool, runId, 'success', payload);
716
  });
 
311
  }
312
 
313
  async function fetchJson(url, options = {}) {
314
+ let response;
315
+ try {
316
+ response = await fetch(url, options);
317
+ } catch (error) {
318
+ const cause = error.cause ? ` cause=${error.cause.code || error.cause.message || String(error.cause)}` : '';
319
+ throw new Error(`Fetch failed for ${url}: ${error.message || String(error)}${cause}`);
320
+ }
321
  if (!response.ok) {
322
  const text = await response.text().catch(() => '');
323
  throw new Error(`HTTP ${response.status} ${response.statusText} for ${url}: ${text.slice(0, 300)}`);
 
325
  return response.json();
326
  }
327
 
328
+ function serializeError(error) {
329
+ return {
330
+ message: error.message || String(error),
331
+ cause: error.cause ? (error.cause.code || error.cause.message || String(error.cause)) : null,
332
+ };
333
+ }
334
+
335
  async function fetchWeatherSnapshots(pool) {
336
  const sourceId = await ensureNamedSource(pool, {
337
  source_name: 'open-meteo',
 
708
  console.log(`[${item.key}] fetched=${result.fetched} inserted=${result.inserted} updated=${result.updated}`);
709
  }
710
 
711
+ try {
712
+ const weatherResult = await fetchWeatherSnapshots(pool);
713
+ payload.recordsFetched.weather = weatherResult.fetched;
714
+ payload.recordsUpserted.weather = {
715
+ inserted: weatherResult.inserted,
716
+ updated: weatherResult.updated,
717
+ };
718
+ console.log(`[weather] fetched=${weatherResult.fetched} inserted=${weatherResult.inserted} updated=${weatherResult.updated}`);
719
+ } catch (error) {
720
+ const serialized = serializeError(error);
721
+ payload.recordsFetched.weather = 0;
722
+ payload.recordsUpserted.weather = { inserted: 0, updated: 0, error: serialized.message, cause: serialized.cause };
723
+ payload.logText += `\n[weather warning]\n${error.stack || serialized.message}`;
724
+ console.warn(`[weather] skipped error=${serialized.message}`);
725
+ }
726
+
727
+ try {
728
+ const oddsResult = await fetchOddsSnapshots(pool);
729
+ payload.recordsFetched.odds = oddsResult.fetched;
730
+ payload.recordsUpserted.odds = {
731
+ inserted: oddsResult.inserted,
732
+ updated: oddsResult.updated,
733
+ skipped: oddsResult.skipped || false,
734
+ };
735
+ console.log(`[odds] fetched=${oddsResult.fetched} inserted=${oddsResult.inserted} updated=${oddsResult.updated}${oddsResult.skipped ? ' skipped=true' : ''}`);
736
+ } catch (error) {
737
+ const serialized = serializeError(error);
738
+ payload.recordsFetched.odds = 0;
739
+ payload.recordsUpserted.odds = { inserted: 0, updated: 0, error: serialized.message, cause: serialized.cause };
740
+ payload.logText += `\n[odds warning]\n${error.stack || serialized.message}`;
741
+ console.warn(`[odds] skipped error=${serialized.message}`);
742
+ }
743
 
744
  await finishRun(pool, runId, 'success', payload);
745
  });