@@ -7,6 +7,7 @@ import * as mockSiteModel from './test-helpers/mock-site-model.json';
7
7
import { createMockKoopApp } from './test-helpers/create-mock-koop-app' ;
8
8
import { readableFromArray } from './test-helpers/stream-utils' ;
9
9
import { DcatUsError } from './dcat-us/dcat-us-error' ;
10
+ import { PassThrough } from 'stream' ;
10
11
11
12
function buildPluginAndApp ( feedTemplate , feedTemplateTransforms ) {
12
13
let Output ;
@@ -21,9 +22,16 @@ function buildPluginAndApp(feedTemplate, feedTemplateTransforms) {
21
22
} ;
22
23
23
24
const app = createMockKoopApp ( ) ;
25
+
24
26
app . get ( '/dcat' , function ( req , res , next ) {
25
27
req . app . locals . feedTemplateTransforms = feedTemplateTransforms ;
26
28
res . locals . feedTemplate = feedTemplate ;
29
+ app . use ( ( err , _req , res , _next ) => {
30
+ res . status ( err . status || 500 )
31
+ res . send ( {
32
+ error : err . message
33
+ } )
34
+ } )
27
35
next ( ) ;
28
36
} , plugin . serve . bind ( plugin ) ) ;
29
37
@@ -61,7 +69,6 @@ describe('Output Plugin', () => {
61
69
mockFetchSite = mocked ( fetchSite ) ;
62
70
63
71
mockFetchSite . mockResolvedValue ( mockSiteModel ) ;
64
-
65
72
[ plugin , app ] = buildPluginAndApp ( dcatTemplate , { } ) ;
66
73
} ) ;
67
74
@@ -137,6 +144,25 @@ describe('Output Plugin', () => {
137
144
// TODO test stream error
138
145
} ) ;
139
146
147
+ it ( 'returns error if stream emits an error' , async ( ) => {
148
+ const mockReadable = new PassThrough ( ) ;
149
+
150
+ plugin . model . pullStream . mockResolvedValue ( mockReadable ) ;
151
+ const mockError = new Error ( 'stream error' )
152
+
153
+ setTimeout ( ( ) => {
154
+ mockReadable . emit ( 'error' , mockError )
155
+ } , 200 )
156
+ await request ( app )
157
+ . get ( '/dcat' )
158
+ . set ( 'host' , siteHostName )
159
+ . expect ( 'Content-Type' , / a p p l i c a t i o n \/ j s o n / )
160
+ . expect ( 500 )
161
+ . expect ( ( res ) => {
162
+ expect ( res . body ) . toEqual ( { error : 'stream error' } ) ;
163
+ } ) ;
164
+ } ) ;
165
+
140
166
it ( 'returns 400 when searchRequest returns 400' , async ( ) => {
141
167
[ plugin , app ] = buildPluginAndApp ( { } , { } ) ;
142
168
0 commit comments