当接收到错误对象时,如何停止函数? [英] How do you stop a function when an Error object is received?

查看:256
本文介绍了当接收到错误对象时,如何停止函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在发生错误的地方停止该功能。



我正在进行http post通话。我已经构造了一个错误优先风格的回调函数。



如果传入一个Error对象,如何停止回调函数?在我的用例中,我需要停止,无论错误类型,因为我需要数据对象后续的回调函数链。



只是 return ?我已经阅读了更高级别的回报,希望得到确认,如果我的代码是正确的我想实现。



我只是想确保回调函数不会继续,因为它们不会传递任何数据对象,因此,都将是错误



server.js

  var request = function(callback){
// HTTP调用服务器
},
函数){
if(!error&& response.statusCode === 200){
//处理和创建的数据对象
return callback(null,data)
}
else {
console.log(error.stack)
return callback(error);
}
})
};
var requestSync = Meteor.wrapAsync(request);


var callback = function(error,data,callback){
if(error){
//如何正确停止这个函数?
return callback(new Error('custom message I send to client'))
}
else {
//使用数据对象执行操作...
}
}

函数在我的方法中调用。

  try {
var resultOne = requestSync()
var resultTwo = callback(resultOne)
//更多async回调函数这里
} catch(e){
console.error('error occured somehwere ='+ e);
} finally {
//
}


解决方案

我没有看到这种方式的编码,所以很难评论,如果这是正确的方式,而不知道一个更多的是你真正想做的。我假设块:

  try {
var resultOne = requestSync()
var resultTwo = callback (resultOne)
//更多的异步回调函数将驻留在这里
} catch(e){
console.error('error occured somehwere ='+ e);
} finally {
}

...调用另一个回调函数比第一个代码片段(或者你传递的resultOne作为第一个参数,回调函数将读取这是一个有效的错误对象,并抛出一个错误)。



但是,为了回答您的关注,如果代码将继续运行,如果其中一个函数抛出一个错误,答案是否定的 - 它会直接转移到catch块。请查看:此文档在MDN



特别是此部分:


它指定在try块中抛出异常时该做什么。也就是说,您希望try块成功,如果它不成功,您希望控制权传递给catch块。如果try块内的任何语句(或在从try块内调用的函数中)抛出异常,则控制立即转移到catch子句。如果没有在try块中抛出异常,则跳过catch子句。


我希望这能回答您的主要问题。由于你似乎在Meteor.wrapAsync()中包装所有异步调用,所以如果实现正确,它们应该在调用下一个函数之前等待结果返回。我可以推荐查看此答案,了解有关实现wrapAsync的正确方法和处理错误的更多信息。 p>

UPDATE



我认为这很有意思,因为这篇文章已经越来越长。看看这个例子我上传到Github ,它可能会给你所需的答案。


那么,从http调用总是认为是错误对象的错误对象是什么?




好吧,fibers / future假定标准节点回调作为最后一个参数,错误对象作为第一个参数,结果对象作为第二个参数。


如果是这样,如何处理我可以附加另一个字段,以便错误对我有意义吗?


不要确定你在这里想什么,我的例子在Github。在那里我创建一个错误对象。如果你不是使用setTimeout(其中我只是模拟一个异步调用),构造函数相同的方式,从远程调用捕获错误对象,追加一个字段,或更改err.message更有意义的东西,然后调用回调我认为应该工作,但我没有测试只是这样。


如果有错误,但没有错误对象传递,什么是节点约定来创建一个可以返回的错误对象?


再次查看示例。 wrapAsync仅在服务器上工作,它假定您调用的函数具有作为最后(或唯一)参数的回调。该回调应该有一个错误作为第一个参数,一个结果作为第二个。大多数节点函数(如果不是全部)将遵守那些规则。



