My Situation
Hi, Im relatively new to Javascript so my question could be really easy. Im developing multiple webapps for my company. One problem I run in regularly are Javascript-errors. I know how to handly them using try/catch.
What I want to do
I want to either write a logfile on the server or give the user something they can send me without any knowledge of debugging. This means, the user has to be informed that an error occurred in both cases.
What I already did
One idea I had was using try catch and using code I found here: https://stackoverflow.com/a/6055620/3581748 to give the user the possibility to send me the stacktrace.
Example:
<button id="demo" onClick="errorHandling()">Produce error</button>
<script>
function errorHandling() {
try {
document.getElementById("somethingNotExisting").value * 2;
} catch (_err) {
window.prompt("Copy to clipboard: Ctrl+C, Enter", _err.stack);
}
}
</script>
Or here: https://jsfiddle.net/n79xv6nt/
Which works most times.
The Problem
I sometimes embed scriptfiles in my "main"-page using jQuery. Example:
<div id="forScript"></div>
<script>
$("#forScript").load('scripts/additionalScript.php');
</script>
If I use the above code (the one below "What I already did"). I dont get a stacktrace that tells me where the error occured. It instead points to a jQuery-file. The Google Chrome Console shows the real stacktrace. Another problem is, how do I get to the file after I got the linenumber? In my IDE the lines are different because there is php in between.
My Question
Is it possible to get a good errormessage for this case? (preferably without having to throw an error myself) How can I access the additional script and see the same linenumbers that chrome is seeing? And how would you notify the user/log the error?