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:
Matthew Phillips 2022-07-13 16:37:17 -04:00 committed by GitHub
parent 75f202a124
commit cd2dbfedb1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/rss': patch
---
Adds error messages for missing required fields

View file

@ -113,6 +113,7 @@ export async function generateRSS({ rssOptions, items }: GenerateRSSArgs): Promi
if (typeof rssOptions.customData === 'string') xml += rssOptions.customData;
// items
for (const result of items) {
validate(result);
xml += `<item>`;
xml += `<title><![CDATA[${result.title}]]></title>`;
// 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;
}
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.`);
}
}
}

View file

@ -123,4 +123,24 @@ describe('rss', () => {
).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');
}
});
})
});