From 4b4ff7da7f1b9cd2d97f9d8181a4fea82a9c62ab Mon Sep 17 00:00:00 2001 From: gynt Date: Sat, 5 Oct 2024 21:29:42 +0200 Subject: [PATCH] add special error news message when parsing error occurs --- src/function/news/parsing.ts | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/function/news/parsing.ts b/src/function/news/parsing.ts index 9a83c096..1584ed2e 100644 --- a/src/function/news/parsing.ts +++ b/src/function/news/parsing.ts @@ -38,11 +38,18 @@ function parseNewsV2(content: string) { .filter((s) => s.trim().length > 0); return splitContent.map((docString) => { - const m = new RegExp(REGEX_META).exec(docString); - if (m === null || m.length < 2) throw Error('no meta'); + let meta: NewsMeta; - const meta = yaml.parse(m[1]); - meta.timestamp = new Date(meta.timestamp); + const m = new RegExp(REGEX_META).exec(docString); + if (m === null || m.length < 2) { + meta = { + category: 'community', + timestamp: new Date(), + }; + } else { + meta = yaml.parse(m[1]); + meta.timestamp = new Date(meta.timestamp); + } return { meta, @@ -58,10 +65,10 @@ export function parseNews(content: string): News { } catch { try { return parseNewsV1(content); - } catch { + } catch (e) { return [ { - content: 'Failed to load news', + content: `# Failed to load news\n\nreason:${e}`, meta: { category: 'error', timestamp: new Date() }, }, ];