问题
I have an error in my error handler :) Uncaught TypeError: Converting circular structure to JSON
.
I suspect that there is another exception thrown inside the JSON.stringify
call and that is what is causing it? I certainly can't see how err_msg
, err_url
, or err_line
could refer to themselves somehow.
Is there a way I can protect this code from becoming circular? Does anyone have some insight into exactly why this error is arising?
window.onerror = function myErrorHandler(err_msg, err_url, err_line) {
var xhr = new XMLHttpRequest();
xhr.open("POST", "/log-receiver", true);
xhr.setRequestHeader('X-Csrftoken', document.getElementsByName("csrfmiddlewaretoken")[0].value);
xhr.setRequestHeader('Content-Type', "application/json;charset=UTF-8");
xhr.send(JSON.stringify({
message: err_msg,
url: err_url,
line: err_line,
level: 'ERROR'
}));
return false;
}
回答1:
In case anyone else encounters this. It turned out that very occasionally a browser was sending an ErrorEvent
object as the argument to window.onerror
. A added a check to my error handler to deal with this case.
来源:https://stackoverflow.com/questions/60937599/difficult-to-diagnose-circular-json