Skip to content

Commit

Permalink
add special error news message when parsing error occurs
Browse files Browse the repository at this point in the history
  • Loading branch information
gynt committed Oct 5, 2024
1 parent 1c34da8 commit 4b4ff7d
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/function/news/parsing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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() },
},
];
Expand Down

0 comments on commit 4b4ff7d

Please sign in to comment.