mirror of
https://github.com/tiennm99/miti99bot.git
synced 2026-05-02 20:21:17 +00:00
fix(lolschedule): structured error logging for cargoquery failures
Swaps the best-effort console.warn for JSON log lines emitted via console.log so Workers Observability + wrangler tail surface the real cause (HTTP status, API error info, or non-JSON body) when /lol_today and /lol_week fall into the error branch.
This commit is contained in:
@@ -51,10 +51,35 @@ async function cargoQuery(params) {
|
||||
body,
|
||||
cf: { cacheTtl: 30, cacheEverything: true },
|
||||
});
|
||||
if (!res.ok) throw new Error(`Leaguepedia API HTTP ${res.status}`);
|
||||
const json = await res.json();
|
||||
if (json?.error) throw new Error(`Leaguepedia error: ${json.error.info || json.error.code}`);
|
||||
return (json?.cargoquery || []).map((r) => r.title);
|
||||
const text = await res.text();
|
||||
if (!res.ok) {
|
||||
console.log(
|
||||
JSON.stringify({ msg: "lolschedule_fetch", status: res.status, body: text.slice(0, 500) }),
|
||||
);
|
||||
throw new Error(`Leaguepedia API HTTP ${res.status}`);
|
||||
}
|
||||
let json;
|
||||
try {
|
||||
json = JSON.parse(text);
|
||||
} catch {
|
||||
console.log(
|
||||
JSON.stringify({ msg: "lolschedule_parse_fail", body: text.slice(0, 500) }),
|
||||
);
|
||||
throw new Error("Leaguepedia non-JSON response");
|
||||
}
|
||||
if (json?.error) {
|
||||
console.log(
|
||||
JSON.stringify({
|
||||
msg: "lolschedule_api_error",
|
||||
code: json.error.code,
|
||||
info: json.error.info,
|
||||
}),
|
||||
);
|
||||
throw new Error(`Leaguepedia error: ${json.error.info || json.error.code}`);
|
||||
}
|
||||
const rows = (json?.cargoquery || []).map((r) => r.title);
|
||||
console.log(JSON.stringify({ msg: "lolschedule_fetch_ok", rows: rows.length }));
|
||||
return rows;
|
||||
}
|
||||
|
||||
/** Format a JS Date as Leaguepedia's UTC literal: `YYYY-MM-DD HH:MM:SS`. */
|
||||
|
||||
@@ -41,7 +41,9 @@ export async function handleToday(ctx, db) {
|
||||
const rows = await getCachedMatches(db, from, to, CACHE_TTL_TODAY_SEC);
|
||||
await ctx.reply(renderToday(rows, from), { parse_mode: "HTML" });
|
||||
} catch (err) {
|
||||
console.warn("lolschedule /lol_today failed", String(err));
|
||||
console.log(
|
||||
JSON.stringify({ msg: "lolschedule_today_fail", err: String(err) }),
|
||||
);
|
||||
await ctx.reply("Could not fetch today's matches. Try again later.");
|
||||
}
|
||||
}
|
||||
@@ -61,7 +63,9 @@ export async function handleWeek(ctx, db) {
|
||||
const rows = await getCachedMatches(db, from, to, CACHE_TTL_WEEK_SEC);
|
||||
await ctx.reply(renderWeek(rows, from, to), { parse_mode: "HTML" });
|
||||
} catch (err) {
|
||||
console.warn("lolschedule /lol_week failed", String(err));
|
||||
console.log(
|
||||
JSON.stringify({ msg: "lolschedule_week_fail", err: String(err) }),
|
||||
);
|
||||
await ctx.reply("Could not fetch this week's matches. Try again later.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user