简体   繁体   English

node.js中的错误处理

[英]Error handling in node.js

In node.js asynchronous functions have callback, however only some of them have err argument passed to that function. 在node.js中,异步函数具有回调,但是只有其中一部分将err参数传递给该函数。 eg fs.writeFile has err as parameter 例如fs.writeFile以err作为参数

fs.writeFile('message.txt', 'Hello Node', function (err) {
  if (err) throw err;
  console.log('It\'s saved!');
});

but fs.watchfile doesn't 但是fs.watchfile没有

fs.watchFile('message.text', function (curr, prev) {
  console.log('the current mtime is: ' + curr.mtime);
  console.log('the previous mtime was: ' + prev.mtime);
});

First question is why some async functions have err argument in callback and some don't? 第一个问题是为什么有些异步函数在回调中有err参数而有些没有呢? How are we supposed to handle error of those which don't? 我们应该如何处理那些没有的错误呢?

Also, as far as synchronous functions are concerned, are they all emitting "error" event which we can subscribe to and handle error this way? 此外,就同步功能而言,它们是否都发出了我们可以订阅并以这种方式处理错误的“错误”事件?

var rs = fs.createReadStream("C:\\Temp\\movie.mp4"); 
    rs.on('error', function(err) {
        console.log('!error: ', err);
    });

and last question: most of synchronous functions have Sync in name... why createReadStream does not? 最后一个问题:大多数同步函数的名称中都有Sync ...为什么createReadStream没有?

Thanks! 谢谢!

First question is why some async functions have err argument in callback and some don't? 第一个问题是为什么有些异步函数在回调中有err参数而有些没有呢? How are we supposed to handle error of those which don't? 我们应该如何处理那些没有的错误呢?

The vast majority of asynchronous functions in node code conform to the convention of the first argument to the callback function is the error. 节点代码中的绝大多数异步函数都符合错误的回调函数第一个参数的约定。 There are a very small number of exceptions, such as fs.exists , which is clearly documented as an antipattern that should not be used in the official docs. 有极少数例外,例如fs.exists ,明确记录为反模式,不应在正式文档中使用。

In the case of watchFile in particular, it's just the semantics of the API that it calls the callback repeatedly and only on success just because that's what it means to watch a file and in general the OSes don't provide a mechanism that has the semantic "notify me when anything goes wrong with this filesystem path", so there you have it. 特别是对于watchFile来说,它只是重复调用API的语义,并且仅在成功时才调用回调,这仅是因为这就是监视文件的含义,并且通常情况下,操作系统不提供具有语义的机制。 “如果此文件系统路径有任何问题,请通知我”,因此您已准备就绪。

Also, as far as synchronous functions are concerned, are they all emitting "error" event which we can subscribe to and handle error this way? 此外,就同步功能而言,它们是否都发出了我们可以订阅并以这种方式处理错误的“错误”事件?

No. Your categorization of node mechanics is incomplete. 否。您对节点力学的分类不完整。 There are at least 4 major paradigms: 至少有4种主要范例:

  • synchronous code. 同步代码。 Mostly throws exceptions to indicate errors like JSON.parse but not always. 通常会抛出异常来指示类似JSON.parse错误,但并非总是如此。 For example, parseInt returns NaN to indicate an error. 例如, parseInt返回NaN表示错误。
  • asynchronous code in callback style. 回调样式的异步代码。 Generally passes an error as the first argument to the callback. 通常将错误作为回调的第一个参数传递。
  • asynchronous code in the event emitter/streaming style. 事件发射器/流样式的异步代码。 Generally emits an "error" event. 通常会发出“错误”事件。
  • asynchronous code using promises. 使用承诺的异步代码。 Not used in node core but has a large and loyal set of devotees in the community. 未在节点核心中使用,但在社区中有大量忠实的奉献者。 Generally rejects the promise via invoking the "reject" callback which is the second argument passed to .then . 通常,通过调用“拒绝”回调(即传递给.then的第二个参数)来拒绝承诺。

most of synchronous functions have Sync in name... why createReadStream does not? 大多数同步函数的名称都是Sync ...为什么createReadStream没有?

Because it's not really synchronous. 因为它不是真正同步的。 It's event emitter based streaming. 它是基于事件发射器的流。 The actual createReadStream will synchronously return you the stream object, but the I/O is asynchronous and events won't start to be emitted until the next tick at the earliest. 实际的createReadStream将同步返回您的流对象,但是I / O是异步的,并且直到最早的下一个滴答都不会开始发出事件。

I highly recommend the article Error Handling in Node.js on the joyent blog for the current best practices described thoroughly. 我强烈推荐joyent博客上的文章Node.js中的错误处理,以全面介绍当前的最佳实践。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM