Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a better error message for when RSS is missing link field #3913

Merged
merged 2 commits into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/weak-mangos-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/rss': patch
---

Adds error messages for missing required fields
12 changes: 12 additions & 0 deletions packages/astro-rss/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.`);
}
}
}
20 changes: 20 additions & 0 deletions packages/astro-rss/test/rss.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});
})
});