admin管理员组文章数量:1414930
When run javascript app integrating with some 3rd party library, in some cases , 3rd party lib will lead to bug , for example, following typical error:
Uncaught (in promise) TypeError: Cannot read property 'length' of undefined
The above error is ok to some extend because their code quality is not well(good code should always check if object exist,array length>0 etc before access them), what i concern is: i do not want the js app functions as if they crashed and not work anymore, even if the root cause which should cause above error disappear(for example, the array has been there now), the app will never survive to run again.
Is there some method to force js app to run even after 3rd party code fail(or my own code fail) but should SURVIVE to run for some other function area(which has no the crash causing there), or survive to work again if the cause for above exception disapper?
Please NOTE THAT, I DO NOT WANT TO TOUCH 3RD party code to let this work
Is there a generic mechanism in javascript to be switched on : just skip the un-caught error and continue to run as normal and NOT CRASH the app?
When run javascript app integrating with some 3rd party library, in some cases , 3rd party lib will lead to bug , for example, following typical error:
Uncaught (in promise) TypeError: Cannot read property 'length' of undefined
The above error is ok to some extend because their code quality is not well(good code should always check if object exist,array length>0 etc before access them), what i concern is: i do not want the js app functions as if they crashed and not work anymore, even if the root cause which should cause above error disappear(for example, the array has been there now), the app will never survive to run again.
Is there some method to force js app to run even after 3rd party code fail(or my own code fail) but should SURVIVE to run for some other function area(which has no the crash causing there), or survive to work again if the cause for above exception disapper?
Please NOTE THAT, I DO NOT WANT TO TOUCH 3RD party code to let this work
Is there a generic mechanism in javascript to be switched on : just skip the un-caught error and continue to run as normal and NOT CRASH the app?
-
1
wrap the API that make's call to the library with
try{} catch{}
– NiRUS Commented Oct 8, 2016 at 7:30 - Yes, try catch is ok in one specific place where we know possible bug will be there. But sometime, it is not convinient to wrap hundreds of function call like that. Even worse, i use many libs in app, i can not know where such kind of error will happen. Is there a generic mechanism in javascript to be switched on : skip the un-caught error and continue to run as normal? – kidsit Commented Oct 8, 2016 at 8:21
- In answer to your question above - No. There is no guarantee that an app will be in a suitable state to keep running after an uncaught error occurs - so there isn't really any way that you can ensure that it 'just keeps running' after an error. – tebs1200 Commented Oct 8, 2016 at 8:27
-
Why would you even allow the error in code. Try to fix it. You can ignore warnings but why to ignore error. Error means something wrong and need to be fixed or handled. Error prone code imo is bad practice. If you still want to ignore. Try to move all your code inside one
try-catch
. – NiRUS Commented Oct 8, 2016 at 13:48 - @Nirus, your are right, error should be fixed. But in some situation, the error is caused by temp un-met running condition. Later on, that condition will be met, so the code can run later on. Unfortunately, the code crash before they have chance to run. For my own code, i can add more check and protect for known failure condition, for 3rd party code, maybe there are many such failure cases, I am asking whether or not there is a generic mechanism in javascript: just skip the error and continue to run as if try ... catch for all failure code . It seems there is no such mechanism. – kidsit Commented Dec 28, 2016 at 5:58
2 Answers
Reset to default 1As Nirus said, wrapping everything that could cause an error in a try...catch block:
try{
callYourLibHere();
}
Block should allow the code to ignore errors thrown, which will make the code continue to run after an error occurs. However, this will hide EVERY error inside this block. We can fix this by logging the errors into the console:
try {
callYourLibHere();
}
catch (e) {
console.log(e);
}
Please note that trying to solve the errors instead of ignoring them would be the better way to fix this.
If you don't want to use try-catch
process.on('uncaughtException', err => {
console.error('There was an uncaught error', err)
process.exit(1) //mandatory (as per the Node.js docs)
})
If you don't exit the process, It can cause unexpected behaviour in your application which's why it remended.
Further reading
本文标签: javascripthow to force js ignore uncaught error and continue to workStack Overflow
版权声明:本文标题:javascript - how to force js ignore uncaught error and continue to work? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745178393a2646351.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论