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
This commit is contained in:
parent
75f202a124
commit
cd2dbfedb1
3 changed files with 37 additions and 0 deletions
5
.changeset/weak-mangos-double.md
Normal file
5
.changeset/weak-mangos-double.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/rss': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Adds error messages for missing required fields
|
|
@ -113,6 +113,7 @@ export async function generateRSS({ rssOptions, items }: GenerateRSSArgs): Promi
|
||||||
if (typeof rssOptions.customData === 'string') xml += rssOptions.customData;
|
if (typeof rssOptions.customData === 'string') xml += rssOptions.customData;
|
||||||
// items
|
// items
|
||||||
for (const result of items) {
|
for (const result of items) {
|
||||||
|
validate(result);
|
||||||
xml += `<item>`;
|
xml += `<item>`;
|
||||||
xml += `<title><![CDATA[${result.title}]]></title>`;
|
xml += `<title><![CDATA[${result.title}]]></title>`;
|
||||||
// If the item's link is already a valid URL, don't mess with it.
|
// 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
|
||||||
|
|
||||||
return xml;
|
return xml;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const requiredFields = Object.freeze(['link', 'title']);
|
||||||
|
|
||||||
|
// Perform validation to make sure all required fields are passed.
|
||||||
|
function validate(item: RSSFeedItem) {
|
||||||
|
for(const field of requiredFields) {
|
||||||
|
if(!(field in item)) {
|
||||||
|
throw new Error(`@astrojs/rss: Required field [${field}] is missing. RSS cannot be generated without it.`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -123,4 +123,24 @@ describe('rss', () => {
|
||||||
).to.be.rejected;
|
).to.be.rejected;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('errors', () => {
|
||||||
|
it('should provide a good error message when a link is not provided', async () => {
|
||||||
|
try {
|
||||||
|
await rss({
|
||||||
|
title: 'Your Website Title',
|
||||||
|
description: 'Your Website Description',
|
||||||
|
site: 'https://astro-demo',
|
||||||
|
items: [{
|
||||||
|
pubDate: new Date(),
|
||||||
|
title: 'Some title',
|
||||||
|
slug: 'foo'
|
||||||
|
}]
|
||||||
|
});
|
||||||
|
chai.expect(false).to.equal(true, 'Should have errored');
|
||||||
|
} catch(err) {
|
||||||
|
chai.expect(err.message).to.contain('Required field [link] is missing');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue