简体   繁体   English

AngularJS中的promise会捕获每个异常/错误吗?

[英]Do promises in AngularJS catch every exception/error?

I've inherited a codebase at work that contains a dozen or so examples of the following pattern: 我在工作中继承了一个包含以下模式的十几个示例的代码库:

var promise = null;
try {
    promise = backendService.getResults(input);
}
catch (exception) {
    console.err(exception);
}
if (promise !== null) {
    promise.then(function (response) {
        // do stuff
    })
    .catch(function (error) {
        console.err(error);
    });
}

Where backendService is an Angular service that in turn calls a REST service through $http . 其中backendService是一个Angular服务,后者又通过$http调用REST服务。

So here's my question: is that try/catch really necessary? 所以这是我的问题:try / catch真的有必要吗? Will there ever be any scenario where a particular error/exception is thrown that the promise's .catch fails to catch? 是否会出现一个特定错误/异常的情况,即promise的.catch无法捕获?

This has been the subject of a bit of debate on the team all morning, and the only resolution we've come up with is that we don't think it's necessary, but (a) changing it breaks the tests that were written alongside it (which would also need to be changed), and (b) well... it's defensive coding, right? 这已经有点对球队辩论的整个上午的主题,我们已经拿出了唯一的解决办法是,我们不认为这是必要的,但(一)改变它打破写沿着它的测试(这也需要改变),(b)嗯......这是防御性编码,对吧? It's not a Bad Thing. 这不是一件坏事。

The merits of actually bothering to refactor it into oblivion when there are more important things to do aren't what I'm asking about, though. 然而,当有更重要的事情要做的时候,实际上有必要将它重构为遗忘的优点并不是我所要求的。 I just want to know if it's a reasonable pattern when promises are being passed around like this (in AngularJS specifically, if that makes a difference), or just paranoia. 我只是想知道这是一个合理的模式,当承诺被传递这样(特别是在AngularJS,如果这有所不同),或只是偏执狂。

Do promises in AngularJS catch every exception/error? AngularJS中的promise会捕获每个异常/错误吗?

No. Only exceptions that are thrown from inside then / catch callbacks are automatically caught. 序号只有从里面抛出的异常then / catch 回调会被自动捕获。 All errors happening outside of them will need to be handled explicitly. 发生在它们之外的所有错误都需要明确处理。

Will there ever be any scenario where a particular error/exception is thrown that the promise's .catch fails to catch? 是否会出现一个特定错误/异常的情况,即promise的.catch无法捕获?

Yes. 是。 That backendService.getResults(input) call might not return a promise, but it can throw an exception. backendService.getResults(input)调用可能不会返回 promise,但它可能会抛出异常。 Or it doesn't even get that far when backendService is null, and you'll get a ReferenceError or .getResults is not a function and you'll get a TypeError . 或者,当backendService为null时,它甚至没有那么远,你将得到一个ReferenceError.getResults不是一个函数,你会得到一个TypeError

is that try/catch really necessary? 是try / catch真的有必要吗?

Not really. 并不是的。 In the latter case, that your code has a grave mistake, you probably don't care to throw and crash. 在后一种情况下,你的代码有一个严重的错误,你可能不关心抛出和崩溃。 The former case, that backendService.getResults(input) throws, is heavily despised. 前一种情况,即backendService.getResults(input)抛出,被严重鄙视。 Asynchronous functions should never throw but only return promises - exactly for the reason that you don't have to write two error handling statements then. 异步函数永远不应该throw但只返回promise - 正是因为你不必编写两个错误处理语句。

well... it's defensive coding, right? 嗯......这是防御性的编码,对吗? It's not a Bad Thing. 这不是一件坏事。

Yeah, pretty much. 是的,差不多。 But it is questionable here. 但这里有疑问。 A synchronous exceptions is really unexpected here, not just a service whose failure can be handled. 这里的同步异常实际上意外的 ,而不仅仅是可以处理其故障的服务。 The logging message in the catch block should signify that. catch块中的日志消息应表示这一点。

Notice that it also isn't defensive enough. 请注意,它也不够防御。 It doesn't catch the probably more likely mistake where getResults() does return, but something that is not a promise. 它没有捕获getResults()确实返回的可能更可能的错误,而是一些不是promise的错误。 Calling .then() on that might throw. 在那上面调用.then()可能会抛出。 Similarly, the if (promise !== null) is dubious, as it hides when null is returned erroneously (we really could need try-catch-else here). 类似地, if (promise !== null)是可疑的,因为它在错误地返回null时隐藏(我们真的需要try-catch-else )。

is that try/catch really necessary? 是try / catch真的有必要吗?

Not really. 并不是的。 As long as your backendService.getResults() returns a promise. 只要你的backendService.getResults()返回一个promise。 Like return $http(...) 喜欢return $http(...)

Will there ever be any scenario where a particular error/exception is thrown that the promise's .catch fails to catch? 是否会出现一个特定错误/异常的情况,即promise的.catch无法捕获?

I don't think so, since any error will be a rejected promise, it will fall into your .catch() handler 我不这么认为,因为任何错误将被拒绝承诺,它将落入你的.catch()处理程序

well... it's defensive coding, right? 嗯......这是防御性的编码,对吗? It's not a Bad Thing. 这不是一件坏事。

It depends... Javascript try/catch is has some performance issues. 这取决于... Javascript try / catch有一些性能问题。 So if you're using just for the sake of making sure, you could remove it :) 因此,如果你只是为了确保使用,你可以删除它:)

Take a further look at try-catch discussion here if you wish: Javascript Try-Catch Performance Vs. 如果您愿意, 在此处进一步了解try-catch讨论: Javascript Try-Catch Performance Vs. Error Checking Code 检查代码时出错

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

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