如果由于某种原因想要自己处理回调,你需要让shure手动调用回调,并传入null作为第一个参数,如果有是没有错误,或者创建一个错误对象并将其作为第一个参数传递,如果有错误。在示例中查看wrapAsyncTest.js。


How do I stop the function at the point an error occurred.

I am making an http post call. I have constructed an error-first style callback function.

How do I stop the callback function if an Error object was passed in it? In my use case, I need to stop regardless of error type as I need the data object for subsequent chain of callback functions.

Is it simply return? I have read what return does on a higher level so hoping to get confirmation if my code is right for what I would like to achieve.

I just want to ensure the proceeding callback functions don't continue as they wont be passed any data objects, therefore, will all be 'errors'

server.js

var request = function(callback) {
  // HTTP call to server
      },  
      function (error, response, body) {    
      if (!error && response.statusCode === 200) {
        // processed and created data object
          return callback(null, data)
      }
      else {
         console.log(error.stack)
         return callback(error) ;
      }
    })
};
var requestSync = Meteor.wrapAsync(request);


var callback = function(error, data, callback) {
  if (error) {
    // How do I properly stop this function?
     return callback(new Error('custom message I send to client'))
  }
  else {
    // doing something with data object..
  }
}

The functions are invoked within my method.

try {
  var resultOne = requestSync()
  var resultTwo = callback(resultOne)
  // more async callback functions will reside here
} catch (e) {
  console.error('error occured somehwere = ' + e);
} finally {
//
}

解决方案

I haven't seen exactly this way of coding before, so it's difficult to comment if this is the correct way without knowing a bit more about what you are really trying to do. I assume that the block:

try {
  var resultOne = requestSync()
  var resultTwo = callback(resultOne)
  // more async callback functions will reside here
} catch (e) {
  console.error('error occured somehwere = ' + e);
} finally {
}

...calls another "callback" function than in the first code snippet (or else you are passing the resultOne in as the first parameter, and the callback function will read this as a valid error object and throw an error).

However, to answer your concern if the code will continue running if one of the functions throws an error, the answer is no - it will shift directly to the catch block. Have a look at: this documentation at MDN

Especially this part:

A catch clause contain statements that specify what to do if an exception is thrown in the try block. That is, you want the try block to succeed, and if it does not succeed, you want control to pass to the catch block. If any statement within the try block (or in a function called from within the try block) throws an exception, control immediately shifts to the catch clause. If no exception is thrown in the try block, the catch clause is skipped.

I hope this answers your primary question. Since you seem to wrap all async calls in Meteor.wrapAsync() they should wait for the result to return before calling the next function if implemented correctly. I can recommend to have a look at this answer for more information on the correct way of implementing wrapAsync and handling errors.

UPDATE

I thought this was interesting to make an example out of since this post already is getting long. Have a look at this example i uploaded to Github, it might give you the answers you need.

So is the 'error' object passed back from the http call always recognised as an error object?

Well, fibers/future assumes a standard node callback as the last argument with an error object as the first parameter and a result object as the second. As long as this is the case the error will get caught in the "catch" block and the execution will stop.

If so, how can I append another field so the error makes sense for me?

Not shure exactly what you think of here, but have a look at my example on Github. There i create an error object. If you instead of using setTimeout (where i just simulate an async call), construct your function the same way, catch the error object from the remote call, append a field to it or change the err.message to something more meaningful to you, and then call the callback I think that should work but I haven't tested just that.

If there is an error but no error object is passed, what is the node convention to create an error object that I can return?

See the example again. wrapAsync only work on the server, it assumes the function you call it on has a callback as the last (or only) parameter. That callback should have an error as the first parameter, and a result as the second. Most node functions (if not all) will comply to those rules.

If you for some reason want to handle the callback yourself, you need to make shure to call the callback manually, and pass in "null" as the first parameter if there is no error, or create an error object and pass that in as the first parameter if there is an error. Look at wrapAsyncTest.js in the example.

这篇关于当接收到错误对象时,如何停止函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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