Difficult to diagnose circular JSON

假如想象 提交于 2021-02-11 14:37:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!