【问题标题】:What type of error object is retuned from a http call if statusCode is not 200?如果状态码不是 200,http 调用会返回什么类型的错误对象?
【发布时间】:2015-05-01 04:37:26
【问题描述】:

我正在使用 npm 'request' 包在流星中进行 http 调用。

我想知道当 response.statusCode 不等于 200 时会创建什么类型的错误对象?这是否由 javascript 引擎处理并被视为 运行时错误

或者如果statusCode不等于200由服务提供者决定是否会创建一个错误对象?

如果是后者,我是否会创建一个“手动错误”,new Error('throwing my own error for the catch block to handle')

var request = function() {
// HTTP call to server
  },  
  function (error, response, body) {    
  if (!error && response.statusCode === 200) {
    // processed and created data object
      return data
  }
  else {
     return (error) ; // How can I ensure the catch(e) block in the try/catch method will catch the error with an addition of my own 'message'. 
  }
})
};

try {
    request()
}
catch(e) {
  console.log(e)
}

对于我的场景,我想确保 catch(e) 块能够捕获此错误,并且我想添加我自己的消息,包括堆栈跟踪。

【问题讨论】:

  • 记住 XMLHttpRequestasynchronous,如果有错误,你会得到 error 回调,用@ 987654323@等于请求。

标签: javascript node.js api meteor error-handling


【解决方案1】:

您不必使用try..catch,除非操作使用同步代码(例如使用meteor http 包使用http.get 发出请求)。

错误码一般是对方服务器返回的。 200 表示没问题。有一个huge list of potential 错误代码及其含义。

因此状态码将由error 对象中的回调返回,但不会被catch 捕获,除非它像您提到的那样使用throw 抛出。

在 javascript 中,问题是当有带有错误参数的回调时,它实际上并没有抛出可以从触发原始函数的位置捕获的错误(您拥有的 request() 方法),如果这有意义的话。

即使您在回调中使用throw ,它也不会被catch 捕获。这是因为回调是作为一个全新的事件触发的。如果您想使用catch 技术,您需要同步javascript。即使用 http 包。

meteor add http

然后是代码:

try {
    var result = HTTP.get("<url>")
    result.content //this will contain the response
}catch(e) {
    the `e` object will contain the error (if the status code is not 200)
}

您可以通过在 catch(e) {...} 块中添加自己的错误来更改错误,即

...
catch(e) {
    throw new Meteor.Error(500, "Internal Server Error");
}
...

您可以抛出 new Errornew Meteor.Error。不同之处在于 Meteor.Error 也会将错误作为err 回调的一部分发送给客户端,如果这段代码正在使用Meteor.call 从客户端调用的方法中运行,例如

Meteor.call("something", function(err, result) {
    if(err) alert(err.reason);
});

如果调用这样的方法:

Meteor.methods({
    something: function() {
        throw new Meteor.Error(500, "This is the message");
    }
});

客户端将显示一个消息框,将This is the message 称为err.reason。如果你抛出new Error(..,它不会将消息发送到客户端,而是消息将是Internal Server Error而不是This is the message

在该方法中,您可以使用请求 url 的代码。如果它在其中抛出错误,它将冒泡堆栈并找到通往客户端的方式。

【讨论】:

  • 我还应该补充一点,我使用了未来来使我的函数看起来是同步的。然后我一个接一个地运行这些函数,将前一个函数的返回结果传递给下一个函数。从技术上讲,我没有使用回调函数,而只是将结果从一个函数传递到下一个函数。处理错误的好习惯是什么?我之前的问题通过 try/catch 演示了这一点,但我没有使用回调...我“返回 future.return(resultObj)”stackoverflow.com/questions/28703715/…
  • 感谢您指出 Meteor.Error,因为我的函数是在 Meteor.call/method 中执行的。所以我想将错误传递给客户端。
  • @CodeCandy 是的,我认为这是最好的方法。对同一件事使用 futures/Meteor.wrapAsync 帐户,因为它会在触发错误时抛出错误。 throw 的好处是它可以阻止进一步的代码执行。我会小心这一点,尽管它需要考虑到执行可能会在中途停止——例如不利于金融交易和支付
  • 这是我的问题的最后一个扩展,为什么在我的代码中“抛出”一个意外的标记? .."return future.return (throw new Meteor.Error("request is not a 200 response"));"
  • @CodeCandy 您不必返回错误,自行抛出错误。 throw new Meteor.Error(500, "Error"); return future.return(null)
猜你喜欢
  • 2021-10-23
  • 2023-03-12
  • 2017-04-23
  • 2013-10-25
  • 2018-05-28
  • 2015-05-12
  • 2021-10-21
  • 2015-10-13
  • 2012-07-31
相关资源
最近更新 更多