Files
biomedjs/config/promise.js
Dobie Wollert f94ca33b9e Changes
2015-12-16 09:12:35 -08:00

37 lines
721 B
JavaScript

'use strict';
const mongoose = require('mongoose');
module.exports = function() {
return function(req, res, next) {
res.promise = (promise) => {
promise
.then((data) => {
res.json({
data
});
})
.catch((error) => {
if (typeof error === 'string') {
return res.json(400, {
error: {
message: error
}
});
}
if (error.name === 'ValidationError') {
return res.json(400, {
error: error
});
}
console.log(error.stack);
res.json(500, 'Internal error');
});
};
next();
};
};