-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (69 loc) · 1.82 KB
/
index.js
File metadata and controls
75 lines (69 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
// @holmwell/errors
//
// A basic error manager for Express projects. Logs
// to the console.
//
module.exports = function () {
var logError = function (err) {
console.log(err);
if (err && err.stack) {
console.log(err.stack);
}
};
var handleError = function (err, res) {
var message = err.message || "Internal server error";
var status = err.status || 500;
if (typeof res === "function") {
// Internal error.
console.log(err);
res(err);
}
else if (res) {
// Customer-visible error.
logError(err);
res.status(status).send(message);
}
else {
// Internal error, no callback.
console.log(err);
}
};
// Useful for guarding callbacks. For example,
// say we have:
//
// db.circles.getAll(function (err, circles) {
// if (err) {
// handleError(err, res);
// }
// <deal with circles>
// });
//
// We can use 'guard' to do this instead:
//
// db.circles.getAll(guard(res, function (circles) {
// <deal with circles>
// }));
//
var guard = function (res, callback) {
var fn = function (err, data) {
if (err) {
return handleError(err, res);
}
callback(data);
};
return fn;
};
var middleware = function (err, req, res, next) {
if (err) {
return handleError(err, res);
}
// TODO: Should not get here. Panic.
next();
};
return {
log: logError,
handle: handleError,
guard: guard,
middleware: middleware
};
}();