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?

  • so if the error during the connection, then where is the other? You send an error, and the end of the package. What is the problem ? There is no need to slander anything, in response just put a header and then res.end('Error message'); or whatever you have implemented. - zb '
  • if there were no errors at the connection now, then there will be. the chain of “treatments” (I don’t know how to call it correctly) can also end at any link. depending on the link, where something went wrong, there will be your own error code. but this error should be raised on the stack. the only thing I thought of was to write its code in the object of the error itself and if any message would be necessary. - alvoro
  • in the model it is not necessary to finish, the controller handles the model errors. and the communication of the model with the controller to http has nothing to do. A typical model call looks like this: model.method (params) .then (successCallback, errorCallback); // depends on your promise implementation, happens .success (), .error () - zb '
  • Application on express. Arranged as follows: models ads routes get lib db app - alvoro
  • In the app, everything is standard. We start the server, we connect middleware and routes. In lib, a wrapper over node-mongodb-native. In ads module that manipulates ads (add, delete, edit, find in the database). ads uses db. After all routes there is one exception handler. If any error reaches him, then he himself has to figure out what to do with it and what to send to the client. Therefore, if something happens in db, ads or in routes, you need to somehow tell this single handler what actually happened. - alvoro

1 answer 1

how do we define? The error code is a numerical representation of one of the standard errors defined by w3c , respectively, if the entry is not found - 404, if not authorized, 401, if not allowed - 403, etc. if something fell down something 5xx and in the title it’s just a number, it shouldn’t be difficult to put it there.

  • I’ll put an answer here so that you can continue to write :) in general, yes, think about the error handling architecture itself, it’s not difficult to set up a stack, in the end you can tell the handler about an error at any level with the help of emit thread . type errorHandler.emit ('error', errorParams) and then at the end, when it seems that the answer is to form an errorHandler.emit ('end'); but this is all straightforward, it will be very confusing and it will be difficult to do some simple actions, I would also advise you to take the mvc framework based on express, and not pure express. - zb '
  • Well, if for some reason you are not satisfied with the set of http errors, you can always make your own protocol for communication with the client, generate a response in the {errors: [], data: {}} style and let the client think how to react to it. But this is also not very necessary. - zb '
  • I am ashamed to admit, but I scored on mvc. I understand, if I understand correctly (about mvc), this question will disappear by itself? - alvoro