Programing

모든 자바 스크립트 처리되지 않은 예외 잡기

lottogame 2020. 8. 28. 07:47
반응형

모든 자바 스크립트 처리되지 않은 예외 잡기


응용 프로그램에서 처리되지 않은 모든 자바 스크립트 예외를 경고 상자에 표시하는 방법을 찾거나 알아 내려고합니다. 이 모든 것이 서버 측 코드를 사용하지 않고 클라이언트 측에서 수행되기를 바랍니다. MVC3를 환경으로 사용하고 있습니다.

나는 지난 며칠 동안 조사를 해왔지만 내가 원하는 것을 정확히 찾지 못했습니다.

아래에서 두 가지 방법을 찾았는데, 거의 내가 찾고있는 것 같지만, 이러한 방법은 함수 이름을 사용자 지정 메서드에 전달하여 해당 메서드 내에서 처리되지 않은 모든 예외의 스택 추적을 인쇄해야합니다. 특정 기능. 처리되지 않은 모든 예외의 스택 추적을 인쇄하는 사용자 지정 메서드에 함수 이름을 수동으로 전달할 필요가없는 방법을 찾고 있습니다. 이 사용자 지정 메서드가 전체 응용 프로그램 내에서 처리되지 않은 모든 예외를 '듣기'를 원합니다.

http://eriwen.com/javascript/js-stack-trace/

또한 이전 링크와 유사합니다.

https://github.com/eriwen/javascript-stacktrace

다음은 지정된 자바 스크립트 함수의 스택 추적을 인쇄하는 위의 두 번째 링크의 기본 코드입니다.

instrumentFunction: function (context, functionName, callback) {
    context = context || window;
    var original = context[functionName];
    context[functionName] = function instrumented() {
        callback.call(this, printStackTrace().slice(4));
        return context[functionName]._instrumented.apply(this, arguments);
    };
    context[functionName]._instrumented = original;
}

function printStackTrace(options) {
    options = options || {
        guess: true
    };
    var ex = options.e || null,
        guess = !! options.guess;
    var p = new printStackTrace.implementation(),
        result = p.run(ex);
    return (guess) ? p.guessAnonymousFunctions(result) : result;
}

요약하자면, 모든 자바 스크립트 처리되지 않은 예외를 수신 한 다음 경고 상자의 화면에 인쇄하는 일종의 '리스너'를 갖는 방법을 알고 있습니까?

감사! 제이슨


window.onerror 메서드를 사용하여이 작업을 수행 할 수 있습니다.

window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {
    alert("Error occured: " + errorMsg);//or any message
    return false;
}

를 사용 window.onerror하거나 (놀랍게도!) '오류'이벤트에 올바르게 바인딩 할 수 있습니다.

window.onerror = function (message, file, line, col, error) {
   alert("Error occurred: " + error.message);
   return false;
};
window.addEventListener("error", function (e) {
   alert("Error occurred: " + e.error.message);
   return false;
})

자바 스크립트 오류를 ​​추적하려면 Atatus를 사용해보세요 . 저는 Atatus에서 일합니다.


이외에

window.onerror = function (message, file, line, col, error) {
   alert("Error occurred: " + error.message);
   return false;
};
window.addEventListener("error", function (e) {
   alert("Error occurred: " + e.error.message);
   return false;
})

이벤트를 .then()수신 하는 promise 콜백 ( ) 내에서 발생한 모든 오류를 포착 할 수도 있습니다.unhandledrejection

window.addEventListener('unhandledrejection', function (e) {
  alert("Error occurred: " + e.reason.message);
})

Check out http://log4javascript.org it is based on Log4J. If most of your code is wrapped in try/catch statements to handle exceptions you can make use of this library as a common interface for sending output to an always available "dialog box" or logging window that your end user could see. You could even have a button that performs a window.print() to print the contents of the dialog box to the printer or PDF. Good luck.

참고URL : https://stackoverflow.com/questions/12571650/catching-all-javascript-unhandled-exceptions

반응형