walnut.js/lib/common.js

34 lines
1.0 KiB
JavaScript
Raw Normal View History

2017-06-08 22:16:28 +00:00
'use strict';
2017-09-12 17:55:47 +00:00
function rejectableRequest(req, res, promise, msg) {
2017-06-08 22:16:28 +00:00
return promise.error(function (err) {
res.error(err);
}).catch(function (err) {
console.error('[ERROR] \'' + msg + '\'');
2017-09-12 17:55:47 +00:00
// The stack contains the message as well, so no need to log the message when we log the stack
console.error(err.stack || err.message || JSON.stringify(err));
2017-06-08 22:16:28 +00:00
res.error(err);
});
2017-09-12 17:55:47 +00:00
}
module.exports.rejectableRequest = rejectableRequest;
2017-06-08 22:16:28 +00:00
module.exports.promisableRequest =
module.exports.promiseRequest = function promiseRequest(req, res, promise, msg) {
2017-09-12 17:55:47 +00:00
promise = promise.then(function (result) {
2017-06-08 22:16:28 +00:00
if (result._cache) {
res.setHeader('Cache-Control', 'public, max-age=' + (result._cache / 1000));
res.setHeader('Expires', new Date(Date.now() + result._cache).toUTCString());
}
if (result._mime) {
res.setHeader('Content-Type', result._mime);
}
if (result._value) {
result = result._value;
}
res.send(result);
});
2017-09-12 17:55:47 +00:00
return rejectableRequest(req, res, promise, msg);
2017-06-08 22:16:28 +00:00
};