Skip to content

Commit c236bb1

Browse files
author
Martin Martinez Rivera
committed
Modify request handlers to forward errors to custom page error handler
1 parent c5d45c0 commit c236bb1

8 files changed

Lines changed: 83 additions & 57 deletions

File tree

‎lib/acl.js‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,12 @@ ACL.prototype.allowMode = function (mode, userId, aclGraph, accessType, pathUri,
230230
return callback(allowed);
231231
});
232232
};
233+
233234
/**
234235
* Callback used by allowMode.
235236
* @callback ACL~allowMode_cb
236237
* @param {Boolean} result Found valid ACL statement
237238
*/
238-
239-
240239
ACL.prototype.allowControl = function (mode, userId, aclGraph, accessType, pathUri, callback) {
241240
var acl = this;
242241

@@ -257,12 +256,12 @@ ACL.prototype.allowControl = function (mode, userId, aclGraph, accessType, pathU
257256

258257
}, callback);
259258
};
259+
260260
/**
261261
* Callback used by allowControl.
262262
* @callback ACL~allowControl_cb
263263
* @param {Boolean} result Found valid ACL statement
264264
*/
265-
266265
ACL.prototype.allow = function(mode, address, callback) {
267266
var ldp = this.ldp;
268267
var acl = this;
@@ -384,7 +383,7 @@ ACL.prototype.fetchDocument = function(uri, callback) {
384383
// return an empty body to be parsed
385384
return cb(null, '');
386385
}
387-
386+
388387
return ldp.readFile(documentPath, cb);
389388
});
390389
},
@@ -510,7 +509,11 @@ function allow(mode, req, res, next) {
510509
var reqPath = res && res.locals && res.locals.path ? res.locals.path : req.path;
511510
var acl = reqToACL(req);
512511
acl.allow(mode, reqPath, function(err) {
513-
next(err);
512+
if (err) {
513+
next(err);
514+
} else {
515+
next();
516+
}
514517
});
515518
}
516519

@@ -540,7 +543,11 @@ exports.allowAppendThenWriteHandler = function(req, res, next) {
540543
}
541544
// Append failed, maybe user can write
542545
allow("Write", req, res, function(err) {
543-
next(err);
546+
if (err) {
547+
return next(err);
548+
} else {
549+
return next();
550+
}
544551
});
545552
});
546553

@@ -549,4 +556,4 @@ exports.allowAppendThenWriteHandler = function(req, res, next) {
549556

550557
exports.allowControlHandler = function(req, res, next) {
551558
allowIfACLEnabled("Control", req, res, next);
552-
};
559+
};

‎lib/handlers/delete.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var utils = require('../utils.js');
77
var metadata = require('../metadata.js');
88

