Errors that happen inside Deferred object throws a warning in to the console and not getting noticed in:
window.addEventListener("error", function(e) {
// Global handler
});
How to make centralized error handler work for all errors including Deferred object?
I am using most recent jQuery 3.3.1 and couldn't find woking solution.
After reading jQuery 3.3.1 (line 3605) , they actually have implemented $.Deferred.exceptionHook
to be called when deferred object fails.
And for your case, you just need to implement it like,
$.Deferred.exceptionHook = function (err, stackTrace) {
// 'err' is what you throw in your deferred's catch.
window.dispatchEvent( new CustomEvent('error', {
detail: err
}) );
}
Here's some simple example.
$.Deferred.exceptionHook = function (err, stackTrace) { // 'err' is what you throw in your deferred's catch. $("#errorMessage").text(err); } $.when( $.ajax( "https://example.com" ) // This should fail since SO is sandboxed. ).then(function successFn() { alert("Impossible thing is happening"); }, function failFn() { throw "A nice error"; });
<script src="https://code.jquery.com/jquery-3.3.1.js"></script> <p id="errorMessage"> </p>
If you're using jquery for ajax requests you could do:
$(document).ajaxError(function(){
console.log(arguments);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.