@@ -23,7 +23,7 @@ var turtleExtension = '.ttl';
2323// this should be moved to options
2424var browseSkin = 'https://linkeddata.github.io/warp/#/list/' ;
2525
26- function get ( req , res , includeBody ) {
26+ function get ( req , res , next , includeBody ) {
2727 var ldp = req . app . locals . ldp ;
2828 var uri = utils . uriBase ( req ) ;
2929 var filename = utils . uriToFilename ( req . path , ldp . root ) ;
@@ -63,15 +63,13 @@ function get(req, res, includeBody) {
6363 // This should be implemented in LDP.prototype.get
6464 if ( err && err . status === 404 && glob . hasMagic ( filename ) ) {
6565 debug ( "GET/HEAD -- Glob request" ) ;
66- return globHandler ( req , res ) ;
66+ return globHandler ( req , res , next ) ;
6767 }
6868
6969
7070 if ( err ) {
7171 debug ( 'GET/HEAD -- Read error: ' + err . status + ' ' + err . message ) ;
72- return res
73- . status ( err . status )
74- . send ( err . message ) ;
72+ return next ( err ) ;
7573 }
7674
7775 // Just return that file exists
@@ -108,11 +106,11 @@ function get(req, res, includeBody) {
108106
109107 // TODO this should be added as a middleware in the routes
110108 res . locals . turtleData = data ;
111- return parseLinkedData ( req , res ) ;
109+ return parseLinkedData ( req , res , next ) ;
112110 } ) ;
113111}
114112
115- function globHandler ( req , res ) {
113+ function globHandler ( req , res , next ) {
116114 var ldp = req . app . locals . ldp ;
117115 var filename = utils . uriToFilename ( req . path , ldp . root ) ;
118116 var uri = utils . uriBase ( req ) ;
@@ -125,7 +123,10 @@ function globHandler(req, res) {
125123 glob ( filename , globOptions , function ( err , matches ) {
126124 if ( err || matches . length === 0 ) {
127125 debug ( "GET/HEAD -- No files matching the pattern" ) ;
128- return res . sendStatus ( 404 ) ;
126+ var globErr = new Error ( ) ;
127+ globErr . status = 404 ;
128+ globErr . message = "No files matching glob pattern" ;
129+ return next ( globErr ) ;
129130 }
130131
131132 // Matches found
@@ -162,7 +163,7 @@ function globHandler(req, res) {
162163 'text/turtle' ) ;
163164 // TODO this should be added as a middleware in the routes
164165 res . locals . turtleData = data ;
165- return parseLinkedData ( req , res ) ;
166+ return parseLinkedData ( req , res , next ) ;
166167 } ) ;
167168 } ) ;
168169}
@@ -181,7 +182,7 @@ function aclAllow(match, req, res, callback) {
181182 } ) ;
182183}
183184
184- function parseLinkedData ( req , res ) {
185+ function parseLinkedData ( req , res , next ) {
185186 var ldp = req . app . locals . ldp ;
186187 var filename = utils . uriToFilename ( req . path , ldp . root ) ;
187188 var uri = utils . uriBase ( req ) ;
@@ -206,16 +207,20 @@ function parseLinkedData(req, res) {
206207 $rdf . parse ( turtleData , resourceGraph , baseUri , 'text/turtle' ) ;
207208 } catch ( err ) {
208209 debug ( "GET/HEAD -- Error parsing data: " + err ) ;
209- return res
210- . status ( 500 )
211- . send ( err ) ;
210+ var parseErr = new Error ( ) ;
211+ parseErr . status = 500 ;
212+ parseErr . message = err . message ;
213+ return next ( parseErr ) ;
212214 }
213215
214216 // Graph to `accept` type
215217 $rdf . serialize ( undefined , resourceGraph , null , accept , function ( err , result ) {
216218 if ( result === undefined || err ) {
217219 debug ( "GET/HEAD -- Serialization error: " + err ) ;
218- return res . sendStatus ( 500 ) ;
220+ var serializeErr = new Error ( ) ;
221+ serializeErr . status = 500 ;
222+ serializeErr . message = err . message ;
223+ return next ( serializeErr ) ;
219224 }
220225
221226 return res
@@ -225,12 +230,12 @@ function parseLinkedData(req, res) {
225230 } ) ;
226231}
227232
228- function getHandler ( req , res ) {
229- get ( req , res , true ) ;
233+ function getHandler ( req , res , next ) {
234+ get ( req , res , next , true ) ;
230235}
231236
232- function headHandler ( req , res ) {
233- get ( req , res , false ) ;
237+ function headHandler ( req , res , next ) {
238+ get ( req , res , next , false ) ;
234239}
235240
236241exports . handler = getHandler ;
0 commit comments