99
// Delete a container or resource
10-
function handler(req, res) {
10+
function handler(req, res, next) {
1111
debug('DELETE -- ' + req.originalUrl);
1212

1313
var ldp = req.app.locals.ldp;
@@ -16,9 +16,7 @@ function handler(req, res) {
1616
ldp.delete(filename, function(err) {
1717
if (err) {
1818
debug("DELETE -- error: " + err);
19-
return res
20-
.status(err.status)
21-
.send(err.message);
19+
return next(err);
2220
}
2321

2422
debug("DELETE -- Ok.");

‎lib/handlers/get.js‎

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var turtleExtension = '.ttl';
2323
// this should be moved to options
2424
var 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

250255
exports.handler = getHandler;

‎lib/handlers/patch.js‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var debug = require('../logging').handlers;
99
var utils = require('../utils.js');
1010
var subscription = require('../subscription.js');
1111

12-
function handler(req, res) {
12+
function handler(req, res, next) {
1313
var ldp = req.app.locals.ldp;
1414
debug('PATCH -- ' + req.originalUrl);
1515
debug('PATCH -- text length: ' + (req.text ? req.text.length : 'undefined2'));
@@ -26,9 +26,7 @@ function handler(req, res) {
2626
if (patchContentType === 'application/sparql') {
2727
sparql(filename, targetURI, req.text, function(err, result) {
2828
if (err) {
29-
return res
30-
.status(err.status)
31-
.send(err.message);
29+
next(err);
3230
}
3331

3432
return res.json(result);
@@ -37,9 +35,7 @@ function handler(req, res) {
3735
if (patchContentType === 'application/sparql-update') {
3836
return sparqlUpdate(filename, targetURI, req.text, function (err, patchKB) {
3937
if (err) {
40-
return res
41-
.status(err.status)
42-
.send(err.message);
38+
return next(err);
4339
}
4440

4541
if (ldp.live) {
@@ -49,9 +45,11 @@ function handler(req, res) {
4945
res.send("Patch applied OK\n");
5046
});
5147
} else {
52-
return res
53-
.status(400)
54-
.send("Sorry unknowm patch content type: " + patchContentType);
48+
var contentErr = new Error();
49+
contentErr.status = 400;
50+
contentErr.message = "Sorry unknowm patch content type: " +
51+
patchContentType;
52+
next(contentErr);
5553
}
5654
} // postOrPatch
5755

‎lib/handlers/post.js‎

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var patch = require('./patch.js');
1414
var ldpVocab = require('../vocab/ldp.js');
1515
var rdfVocab = require('../vocab/rdf.js');
1616

17-
function handler(req, res) {
17+
function handler(req, res, next) {
1818
var ldp = req.app.locals.ldp;
1919
var contentType = req.get('content-type');
2020

@@ -38,9 +38,10 @@ function handler(req, res) {
3838
contentType != 'application/nquads' &&
3939
contentType != 'application/n-quads') {
4040
debug("POST -- Invalid Content Type: " + contentType);
41-
return res
42-
.status(415)
43-
.send("Invalid Content Type");
41+
var contentErr = new Error();
42+
contentErr.status = 415;
43+
contentErr.message = "Invalid Content Type";
44+
return next(contentErr);
4445
}
4546

4647

@@ -50,9 +51,11 @@ function handler(req, res) {
5051
// Not a container
5152
if (containerPath[containerPath.length - 1] != '/') {
5253
debug("POST -- Requested resource is not a container");
53-
return res
54-
.set('Allow', 'GET,HEAD,PUT,DELETE')
55-
.sendStatus(405);
54+
res.set('Allow', 'GET,HEAD,PUT,DELETE');
55+
var allowErr = new Error();
56+
allowErr.status = 405;
57+
allowErr.message = "Requested resource is not a container";
58+
return next(allowErr);
5659
}
5760

5861
debug("POST -- Content Type: " + contentType);
@@ -69,7 +72,10 @@ function handler(req, res) {
6972
if (resourcePath === null) {
7073
ldp.releaseResourceUri(resourcePath);
7174
debug("POST -- URI already exists or in use");
72-
return res.sendStatus(400);
75+
var resourceErr = new Error();
76+
resourceErr.status = 400;
77+
resourceErr.message = "URI alredy exists or in use";
78+
return next(resourceErr);
7379
}
7480

7581
// Creating a graph and add the req text
@@ -90,7 +96,8 @@ function handler(req, res) {
9096
} catch (parseErr) {
9197
debug("POST -- Error parsing resource: " + parseErr);
9298
ldp.releaseResourceUri(resourcePath);
93-
return res.sendStatus(400);
99+
parseErr.status = 400;
100+
return next(parseErr);
94101
}
95102

96103
// Add header link to the resource
@@ -116,7 +123,10 @@ function handler(req, res) {
116123
function containerCallback(err) {
117124
if (err) {
118125
debug("POST -- Error creating new container: " + err);
119-
return res.sendStatus(500);
126+
var createErr = new Error();
127+
createErr.status = 500;
128+
createErr.message = "Cannot create new container";
129+
return next(createErr);
120130
}
121131
debug("POST -- Created new container " + resourceBaseUri);
122132
res.set('Location', resourceBaseUri);
@@ -126,7 +136,10 @@ function handler(req, res) {
126136
function resourceCallback(err) {
127137
if (err) {
128138
debug("POST -- Error creating resource: " + err);
129-
return res.sendStatus(500);
139+
var createErr = new Error();
140+
createErr.status = 500;
141+
createErr.message = "Cannot create new container";
142+
return next(createErr);
130143
}
131144
res.set('Location', resourceBaseUri);
132145
return res.sendStatus(201);

‎lib/handlers/put.js‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var debug = require('../logging').handlers;
99
var utils = require('../utils.js');
1010
var header = require('../header.js');
1111

12-
function handler(req, res) {
12+
function handler(req, res, next) {
1313
var ldp = req.app.locals.ldp;
1414
debug("PUT -- Request path: " + req.originalUrl);
1515
debug("PUT -- Text length: " + (req.text ? req.text.length : 'undefined'));
@@ -20,9 +20,8 @@ function handler(req, res) {
2020
ldp.put(filePath, req.text, function(err) {
2121
if (err) {
2222
debug("PUT -- Write error: " + err.message);
23-
return res
24-
.status(err.status)
25-
.send("Can't write file: "+ err.message);
23+
err.message = "Can't write file: " + err.message;
24+
return next(err);
2625
}
2726

2827
debug("PUT -- Write Ok. Bytes written: " + req.text.length);

‎lib/header.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ function linksHandler(req, res, next) {
3636
filename = path.join(filename, req.path);
3737
if (ldp.isMetadataFile(filename)) {
3838
debug.metadata("Trying to access metadata file as regular file.");
39-
return res.send(404);
39+
var metaErr = new Error();
40+
metaErr.status = 404;
41+
metaErr.message = "Trying to access metadata file as regular file";
42+
return next(metaErr);
4043
}
4144
var fileMetadata = new metadata.Metadata();
4245
if (S(filename).endsWith('/')) {
@@ -117,4 +120,4 @@ module.exports.addLinks = addLinks;
117120
module.exports.parseMetadataFromHeader = parseMetadataFromHeader;
118121
module.exports.parseAcceptRDFHeader = parseAcceptRDFHeader;
119122
module.exports.linksHandler = linksHandler;
120-
module.exports.negotiateContentType = negotiateContentType;
123+
module.exports.negotiateContentType = negotiateContentType;

‎lib/login.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ function loginHandler(req, res, next) {
4040
}
4141
debug("Error processing certificate: " + message);
4242
setEmptySession(req);
43-
return res.status(403).send(message);
43+
var authError = new Error();
44+
authError.status = 403;
45+
authError.message = message;
46+
return next(authError);
4447
} else {
4548
req.session.userId = result;
4649
req.session.identified = true;

0 commit comments

Comments
 (0)