mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-20 15:36:16 -04:00
pad: Pop up an error message on unhandled Promise rejection
This commit is contained in:
parent
93c335b3b8
commit
c845d985e0
1 changed files with 21 additions and 10 deletions
|
@ -474,11 +474,22 @@ var padutils = {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var globalExceptionHandler = undefined;
|
let globalExceptionHandler = null;
|
||||||
function setupGlobalExceptionHandler() {
|
padutils.setupGlobalExceptionHandler = () => {
|
||||||
if (!globalExceptionHandler) {
|
if (globalExceptionHandler == null) {
|
||||||
globalExceptionHandler = function test (msg, url, linenumber)
|
globalExceptionHandler = (e) => {
|
||||||
{
|
let type;
|
||||||
|
let msg, url, lineno;
|
||||||
|
if (e instanceof ErrorEvent) {
|
||||||
|
type = 'Uncaught exception';
|
||||||
|
({message: msg, filename: url, lineno: linenumber} = e);
|
||||||
|
} else if (e instanceof PromiseRejectionEvent) {
|
||||||
|
type = 'Unhandled Promise rejection';
|
||||||
|
const err = e.reason || {};
|
||||||
|
({message: msg = 'unknown', fileName: url = 'unknown', lineNumber: linenumber = -1} = err);
|
||||||
|
} else {
|
||||||
|
throw new Error(`unknown event: ${e.toString()}`);
|
||||||
|
}
|
||||||
var errorId = randomString(20);
|
var errorId = randomString(20);
|
||||||
|
|
||||||
var msgAlreadyVisible = false;
|
var msgAlreadyVisible = false;
|
||||||
|
@ -497,6 +508,7 @@ function setupGlobalExceptionHandler() {
|
||||||
.text('If the problem persists, please send this error message to your webmaster:'),
|
.text('If the problem persists, please send this error message to your webmaster:'),
|
||||||
$('<div>').css('text-align', 'left').css('font-size', '.8em').css('margin-top', '1em')
|
$('<div>').css('text-align', 'left').css('font-size', '.8em').css('margin-top', '1em')
|
||||||
.append(txt(`ErrorId: ${errorId}`)).append($('<br>'))
|
.append(txt(`ErrorId: ${errorId}`)).append($('<br>'))
|
||||||
|
.append(txt(type)).append($('<br>'))
|
||||||
.append(txt(`URL: ${window.location.href}`)).append($('<br>'))
|
.append(txt(`URL: ${window.location.href}`)).append($('<br>'))
|
||||||
.append(txt(`UserAgent: ${navigator.userAgent}`)).append($('<br>'))
|
.append(txt(`UserAgent: ${navigator.userAgent}`)).append($('<br>'))
|
||||||
.append($('<b>').addClass('error-msg').text(msg)).append($('<br>'))
|
.append($('<b>').addClass('error-msg').text(msg)).append($('<br>'))
|
||||||
|
@ -516,6 +528,7 @@ function setupGlobalExceptionHandler() {
|
||||||
$.post('../jserror', {
|
$.post('../jserror', {
|
||||||
errorInfo: JSON.stringify({
|
errorInfo: JSON.stringify({
|
||||||
errorId,
|
errorId,
|
||||||
|
type,
|
||||||
msg,
|
msg,
|
||||||
url: window.location.href,
|
url: window.location.href,
|
||||||
source: url,
|
source: url,
|
||||||
|
@ -523,15 +536,13 @@ function setupGlobalExceptionHandler() {
|
||||||
userAgent: navigator.userAgent,
|
userAgent: navigator.userAgent,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
return false;
|
|
||||||
};
|
};
|
||||||
window.onerror = globalExceptionHandler;
|
window.onerror = null; // Clear any pre-existing global error handler.
|
||||||
|
window.addEventListener('error', globalExceptionHandler);
|
||||||
|
window.addEventListener('unhandledrejection', globalExceptionHandler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
padutils.setupGlobalExceptionHandler = setupGlobalExceptionHandler;
|
|
||||||
|
|
||||||
padutils.binarySearch = require('./ace2_common').binarySearch;
|
padutils.binarySearch = require('./ace2_common').binarySearch;
|
||||||
|
|
||||||
// https://stackoverflow.com/a/42660748
|
// https://stackoverflow.com/a/42660748
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue