Skip to content

Commit

Permalink
Merge pull request #43 from koopjs/b/10625-handle-stream-errors
Browse files Browse the repository at this point in the history
handle stream error
  • Loading branch information
sansth1010 authored Jun 5, 2024
2 parents fce09d2 + 4b73849 commit f7033e7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
27 changes: 27 additions & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as mockSiteModel from './test-helpers/mock-site-model.json';
import * as mockDataset from './test-helpers/mock-dataset.json';
import { readableFromArray } from './test-helpers/stream-utils';
import { DcatApError } from './dcat-ap/dcat-ap-error';
import { PassThrough } from 'stream';

describe('Output Plugin', () => {
let mockFetchSite;
Expand All @@ -30,6 +31,13 @@ describe('Output Plugin', () => {
app.get('/dcat', function (req, res, next) {
req.app.locals.feedTemplateTransforms = feedTemplateTransforms;
res.locals.feedTemplate = feedTemplate;

app.use((err, _req, res, _next) => {
res.status(err.status || 500)
res.send({
error: err.message
})
})
next();
}, plugin.serve.bind(plugin));

Expand Down Expand Up @@ -132,6 +140,25 @@ describe('Output Plugin', () => {
// TODO test stream error
});

it('returns error if stream emits an error', async () => {
const mockReadable = new PassThrough();

plugin.model.pullStream.mockResolvedValue(mockReadable);
const mockError = new Error('stream error')

setTimeout(() => {
mockReadable.emit('error', mockError)
}, 200)
await request(app)
.get('/dcat')
.set('host', siteHostName)
.expect('Content-Type', /application\/json/)
.expect(500)
.expect((res) => {
expect(res.body).toEqual({ error: 'stream error' });
});
});

it('returns 400 when searchRequest returns 400', async () => {
[plugin, app] = buildPluginAndApp({}, {});

Expand Down
10 changes: 6 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ export = class OutputDcatAp201 {
const { dcatStream } = getDataStreamDcatAp201(feedTemplate, feedTemplateTransformsDcatAp);

const datasetStream = await this.getDatasetStream(req);

datasetStream
.pipe(dcatStream)
.pipe(res);

datasetStream.on('error', (err) => {
if (req.next) {
req.next(err);
}
}).pipe(dcatStream).pipe(res);

} catch (err) {
res.status(err.statusCode).send(this.getErrorResponse(err));
Expand Down

0 comments on commit f7033e7

Please sign in to comment.