故宫的回调错误指引 [英] npm's guidelines on callback errors

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

问题描述

我是通过 NPM的编码风格指南阅读并在以下来了很神秘的建议:


  

要非常小心,从来没有永远永远扔东西。这是有害无益。只需发送错误信息后面的第一个参数回调。


究竟是什么他们的意思是和一个人如何实现此行为?难道他们建议调用它自己内部的回调函数?

这是我能想到使用异步 fs.readdir 的方法。

  fs.readdir(./,回调函数(ERR文件){
  如果(ERR){
    //扔犯错// NPM说,不​​这样做!
    回调(ERR)//这是不是导致无限循环?
  }
  其他{
    //正常的东西
  }
})


解决方案

什么他们想要说的是,你应该设计自己的模块,因此异步函数没有抛出错误赶上,而是一种内部的处理,而回调(如在您提供的 fs.readdir 为例)...

所以,举例来说,这是他们在说什么,你应该设计自己的模块,如:

  VAR例如= {
    logString:功能(数据,回调){
      VAR ERR = NULL;
      如果(typeof运算数据===串){
        的console.log(数据);
      }其他{
        ERR = {消息:数据是不是字符串!};
      }
      回调(ERR);
    }
}

他们希望你来设计它,因此最终用户可以处理错误的回调内,而不是使用try / catch语句...例如,当我们使用例如对象:

  example.logString(123,功能(错误){
  //错误的回调,而不是try / catch语句处理
  如果(ERR)的console.log(ERR)
});

这将登录 {消息:数据是不是字符串!} ,由于数据不具有 typeof运算等于

这是他们在说什么,你应该避免的一个例子:

他们不想让你当你有你在您的处置异步回调抛出错误......因此,可以说,我们重新设计我们的模块,让 logString 方法抛出错误而不是将其传递到回调......像这样:

  VAR例如= {
    logString:功能(数据,回调){
      如果(typeof运算数据===串){
        的console.log(数据);
      }其他{
        //看到的,我们把它扔,而不是...
        扔{消息:数据是不是字符串!};
      }
      回电话();
    }
}

通过这一点,我们必须做整个try / catch语句,否则你会得到一个未捕获的错误:

  {尝试
  example.logString(321,函数(){
    的console.log(完成!)
  });
}赶上(E){
  的console.log(五)
}

最后的想法/概述:

我觉得故宫之所以提出这个方法,因为它是一个简单的异步方法里面更易于管理。

和的NodeJS中的JavaScript一般喜欢有一个异步的环境那么好就什么都有了紧凑到一个地方,错误处理和所有。

随着try / catch语句,它只是多了一个额外的步骤,你必须采取,当它很容易被回调内,而不是处理(如果你异步设计它,你应该)。

I was reading through npm’s coding style guidelines and came across the following very cryptic suggestion:

Be very careful never to ever ever throw anything. It’s worse than useless. Just send the error message back as the first argument to the callback.

What exactly do they mean and how does one implement this behavior? Do they suggest calling the callback function within itself?

Here’s what I could think of using the async fs.readdir method.

fs.readdir('./', function callback(err, files) {
  if (err) {
    // throw err  // npm says DO NOT do this!
    callback(err) // Wouldn’t this cause an infinite loop?
  }
  else {
    // normal stuff
  }
})

解决方案

What they're trying to say is that you should design your modules so the asynchronous functions don't throw errors to catch, but are rather handled inside of a callback (like in the fs.readdir example you provided)...

So, for instance this is what they're saying you should design your module like:

var example = {
    logString: function(data, callback){
      var err = null;
      if (typeof data === "string") {
        console.log(data);
      } else {
        err = {"message": "Data is not a string!"};
      }
      callback(err);
    }
}

They want you to design it so the end user can handle the error inside of the callback instead of using a try/catch statement... For instance, when we use the example object:

example.logString(123, function(err){
  // Error is handled in callback instead of try/catch
  if (err) console.log(err)
});

This would log {"message": "Data is not a string!"}, because the data doesn't have a typeof equal to "string".

Here is an example of what they're saying you should avoid:

They don't want you to throw errors when you have your asynchronous callback at your disposal... So lets say we redesigned our module so the logString method throws an error instead of passing it into a callback... Like this:

var example = {
    logString: function(data, callback){
      if (typeof data === "string") {
        console.log(data);
      } else {
        // See, we're throwing it instead...
        throw {"message": "Data is not a string!"};
      }
      callback();
    }
}

With this, we have to do the whole try/catch statement, or else you'll get an uncaught error:

try {
  example.logString(321, function(){
    console.log("Done!")
  });
} catch (e) {
  console.log(e)
}

Final thoughts / Summary:

The reason I think NPM suggests this method is because it's simply more manageable inside of a asynchronous method.

NodeJS and JavaScript in general likes to have a asynchronous environment so nice to have it all compact into one place, error handling and all.

With the try/catch, it's just one more extra step you have to take, when it could EASILY be handled inside of the callback instead (if you're designing it asynchronously, which you should).

这篇关于故宫的回调错误指引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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