Intercepting error handling with loopback

烂漫一生 提交于 2019-12-07 14:24:39

问题


Is there somewhere complete, consistent and well documented source of information on error handling in loopback?

Things like error codes and their meaning, relation with http statuses. I've already read their docs and have not found anything like this.

I would like to translate all the messages to add multi language support to my app. I would also like to add my custom messages, with their code and to use it consistently with other loopback errors.

In order to achieve this, I need to intercept all the errors (I've done this already) and to know all the possible different codes, so I can translate them.

For example, if there is an error with code 555, I have to know what it means and treat it accordingly.

Any ideas?


回答1:


I need to "catch" all the messages and translate them

This is the beginning of an answer. You can write an error-handling middleware that will intercept any error returned by the server. You will need in turn to implement the logic for making the translation.

module.exports = function() { 
   return function logError(err, req, res, next) { 
      if (err) {
        console.log('ERR', req.url, err);
      }
      next();
   };
};

This middleware must be configured to be called in the final phase. Save the code above in log-error.js for instance, then modify server/middleware.json

{ "final": { "./middleware/log-error": {} } }

I need a full list of loopback codes/messages

I'm pretty sure there is no such thing. Errors are build and returned all over the place in the code, not centralized anywhere.



来源:https://stackoverflow.com/questions/40152627/intercepting-error-handling-with-loopback

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!