I have a lot of JavaScript code that get's executed on a website. If every plugin works, everything is fine. But when only one JavaScript error occurs, all other code fails, too.
I am concatenating all JavaScript code to one file. This is currently a must, so a change here is not possible.
What's the general way of preventing (vendor) code not fail everything else?
EDIT:
To simplify my question: What's the best way of handling big JavaScript error from vendor code?
Your error handling should be as simple as the following example:
try{
var x = functionThatDoesNotExist();
}
catch(error){
console.log('An error occured');
console.log(error);
}
You should try and break down your javascript files according to the templates that contain your HTML markup, and simply when an error occurs do not render that template.
You can read more on Try/Catch here.
Related
Is there a static code analysis tool that would tell me that there are places in my code that don't handle possible error events?
I had an important script crash because I used new WebSocket without registering an on('error') - even though the place where the websocket was created was in a try/catch block. And I'd like to prevent similar situations again.
Not directly answering your question about static analysis -- which always has limits -- but you can prevent crashes like this from bringing down your service by adding a catch-all:
process.on('uncaughtException', (err) => {
console.error(`**** Caught exception: ${err}:`, err.stack);
});
Clearly, you should try hard not to rely on this, because the code blocks where such exceptions are thrown will not complete as intended. But it's a useful safe-guard to have to avoid downtime.
Two aspects of my project's Javascript are interfering with each other. Not sure why, or how to resolve.
I have the following in my code, in order to allow for a URL hashtag action:
function getHashValue(key) {
return location.hash.match(new RegExp(key+'=([^&]*)'))[1];
}
var hash = getHashValue('hash');
console.log(hash);
I'm also running video.js and bigvideo.js within my Rails project. For some reason, the javascript code above prevents the other javascript (my bigvideo implementation) from functioning. Why? How do I resolve?
My bigvideo implementation is described here: https://stackoverflow.com/a/17581187/1318135
It seems possible that getHashValue is throwing an exception (array limit exceeded?) which could interfere prevent other code on the thread from running. Try enclosing the return statement in a try/catch.
Here's what you get: TypeError (exception) Cannot read property '1' of null.
Catch the exception and you'll be okay, I think.
I know this is a little to less to get an answer on what the problem is so what I ask is how to debug it.
I get the following error (the image below). No line, script or anything specified. Also except the ones in jQuery and raphaeljs libraries I don't have any custom error handler defined.
Got any ideas on how to debug this?
(The main script for example has around 3k lines and since I don't know where the error occurs I don't know witch part of it to post. I need only a way to find that.)
Thank you for your time.
This happens when the script throws a string, rather than a proper exception, like:
throw 'Error in protected function: )55';
See this other SO question for possible solutions:
How can I get a Javascript stack trace when I throw an exception?
Try chrome. Webkit can provide stack traces:
Web Inspector: Understanding Stack Traces
Sample:
<script>
function i2(){
throw "CustomError";
}
function invoke(){
i2();
}
</script>
<button onclick="invoke()">yo</button>
local function ensureAnimDict(animDict)
if not HasAnimDictLoaded(animDict) then
RequestAnimDict(animDict)
while not HasAnimDictLoaded(animDict) do
Wait(0)
end
return animDict
end
I'm trying to make a PoC of reflected Cross-Site Scripting on a website that I'm testing right now. I've found a place inside of a Javascript code where commands can be injected, however the trouble is that there the previous block of code throws a 'not defined' error and therefore (at least I think so) my injected code is not executed. Is there any chance to execute the code anyway?
Here is the code:
UndefinedObject.Init({
Var1:"a",
Var2:"b",
Var3:"can_be_injected_with_JS_code")}
I can't inject any HTML tags as these are filtered by the application.
Many thanks!
Wrap them under try catch block.
In a sequence of execution, if the code fails, the remaining part will not be executed. Javascript errors ("Exceptions") can be caught using try...catch (if you are able to inject this try - catch also).
If there is a different flow (via another event), the code will continue.
You can either try using a try-catch, or if that won't help, try using window.onerror
Generally the right way of doing that is using try-catch-finally or try-finally:
If you make something about the error - log or do something else. Catch may be also used to execute your code, but not a good practice. You can do nothing about the error if you want, that`s why finally is used.
Finally is used when it is important to execute a piece of code, no matter if an error is thrown or not. For example in C++ or other language when you work with files inside finally the file is closed ( you can not leave it opened ). Look here for some examples.
Is there some way to wrap the entire page in a try/catch, so that I can catch any error from any script that is executing?
Use window.onerror instead of a big try/catch.
You could actually do some useful things in the error handler, like posting the error information to your server so you know when things are breaking on your page.
Turn on the debugger and break on error.
You don't want to catch every error in the entire page on production code. That's horrible.