luci-base: luci.js: rework error handling
- Capture stack trace in L.raise() if passed type is not an Error instance - Use L.ui.addNotification in L.error() to render the error message - Prevent duplicate error reporting in the ui Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
parent
f141433f5e
commit
c3ddcbb542
1 changed files with 29 additions and 22 deletions
|
@ -619,28 +619,19 @@
|
||||||
|
|
||||||
if (type instanceof Error) {
|
if (type instanceof Error) {
|
||||||
e = type;
|
e = type;
|
||||||
stack = (e.stack || '').split(/\n/);
|
|
||||||
|
|
||||||
if (msg)
|
if (msg)
|
||||||
e.message = msg + ': ' + e.message;
|
e.message = msg + ': ' + e.message;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
try { throw new Error('stacktrace') }
|
||||||
|
catch (e2) { stack = (e2.stack || '').split(/\n/) }
|
||||||
|
|
||||||
e = new (window[type || 'Error'] || Error)(msg || 'Unspecified error');
|
e = new (window[type || 'Error'] || Error)(msg || 'Unspecified error');
|
||||||
e.name = type || 'Error';
|
e.name = type || 'Error';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (window.console && console.debug)
|
stack = (stack || []).map(function(frame) {
|
||||||
console.debug(e);
|
|
||||||
|
|
||||||
throw e;
|
|
||||||
},
|
|
||||||
|
|
||||||
error: function(type, fmt /*, ...*/) {
|
|
||||||
try {
|
|
||||||
L.raise.apply(L, Array.prototype.slice.call(arguments));
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
var stack = (e.stack || '').split(/\n/).map(function(frame) {
|
|
||||||
frame = frame.replace(/(.*?)@(.+):(\d+):(\d+)/g, 'at $1 ($2:$3:$4)').trim();
|
frame = frame.replace(/(.*?)@(.+):(\d+):(\d+)/g, 'at $1 ($2:$3:$4)').trim();
|
||||||
return frame ? ' ' + frame : '';
|
return frame ? ' ' + frame : '';
|
||||||
});
|
});
|
||||||
|
@ -654,14 +645,30 @@
|
||||||
if (/\berror /.test(stack[0]))
|
if (/\berror /.test(stack[0]))
|
||||||
stack.shift();
|
stack.shift();
|
||||||
|
|
||||||
stack = stack.length ? '\n' + stack.join('\n') : '';
|
if (stack.length)
|
||||||
|
e.message += '\n' + stack.join('\n');
|
||||||
|
|
||||||
|
if (window.console && console.debug)
|
||||||
|
console.debug(e);
|
||||||
|
|
||||||
|
throw e;
|
||||||
|
},
|
||||||
|
|
||||||
|
error: function(type, fmt /*, ...*/) {
|
||||||
|
try {
|
||||||
|
L.raise.apply(L, Array.prototype.slice.call(arguments));
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
if (!e.reported) {
|
||||||
if (L.ui)
|
if (L.ui)
|
||||||
L.ui.showModal(e.name || _('Runtime error'),
|
L.ui.addNotification(e.name || _('Runtime error'),
|
||||||
E('pre', { 'class': 'alert-message error' }, e.message + stack));
|
E('pre', {}, e.message), 'danger');
|
||||||
else
|
else
|
||||||
L.dom.content(document.querySelector('#maincontent'),
|
L.dom.content(document.querySelector('#maincontent'),
|
||||||
E('pre', { 'class': 'alert-message error' }, e + stack));
|
E('pre', { 'class': 'alert-message error' }, e.message));
|
||||||
|
|
||||||
|
e.reported = true;
|
||||||
|
}
|
||||||
|
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue