I have html page which causes js error and an global handler for it.
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<script type="text/javascript" src="js/jquery-3.1.0.min.js"></script>
<script type="text/javascript">
$(function () {
window.onerror = function (errorMsg, url, lineNumber) {
alert('Error: ' + errorMsg + ' Script: ' + url + ' Line: ' + lineNumber);
}
throw new Error("this is error");
});
</script>
</head>
<body>
<div>Test page</div>
</body>
</html>
I receive alert: "Error: Script error. Script: Line: 0". As you can see error info is missing. How can I get this info here like I can see errors in developer tools. Any extensions for chrome are not suitable. I must catch errors with their info in javascript.
When a syntax(?) error occurs in a script, loaded from a different origin, the details of the syntax error are not reported to prevent leaking information (see bug 363897). Instead the error reported is simply "Script error." This behavior can be overriden in some browsers using the crossorigin attribute on and having the server send the appropriate CORS HTTP response headers. A workaround is to isolate "Script error." and handle it knowing that the error detail is only viewable in the browser console and not accessible via JavaScript.
I also ran into this. See notes section here: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror
This implies the development setup might need some adjustment to avoid this policy issue. I'm merely guessing here, so don't take it for granted.
I am just adding answer for the sake of those who came here looking.
Source: https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onerror
Please go this URL, as you can see there are five parameters to
window.onerror = function(message, source, lineno, colno, error) { ... }
the last one is the one you need, it has complete record of what you see in console in case of error.
message: error message (string). Available as event (sic!) in HTML onerror="" handler.
source: URL of the script where the error was raised (string)
lineno: Line number where error was raised (number)
colno: Column number for the line where the error occurred (number)
error: Error Object (object)
Then Error Object has two properties
message
stack
stack is the one which will contain all the information you required.
use console.log(errorMsg, url, lineNumber)instead of alert to get more readable log. You can see the log in browser developer console. Also you can check the use of try..catch for better error handling.
use jquery script with anonymous attirbute, and the script server response header with [access-control-allow-origin:*]. Your error throw code is runned with jquery callback.
errorMsg is NOT the message "this is error"; it is Script error., which is precisely what you're seeing. You simply don't get the error message that way. onerror is unsuited to replace a try-catch block.
Related
Almost every JS error I get using window.onerror is the unhelpful "Script error" message.
I have read everything I can find on this but it is always said it is caused by cross-site scripting, but there's none in my code.
Why do these unhelpful error messages appear and how can I replace them with the full messages that appear in the console?
As a test example, I created a minimal HTML/JS code pair. This is the HTML page, testpage.html:
<html>
<head></head>
<body>
<script src="/js/testpage.min.js"></script>
</body>
</html>
and JS script:
window.onerror = function(msg, url, lineNo, columnNo, error) {
// code goes here to generate log entry on the server
return true;
}
// generate illustrative sample error
var jsonData = "";
var data = JSON.parse(jsonData);
Running this, msg = "Script error" and other parameters are either null or zero. The Google DevTools console displays the full error:
Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at testpage.min.js:7
Any help towards being able to log the full error messages will be most welcome. I'm at a complete loss.
EDIT: Recast to make problem clearer.
First: I have my server set to catch and log all client JS errors, and display an alert() anytime a runtime JS error is encountered. Ex:
<script type="text/javascript">
window.onerror = function() {
alert("Error caught");
};
</script>
If a user has the DOM Explorer open in IE and closes the window, they get an "Unspecified error" alert after the window closes. There is no source file listed in the error, but it happens on every page that sources JavaScript. The client and the server both recognize the error.
This is the last error I'm getting; all the JavaScript, HTML, and CSS work/display correctly on every browser, but IE has this last issue. This happens in IE5, IE7 and IE11(Edge).
What else can I provide to help diagnose?
Edit:: Code is as follows.
(function(window) {
function CatchRuntimeJSError(message, filename, lineno, colno, error) {
Log(filename + ":" + lineno, message);
var err = "You've had a run-time JavaScript Error.";
alert(err);
}
window.onerror = CatchRuntimeJSError;
})(window);
This code snippet is in "Errors.js" and is the last thing included by a page.
The handler values are as follows:
message: "Unspecified error"
filename: the URL of the page if static, blank if generated by other code via cgi.
lineno: 1
colno: 1
error: "Error: unspecified error."
When passing event as a parameter, event is undefined.
When I have uncaught errors in my angular applications the console message looks like this:
Error: [$injector:modulerr] http://errors.angularjs.org/1.4.3/$injector/modulerr?p0=app&p1=%5B%24injector%3Aunpr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1.4.3%2F%24injector%2Funpr%3Fp0%3DENVIRONMENT%0AJ%2F%3C%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A11%3A416%0Aeb%2Fr.%24injector%3C%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A45%3A375%0Ad%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A43%3A364%0A%40http%3A%2F%2Flocalhost%3A8600%2Fapp%2Fapp.js%3A42%3A18%0Ae%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A44%3A154%0Ad%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A42%3A320%0Ag%2F%3C%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A42%3A452%0Am%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A12%3A320%0Ag%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A42%3A229%0Aeb%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A45%3A1%0AAc%2Fd%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A24%3A339%0AAc%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A25%3A151%0AZd%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A23%3A464%0A%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A294%3A428%0An.Callbacks%2Fj%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A2%3A26920%0An.Callbacks%2Fk.fireWith%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A2%3A27738%0A.ready%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A2%3A29530%0AI%40http%3A%2F%2Flocalhost%3A8600%2Flibs.js%3A2%3A29721%0AnrWrapper%40http%3A%2F%2Flocalhost%3A8600%2F%23%2Fdashboard%3A15%3A12749%0A
Which is not useful, but if I url_decode it it is much better, like this:
http://errors.angularjs.org/1.4.3/$injector/modulerr?p0=app&p1=[$injector:unpr] http://errors.angularjs.org/1.4.3/$injector/unpr?p0=ENVIRONMENT
J/<#http://localhost:8600/libs.js:11:416
eb/r.$injector<#http://localhost:8600/libs.js:45:375
d#http://localhost:8600/libs.js:43:364
#http://localhost:8600/app/app.js:42:18
e#http://localhost:8600/libs.js:44:154
d#http://localhost:8600/libs.js:42:320
g/<#http://localhost:8600/libs.js:42:452
m#http://localhost:8600/libs.js:12:320
g#http://localhost:8600/libs.js:42:229
eb#http://localhost:8600/libs.js:45:1
Ac/d#http://localhost:8600/libs.js:24:339
Ac#http://localhost:8600/libs.js:25:151
Zd#http://localhost:8600/libs.js:23:464
#http://localhost:8600/libs.js:294:428
n.Callbacks/j#http://localhost:8600/libs.js:2:26920
n.Callbacks/k.fireWith#http://localhost:8600/libs.js:2:27738
.ready#http://localhost:8600/libs.js:2:29530
I#http://localhost:8600/libs.js:2:29721
nrWrapper#http://localhost:8600/#/dashboard:15:12749
How can I get angular to output errors this way by default? Thanks.
It's url encoded because it's a link. Click it! :D
It will bring you to a webpage that explains the error.
This is your error
If you go to that page and look at the error it will say that it threw an error because of x error. Then that error will be a link to, so click that as well.
And now that you've gotten to that last page, the error is that you have an unknown provider called ENVIRONMENT.
You can override exception handler:
angular.module('exceptionOverride', []).factory('$exceptionHandler', function() {
return function(exception, cause) {
exception.message += ' (caused by "' + cause + '")';
throw exception;
};
});
More here
I am getting an error which seems to be originating from one of the page scripts.
file://cdn.widgets.webengage.com/js/widget/webengage-min-v-3.0.js:1
return c.apply(undefined,a)}catch(d){webengage.eLog(d);if(!b){throw d}}}else{t
^
TypeError: Cannot call method 'call' of undefined
at new exports.NOT_IMPLEMENTED (/usr/local/lib/node_modules/jsdom/lib/jsdom/browser/utils.js:9:13)
at Object.webengage.eLog (file://cdn.widgets.webengage.com/js/widget/webengage-min-v-3.0.js:1:366)
at t.extend.u (file://cdn.widgets.webengage.com/js/widget/webengage-min-v-3.0.js:1:19160)
at Object.t.extend.error (file://cdn.widgets.webengage.com/js/widget/webengage-min-v-3.0.js:1:19299)
at file://cdn.widgets.webengage.com/js/widget/webengage-min-v-3.0.js:1:66518
at Object.webengage.withELog (file://cdn.widgets.webengage.com/js/widget/webengage-min-v-3.0.js:1:640)
at Timer.<anonymous> (file://cdn.widgets.webengage.com/js/widget/webengage-min-v-3.0.js:1:937)
at Timer.exports.setInterval.timer.ontimeout (timers.js:234:14)
I am getting this error from the following code:
try {
var document = jsdom.jsdom(str, null, {});
} catch(e) {
console.log("Got ERROR...");
console.log(e);
}
console.log("Page Document Loaded.");
var window = document.parentWindow;
//console.log(window.document.innerHTML);
console.log(window.innerWidth);
console.log(typeof window.document.getElementsByClassName);
here str is the html that i got earlier. I am able to see printed results, the html the innerWidth and the typeof getElementsByClassName i.e.. function. But after around 20 seconds i am getting the above error and my application crashes, without printing Got ERROR... from above.
The first question that i have is why is my application still running after printing the last thing. Is this a normal behavior for jsdom that it keeps on running like how in a browser a script keeps on running until window.close() is given.
The actual problem is, how can i resolve this. I want to trigger few events in this window object and interact with it but it keeps on crashing.
The url for the script is a little confusing:
file://cdn.widgets.webengage.com/js/widget/webengage-min-v-3.0.js:1
this is because the page is fetching this script resource in an ajax call with the url:
//cdn.widgets.webengage.com/js/widget/webengage-min-v-3.0.js:1
although jsdom is able to get the resource, i checked that this script has that line where it is showing the error.
I try to eleminate an 404 error occuring because the source (src) is missing..
var $chart = $("<img />")
.addClass("trend-pic")
.error(function(){
console.log("error loading..")
});
try{
$chart.attr("src", jobs[counter].url + "test/trend")
}catch(err){
$chart.attr("src", "");
}
if tried many stuff to catch the error i.e. putting an .error(function(){}) at the end.
use the $chart.load() - method to check if the images gets loaded?
Non of those helped?
GET {myURLString} 404 (Not Found)
Browser: Safari
You can't really delete those 404 errors from the console.
The best you can do is make some ajax calls and see the return code, but then you'll be limited to request only to your own domain.
EDIT--
Oh, and yes, those errors will keep showing in the "Requests" tab! They just won't appear in the "Console" tab (in Chrome).
I think there is no way to eliminate the error from your console, except using the right url.
In your code with the try {} catch() the error is thrown and $chart.attr will be called again with an empty string.
What you can do is add a check like this before you set the .attr()
if (jobs[counter].url !== void 0 &&
jobs[counter].url.length !== 0) {
$chart.attr("src", jobs[counter].url + "test/trend")
}
so you can remove the try{} catch()
Hope it helps.
Just the answer is No.Because this is the only way Server send "there is a Error".
Refer this one.
Prevent 404 Error in console
404 is not an error. It is the way server says it does not have the source referred by the client.