【发布时间】: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) 块能够捕获此错误,并且我想添加我自己的消息,包括堆栈跟踪。
【问题讨论】:
-
记住 XMLHttpRequest 是 asynchronous,如果有错误,你会得到 error 回调,用@ 987654323@等于请求。
标签: javascript node.js api meteor error-handling