@@ -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 ) ;
@@ -77,15 +77,13 @@ function get(req, res, includeBody) {
7777 // This should be implemented in LDP.prototype.get
7878 if ( err && err . status === 404 && glob . hasMagic ( filename ) ) {
7979 debug ( "GET/HEAD -- Glob request" ) ;
80- return globHandler ( req , res ) ;
80+ return globHandler ( req , res , next ) ;
8181 }
8282
8383
8484 if ( err ) {
8585 debug ( 'GET/HEAD -- Read error: ' + err . status + ' ' + err . message ) ;
86- return res
87- . status ( err . status )
88- . send ( err . message ) ;
86+ return next ( err ) ;
8987 }
9088
9189 // Just return that file exists
@@ -122,11 +120,11 @@ function get(req, res, includeBody) {
122120
123121 // TODO this should be added as a middleware in the routes
124122 res . locals . turtleData = data ;
125- return parseLinkedData ( req , res ) ;
123+ return parseLinkedData ( req , res , next ) ;
126124 } ) ;
127125}
128126
129- function globHandler ( req , res ) {
127+ function globHandler ( req , res , next ) {
130128 var ldp = req . app . locals . ldp ;
131129 var filename = utils . uriToFilename ( req . path , ldp . root ) ;
132130 var uri = utils . uriBase ( req ) ;
@@ -139,7 +137,10 @@ function globHandler(req, res) {
139137 glob ( filename , globOptions , function ( err , matches ) {
140138 if ( err || matches . length === 0 ) {
141139 debug ( "GET/HEAD -- No files matching the pattern" ) ;
142- return res . sendStatus ( 404 ) ;
140+ var globErr = new Error ( ) ;
141+ globErr . status = 404 ;
142+ globErr . message = "No files matching glob pattern" ;
143+ return next ( globErr ) ;
143144 }
144145
145146 // Matches found
@@ -176,7 +177,7 @@ function globHandler(req, res) {
176177 'text/turtle' ) ;
177178 // TODO this should be added as a middleware in the routes
178179 res . locals . turtleData = data ;
179- return parseLinkedData ( req , res ) ;
180+ return parseLinkedData ( req , res , next ) ;
180181 } ) ;
181182 } ) ;
182183}
@@ -195,7 +196,7 @@ function aclAllow(match, req, res, callback) {
195196 } ) ;
196197}
197198
198- function parseLinkedData ( req , res ) {
199+ function parseLinkedData ( req , res , next ) {
199200 var ldp = req . app . locals . ldp ;
200201 var filename = utils . uriToFilename ( req . path , ldp . root ) ;
201202 var uri = utils . uriBase ( req ) ;
@@ -220,16 +221,20 @@ function parseLinkedData(req, res) {
220221 $rdf . parse ( turtleData , resourceGraph , baseUri , 'text/turtle' ) ;
221222 } catch ( err ) {
222223 debug ( "GET/HEAD -- Error parsing data: " + err ) ;
223- return res
224- . status ( 500 )
225- . send ( err ) ;
224+ var parseErr = new Error ( ) ;
225+ parseErr . status = 500 ;
226+ parseErr . message = err . message ;
227+ return next ( parseErr ) ;
226228 }
227229
228230 // Graph to `accept` type
229231 $rdf . serialize ( undefined , resourceGraph , null , accept , function ( err , result ) {
230232 if ( result === undefined || err ) {
231233 debug ( "GET/HEAD -- Serialization error: " + err ) ;
232- return res . sendStatus ( 500 ) ;
234+ var serializeErr = new Error ( ) ;
235+ serializeErr . status = 500 ;
236+ serializeErr . message = err . message ;
237+ return next ( serializeErr ) ;
233238 }
234239
235240 return res
@@ -239,12 +244,12 @@ function parseLinkedData(req, res) {
239244 } ) ;
240245}
241246
242- function getHandler ( req , res ) {
243- get ( req , res , true ) ;
247+ function getHandler ( req , res , next ) {
248+ get ( req , res , next , true ) ;
244249}
245250
246- function headHandler ( req , res ) {
247- get ( req , res , false ) ;
251+ function headHandler ( req , res , next ) {
252+ get ( req , res , next , false ) ;
248253}
249254
250255exports . handler = getHandler ;
0 commit comments