如果 statusCode 不是 200,则从 http 调用返回什么类型的错误对象?
What type of error object is retuned from a http call if statusCode is not 200?
我正在使用 npm 'request' 包在 meteor 中进行 http 调用。
我想知道当 response.statusCode 不等于 200 时会创建什么类型的错误对象?这是由 javascript 引擎处理并被视为 运行时错误 ?
或者如果 statusCode 不等于 200 由服务提供商决定他们是否会创建错误对象?
如果是后者,我创建一个'manual error',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) 块会捕获此错误,并且我想添加我自己的消息,包括堆栈跟踪。
除非操作使用同步代码(例如使用 meteor http 包使用 http.get
发出请求),否则您不必使用 try..catch
。
错误码一般是对方服务器返回的。 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 Error
或 new Meteor.Error
。不同之处在于,如果在使用 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 的代码。如果它在其中抛出错误,它会在堆栈中冒泡并找到到达客户端的方式。
我正在使用 npm 'request' 包在 meteor 中进行 http 调用。
我想知道当 response.statusCode 不等于 200 时会创建什么类型的错误对象?这是由 javascript 引擎处理并被视为 运行时错误 ?
或者如果 statusCode 不等于 200 由服务提供商决定他们是否会创建错误对象?
如果是后者,我创建一个'manual error',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) 块会捕获此错误,并且我想添加我自己的消息,包括堆栈跟踪。
除非操作使用同步代码(例如使用 meteor http 包使用 http.get
发出请求),否则您不必使用 try..catch
。
错误码一般是对方服务器返回的。 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 Error
或 new Meteor.Error
。不同之处在于,如果在使用 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 的代码。如果它在其中抛出错误,它会在堆栈中冒泡并找到到达客户端的方式。