Skip to content

Commit

Permalink
Issue 423 - Propagate SitemapStream error
Browse files Browse the repository at this point in the history
  • Loading branch information
huntharo committed May 22, 2024
1 parent e40ce05 commit f339f7b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions lib/sitemap-index-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export class SitemapAndIndexStream extends SitemapIndexStream {
this.getSitemapStream = opts.getSitemapStream;
[this.idxItem, this.currentSitemap, this.currentSitemapPipeline] =
this.getSitemapStream(0);
this.currentSitemap.on('error', (err) => this.emit('error', err));
this.limit = opts.limit ?? 45000;
}

Expand All @@ -124,6 +125,7 @@ export class SitemapAndIndexStream extends SitemapIndexStream {
const onFinish = () => {
const [idxItem, currentSitemap, currentSitemapPipeline] =
this.getSitemapStream(this.i / this.limit);
currentSitemap.on('error', (err) => this.emit('error', err));
this.currentSitemap = currentSitemap;
this.currentSitemapPipeline = currentSitemapPipeline;
// push to index stream
Expand Down
11 changes: 10 additions & 1 deletion tests/sitemap-index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ describe('sitemapAndIndex', () => {
resolve(join(targetFolder, 'does', 'not', 'exist'), path)
);

// Streams do not automatically propagate errors
// We must propagate this up to the SitemapStream
outputStream.on('error', (err) => {
sm.emit('error', err);
});

const ws = sm.pipe(outputStream);
return [new URL(path, baseURL).toString(), sm, ws];
},
Expand All @@ -202,7 +208,10 @@ describe('sitemapAndIndex', () => {
sms.write('https://3.example.com/a');
sms.write('https://4.example.com/a');
sms.end();
await finished(sms);
await expect(finished(sms)).rejects.toThrow(
'ENOENT: no such file or directory, open'
);

expect(
existsSync(
resolve(join(targetFolder, 'does', 'not', 'exist'), `./sitemap-0.xml`)
Expand Down

0 comments on commit f339f7b

Please sign in to comment.