-
Notifications
You must be signed in to change notification settings - Fork 500
/
ErrorHandlerImpl.ts
263 lines (242 loc) · 8.3 KB
/
ErrorHandlerImpl.ts
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import {
AccessBlockedError,
AccessDeactivatedError,
AccessExpiredError,
ConnectionError,
InsufficientStorageError,
InvalidSoftwareVersionError,
NotAuthenticatedError,
ServiceUnavailableError,
SessionExpiredError,
} from "../api/common/error/RestError"
import {Dialog} from "../gui/base/Dialog"
import {lang} from "./LanguageViewModel"
import {assertMainOrNode, isDesktop, isOfflineStorageAvailable} from "../api/common/Env"
import {ErrorInfo, neverNull, noOp} from "@tutao/tutanota-utils"
import {logins} from "../api/main/LoginController"
import {OutOfSyncError} from "../api/common/error/OutOfSyncError"
import {showProgressDialog} from "../gui/dialogs/ProgressDialog"
import {IndexingNotSupportedError} from "../api/common/error/IndexingNotSupportedError"
import {windowFacade} from "./WindowFacade"
import {locator} from "../api/main/MainLocator"
import {QuotaExceededError} from "../api/common/error/QuotaExceededError"
import {UserError} from "../api/main/UserError"
import {showMoreStorageNeededOrderDialog} from "./SubscriptionDialogs"
import {showSnackBar} from "../gui/base/SnackBar"
import {Credentials} from "./credentials/Credentials"
import {promptForFeedbackAndSend, showErrorDialogNotLoggedIn} from "./ErrorReporter"
import {CancelledError} from "../api/common/error/CancelledError"
import {getLoginErrorMessage} from "./LoginUtils"
import {isOfflineError} from "../api/common/utils/ErrorCheckUtils.js"
import {SessionType} from "../api/common/SessionType.js"
import {OfflineDbClosedError} from "../api/common/error/OfflineDbClosedError.js"
assertMainOrNode()
let unknownErrorDialogActive = false
let notConnectedDialogActive = false
let invalidSoftwareVersionActive = false
let loginDialogActive = false
let isLoggingOut = false
let serviceUnavailableDialogActive = false
let shownQuotaError = false
let showingImportError = false
const ignoredMessages = ["webkitExitFullScreen", "googletag", "avast_submit"]
export async function handleUncaughtErrorImpl(e: Error) {
if (isLoggingOut) {
// ignore all errors while logging out
return
}
// This is from the s.js and it shouldn't change. Unfortunately it is a plain Error.
if (e.message.includes("(SystemJS https://github.com/systemjs/systemjs/blob/master/docs/errors.md#")) {
handleImportError()
return
}
if (isOfflineError(e)) {
showOfflineMessage()
} else if (e instanceof InvalidSoftwareVersionError) {
if (!invalidSoftwareVersionActive) {
invalidSoftwareVersionActive = true
Dialog.message("outdatedClient_msg").then(() => (invalidSoftwareVersionActive = false))
}
} else if (
e instanceof NotAuthenticatedError ||
e instanceof AccessBlockedError ||
e instanceof AccessDeactivatedError ||
e instanceof AccessExpiredError
) {
// If we session is closed (e.g. password is changed) we log user out forcefully so we reload the page
logoutIfNoPasswordPrompt()
} else if (e instanceof SessionExpiredError) {
reloginForExpiredSession()
} else if (e instanceof OutOfSyncError) {
const isOffline = isOfflineStorageAvailable() && logins.isUserLoggedIn() && logins.getUserController().sessionType === SessionType.Persistent
await Dialog.message("outOfSync_label", lang.get(
isOffline
? "dataExpiredOfflineDb_msg"
: "dataExpired_msg"
))
const {userId} = logins.getUserController()
if (isDesktop()) {
await locator.interWindowEventSender?.localUserDataInvalidated(userId)
await locator.sqlCipherFacade?.deleteDb(userId)
}
await logins.logout(false)
await windowFacade.reload({noAutoLogin: true})
} else if (e instanceof InsufficientStorageError) {
if (logins.getUserController().isGlobalAdmin()) {
showMoreStorageNeededOrderDialog(logins, "insufficientStorageAdmin_msg")
} else {
const errorMessage = () => lang.get("insufficientStorageUser_msg") + " " + lang.get("contactAdmin_msg")
Dialog.message(errorMessage)
}
} else if (e instanceof ServiceUnavailableError) {
if (!serviceUnavailableDialogActive) {
serviceUnavailableDialogActive = true
Dialog.message("serviceUnavailable_msg").then(() => {
serviceUnavailableDialogActive = false
})
}
} else if (e instanceof IndexingNotSupportedError) {
console.log("Indexing not supported", e)
locator.search.indexingSupported = false
} else if (e instanceof QuotaExceededError) {
if (!shownQuotaError) {
shownQuotaError = true
Dialog.message("storageQuotaExceeded_msg")
}
} else if (e instanceof OfflineDbClosedError) {
if (!loginDialogActive) {
throw e
}
} else if (ignoredError(e)) {
// ignore, this is not our code
} else {
if (!unknownErrorDialogActive) {
unknownErrorDialogActive = true
// only logged in users can report errors
if (logins.isUserLoggedIn()) {
promptForFeedbackAndSend(e)
.then(({ignored}) => {
unknownErrorDialogActive = false
if (ignored) {
ignoredMessages.push(e.message)
}
})
} else {
console.log("Unknown error", e)
showErrorDialogNotLoggedIn(e)
.then(() => unknownErrorDialogActive = false)
}
}
}
}
function showOfflineMessage() {
if (!notConnectedDialogActive) {
notConnectedDialogActive = true
showSnackBar({
message: "serverNotReachable_msg",
button: {
label: "ok_action",
click: () => {
}
},
onClose: () => {
notConnectedDialogActive = false
}
})
}
}
function logoutIfNoPasswordPrompt() {
if (!loginDialogActive) {
windowFacade.reload({})
}
}
export async function reloginForExpiredSession() {
if (!loginDialogActive) {
// Make sure that partial login part is complete before we will try to make a new session.
// Otherwise we run into a race condition where login failure arrives before we initialize userController.
await logins.waitForPartialLogin()
console.log("RELOGIN", logins.isUserLoggedIn())
const sessionType = logins.getUserController().sessionType
const userId = logins.getUserController().user._id
locator.loginFacade.resetSession()
loginDialogActive = true
const dialog = Dialog.showRequestPasswordDialog({
action: async (pw) => {
let credentials: Credentials
try {
credentials = await logins.createSession(neverNull(logins.getUserController().userGroupInfo.mailAddress), pw, sessionType)
} catch (e) {
if (e instanceof CancelledError ||
e instanceof AccessBlockedError ||
e instanceof NotAuthenticatedError ||
e instanceof AccessDeactivatedError ||
e instanceof ConnectionError
) {
return lang.getMaybeLazy(getLoginErrorMessage(e, false))
} else {
throw e
}
} finally {
// Once login succeeds we need to manually close the dialog
locator.secondFactorHandler.closeWaitingForSecondFactorDialog()
}
// Fetch old credentials to preserve database key if it's there
const oldCredentials = await locator.credentialsProvider.getCredentialsByUserId(userId)
await locator.sqlCipherFacade.closeDb()
await locator.credentialsProvider.deleteByUserId(userId, {deleteOfflineDb: false})
if (sessionType === SessionType.Persistent) {
await locator.credentialsProvider.store({credentials: credentials, databaseKey: oldCredentials?.databaseKey})
}
loginDialogActive = false
dialog.close()
return ""
},
cancel: {
textId: "logout_label",
action() {
windowFacade.reload({})
},
}
})
}
}
function ignoredError(e: Error): boolean {
return e.message != null && ignoredMessages.some(s => e.message.includes(s))
}
/**
* Trying to handle errors during logout can cause unhandled error loops, so we just want to ignore them
*/
export function disableErrorHandlingDuringLogout() {
isLoggingOut = true
showProgressDialog("loggingOut_msg", new Promise(noOp))
}
function handleImportError() {
if (showingImportError) {
return
}
showingImportError = true
const message =
"There was an error while loading part of the app. It might be that you are offline, running an outdated version, or your browser is blocking the request."
Dialog.choice(() => message, [
{
text: "close_alt",
value: false,
},
{
text: "reloadPage_action",
value: true,
},
]).then(reload => {
showingImportError = false
if (reload) {
windowFacade.reload({})
}
})
}
if (typeof window !== "undefined") {
// @ts-ignore
window.tutao.testError = () => handleUncaughtErrorImpl(new Error("test error!"))
}
export function showUserError(error: UserError): Promise<void> {
return Dialog.message(() => error.message)
}