1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-23 02:05:27 +02:00
Sonarr/src/UI/Instrumentation/ErrorHandler.js

93 lines
2.7 KiB
JavaScript
Raw Normal View History

2013-11-21 09:04:15 +03:00
define(
[
'jquery',
'messenger'
], function ($, Messenger) {
'use strict';
window.alert = function (message) {
new Messenger().post(message);
};
2013-11-21 09:04:15 +03:00
var addError = function (message) {
$('#errors').append('<div>' + message + '</div>');
};
2013-11-12 06:25:54 +03:00
2013-11-21 09:04:15 +03:00
window.onerror = function (msg, url, line) {
2013-11-21 09:04:15 +03:00
try {
2013-11-21 09:04:15 +03:00
var a = document.createElement('a');
a.href = url;
var filename = a.pathname.split('/').pop();
2013-07-01 21:25:37 +03:00
2013-11-21 09:04:15 +03:00
//Suppress Firefox debug errors when console window is closed
if (filename.toLowerCase() === 'markupview.jsm' || filename.toLowerCase() === 'markup-view.js') {
return false;
}
2013-07-01 21:25:37 +03:00
2013-11-21 09:04:15 +03:00
var messageText = filename + ' : ' + line + '</br>' + msg;
2013-11-21 09:04:15 +03:00
var message = {
message : messageText,
type : 'error',
hideAfter : 1000,
showCloseButton: true
};
2013-11-21 09:04:15 +03:00
new Messenger().post(message);
2013-11-21 09:04:15 +03:00
addError(message.message);
2013-11-12 06:25:54 +03:00
2013-11-21 09:04:15 +03:00
}
catch (error) {
console.log('An error occurred while reporting error. ' + error);
console.log(msg);
new Messenger().post('Couldn\'t report JS error. ' + msg);
}
2013-11-21 09:04:15 +03:00
return false; //don't suppress default alerts and logs.
};
2013-11-21 09:04:15 +03:00
$(document).ajaxError(function (event, xmlHttpRequest, ajaxOptions) {
2013-11-21 09:04:15 +03:00
//don't report 200 error codes
if (xmlHttpRequest.status >= 200 && xmlHttpRequest.status <= 300) {
return undefined;
}
2013-11-21 09:04:15 +03:00
//don't report aborted requests
if (xmlHttpRequest.statusText === 'abort') {
return undefined;
}
2013-11-21 09:04:15 +03:00
var message = {
type : 'error',
hideAfter : 1000,
showCloseButton: true
};
2013-11-21 09:04:15 +03:00
if (xmlHttpRequest.status === 0 && xmlHttpRequest.readyState === 0) {
return false;
}
2013-11-12 06:25:54 +03:00
2013-11-21 09:04:15 +03:00
if (xmlHttpRequest.status === 400 && ajaxOptions.isValidatedCall) {
return false;
}
2013-11-21 09:04:15 +03:00
if (xmlHttpRequest.status === 503) {
message.message = xmlHttpRequest.responseJSON.message;
}
2013-08-22 03:36:35 +03:00
2013-11-21 09:04:15 +03:00
else {
message.message = '[{0}] {1} : {2}'.format(ajaxOptions.type, xmlHttpRequest.statusText, ajaxOptions.url);
}
2013-08-22 03:36:35 +03:00
2013-11-21 09:04:15 +03:00
new Messenger().post(message);
addError(message.message);
2013-11-12 06:25:54 +03:00
2013-11-21 09:04:15 +03:00
return false;
});
});