Welcome to mirror list, hosted at ThFree Co, Russian Federation.

xhr-error.js « OC « src « core - github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 660651dd0d9e7c9e53c830b48e6bc140b399c315 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
 * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 * @author John Molakvoæ <skjnldsv@protonmail.com>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */

import _ from 'underscore'
import $ from 'jquery'

import OC from './index'
import Notification from './notification'

/**
 * Warn users that the connection to the server was lost temporarily
 *
 * This function is throttled to prevent stacked notfications.
 * After 7sec the first notification is gone, then we can show another one
 * if necessary.
 */
export const ajaxConnectionLostHandler = _.throttle(() => {
	Notification.showTemporary(t('core', 'Connection to server lost'))
}, 7 * 1000, { trailing: false })

/**
 * Process ajax error, redirects to main page
 * if an error/auth error status was returned.
 * @param {XMLHttpRequest} xhr xhr request
 */
export const processAjaxError = xhr => {
	// purposefully aborted request ?
	// OC._userIsNavigatingAway needed to distinguish ajax calls cancelled by navigating away
	// from calls cancelled by failed cross-domain ajax due to SSO redirect
	if (xhr.status === 0 && (xhr.statusText === 'abort' || xhr.statusText === 'timeout' || OC._reloadCalled)) {
		return
	}

	if (_.contains([302, 303, 307, 401], xhr.status) && OC.currentUser) {
		// sometimes "beforeunload" happens later, so need to defer the reload a bit
		setTimeout(function() {
			if (!OC._userIsNavigatingAway && !OC._reloadCalled) {
				let timer = 0
				const seconds = 5
				const interval = setInterval(function() {
					Notification.showUpdate(n('core', 'Problem loading page, reloading in %n second', 'Problem loading page, reloading in %n seconds', seconds - timer))
					if (timer >= seconds) {
						clearInterval(interval)
						OC.reload()
					}
					timer++
				}, 1000 // 1 second interval
				)

				// only call reload once
				OC._reloadCalled = true
			}
		}, 100)
	} else if (xhr.status === 0) {
		// Connection lost (e.g. WiFi disconnected or server is down)
		setTimeout(function() {
			if (!OC._userIsNavigatingAway && !OC._reloadCalled) {
				// TODO: call method above directly
				OC._ajaxConnectionLostHandler()
			}
		}, 100)
	}
}

/**
 * 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 xhr request
 */
export const registerXHRForErrorProcessing = xhr => {
	const loadCallback = () => {
		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)
	}

	const errorCallback = () => {
		// fire jquery global ajax error handler
		$(document).trigger(new $.Event('ajaxError'), xhr)
	}

	if (xhr.addEventListener) {
		xhr.addEventListener('load', loadCallback)
		xhr.addEventListener('error', errorCallback)
	}

}