Skip to content

Commit

Permalink
Provide a better error message for when RSS is missing link field (#…
Browse files Browse the repository at this point in the history
…3913)

* Provide a better error message for when RSS is missing `link` field

* Adds a changeset
  • Loading branch information
matthewp authored Jul 13, 2022
1 parent 75f202a commit cd2dbfe
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
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');
}
});
})
});

0 comments on commit cd2dbfe

Please sign in to comment.