通过环回拦截错误处理 [英] Intercepting error handling with loopback

查看:63
本文介绍了通过环回拦截错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在环回中是否有关于错误处理的完整,一致且记录良好的信息来源?

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

诸如错误代码及其含义之类的东西与http状态相关.我已经阅读了他们的文档,却没有找到类似的东西.

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.

例如,如果代码555错误,我必须知道它的含义并相应地处理它.

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

有什么想法吗?

推荐答案

我需要捕获"所有消息并翻译它们

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();
   };
};

此中间件必须配置为在final阶段被调用.例如,将上面的代码保存在log-error.js中,然后修改server/middleware.json

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.

这篇关于通过环回拦截错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