Skip to content

Commit cd2dbfe

Browse files
authored
Provide a better error message for when RSS is missing link field (#3913)
* Provide a better error message for when RSS is missing `link` field * Adds a changeset
1 parent 75f202a commit cd2dbfe

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

.changeset/weak-mangos-double.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/rss': patch
3+
---
4+
5+
Adds error messages for missing required fields

packages/astro-rss/src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ export async function generateRSS({ rssOptions, items }: GenerateRSSArgs): Promi
113113
if (typeof rssOptions.customData === 'string') xml += rssOptions.customData;
114114
// items
115115
for (const result of items) {
116+
validate(result);
116117
xml += `<item>`;
117118
xml += `<title><![CDATA[${result.title}]]></title>`;
118119
// If the item's link is already a valid URL, don't mess with it.
@@ -146,3 +147,14 @@ export async function generateRSS({ rssOptions, items }: GenerateRSSArgs): Promi
146147

147148
return xml;
148149
}
150+
151+
const requiredFields = Object.freeze(['link', 'title']);
152+
153+
// Perform validation to make sure all required fields are passed.
154+
function validate(item: RSSFeedItem) {
155+
for(const field of requiredFields) {
156+
if(!(field in item)) {
157+
throw new Error(`@astrojs/rss: Required field [${field}] is missing. RSS cannot be generated without it.`);
158+
}
159+
}
160+
}

packages/astro-rss/test/rss.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,24 @@ describe('rss', () => {
123123
).to.be.rejected;
124124
});
125125
});
126+
127+
describe('errors', () => {
128+
it('should provide a good error message when a link is not provided', async () => {
129+
try {
130+
await rss({
131+
title: 'Your Website Title',
132+
description: 'Your Website Description',
133+
site: 'https://astro-demo',
134+
items: [{
135+
pubDate: new Date(),
136+
title: 'Some title',
137+
slug: 'foo'
138+
}]
139+
});
140+
chai.expect(false).to.equal(true, 'Should have errored');
141+
} catch(err) {
142+
chai.expect(err.message).to.contain('Required field [link] is missing');
143+
}
144+
});
145+
})
126146
});

0 commit comments

Comments
 (0)