Hello. Suppose there is a task to take information from the database, do something with it, using the data transmitted by the user (this may return an error), and send it to the client. Very scary implementation:
function sendToUser (cb) { db.get(selector, function (err, data) { if (err) { //??? log(err); return cb(err); } doSomethingWithData(data, function (err, newData) { if (err) { //??? log(err); return cb(err); } // что-то делаем cb(newData); }) }); } sendToUser(function render (err, finalData) { if (err) { res.writeHeader(???) res.render('/path/to/error/page', err); } else { res.render('/data', finalData); } }); How to get now the http-error code to send to the client? There is an option to write something like
db.get(selector, function (err, data) { if (err) { err.httpcode = 5xx; log(err); return cb(err); } doSomethingWithData(data, function (err, newData) { if (err) { err.httpcode = err.httpcode || 4xx; //Пользователь получит в ответе или "база данных не работает" или "неправильные данные в запросе". , but it looks very simple and therefore confuses)). How do you determine the http error code to send to the client?
res.end('Error message');or whatever you have implemented. - zb '