summaryrefslogtreecommitdiffstats
path: root/core/js/js.js
diff options
context:
space:
mode:
authorThomas Müller <thomas.mueller@tmit.eu>2016-02-17 14:49:04 +0100
committerThomas Müller <thomas.mueller@tmit.eu>2016-02-17 14:49:04 +0100
commit7af7d18cfa2f1fab239e9a21e989bd8061cf23bb (patch)
treef11a35340c168a2a866fcf5c82c9d21d847206aa /core/js/js.js
parentadd696b057fa40696c251d4a52ed9d2a997c7aa0 (diff)
parentb99c6f1f67a207984b8b5355703cabd89d1e7c73 (diff)
downloadnextcloud-server-7af7d18cfa2f1fab239e9a21e989bd8061cf23bb.tar.gz
nextcloud-server-7af7d18cfa2f1fab239e9a21e989bd8061cf23bb.zip
Merge pull request #16783 from owncloud/handle-redirects-global
Adding global error handler for ajax calls which run into redirection…
Diffstat (limited to 'core/js/js.js')
-rw-r--r--core/js/js.js64
1 files changed, 64 insertions, 0 deletions
diff --git a/core/js/js.js b/core/js/js.js
index 83658a537b8..fac9c45f668 100644
--- a/core/js/js.js
+++ b/core/js/js.js
@@ -235,6 +235,13 @@ var OC={
},
/**
+ * Reloads the current page
+ */
+ reload: function() {
+ window.location.reload();
+ },
+
+ /**
* Protocol that is used to access this ownCloud instance
* @return {string} Used protocol
*/
@@ -727,6 +734,56 @@ var OC={
isUserAdmin: function() {
return oc_isadmin;
},
+
+ /**
+ * Process ajax error, redirects to main page
+ * if an error/auth error status was returned.
+ */
+ _processAjaxError: function(xhr) {
+ // purposefully aborted request ?
+ if (xhr.status === 0 && (xhr.statusText === 'abort' || xhr.statusText === 'timeout')) {
+ return;
+ }
+
+ if (_.contains([0, 302, 307, 401], xhr.status)) {
+ OC.reload();
+ }
+ },
+
+ /**
+ * Registers XmlHttpRequest object for global error processing.
+ *
+ * This means that if this XHR object returns 401 or session timeout errors,
+ * the current page will automatically be reloaded.
+ *
+ * @param {XMLHttpRequest} xhr
+ */
+ registerXHRForErrorProcessing: function(xhr) {
+ var loadCallback = function() {
+ if (xhr.readyState !== 4) {
+ return;
+ }
+
+ if (xhr.status >= 200 && xhr.status < 300 || xhr.status === 304) {
+ return;
+ }
+
+ // fire jquery global ajax error handler
+ $(document).trigger(new $.Event('ajaxError'), xhr);
+ };
+
+ var errorCallback = function() {
+ // fire jquery global ajax error handler
+ $(document).trigger(new $.Event('ajaxError'), xhr);
+ };
+
+ // FIXME: also needs an IE8 way
+ if (xhr.addEventListener) {
+ xhr.addEventListener('load', loadCallback);
+ xhr.addEventListener('error', errorCallback);
+ }
+
+ }
};
/**
@@ -1311,6 +1368,13 @@ function initCore() {
$('html').addClass('edge');
}
+ $(document).on('ajaxError.main', function( event, request, settings ) {
+ if (settings && settings.allowAuthErrors) {
+ return;
+ }
+ OC._processAjaxError(request);
+ });
+
/**
* Calls the server periodically to ensure that session doesn't
* time out