diff options
author | Jo-Philipp Wich <jo@mein.io> | 2019-09-11 09:28:21 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2019-09-11 12:21:05 +0200 |
commit | c3ddcbb542d99c4284981015fcaa429c7d474391 (patch) | |
tree | 8e7366b59ee5fadd6c9d19b416a80a6d9eb1481a | |
parent | f141433f5e38153e9dfbb8b320dfa70db1a47cf2 (diff) |
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>
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/luci.js | 51 |
1 files changed, 29 insertions, 22 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/luci.js b/modules/luci-base/htdocs/luci-static/resources/luci.js index 80648633d..0b9886680 100644 --- a/modules/luci-base/htdocs/luci-static/resources/luci.js +++ b/modules/luci-base/htdocs/luci-static/resources/luci.js @@ -619,16 +619,35 @@ if (type instanceof Error) { e = type; - stack = (e.stack || '').split(/\n/); if (msg) e.message = msg + ': ' + e.message; } else { + try { throw new Error('stacktrace') } + catch (e2) { stack = (e2.stack || '').split(/\n/) } + e = new (window[type || 'Error'] || Error)(msg || 'Unspecified error'); e.name = type || 'Error'; } + stack = (stack || []).map(function(frame) { + frame = frame.replace(/(.*?)@(.+):(\d+):(\d+)/g, 'at $1 ($2:$3:$4)').trim(); + return frame ? ' ' + frame : ''; + }); + + if (!/^ at /.test(stack[0])) + stack.shift(); + + if (/\braise /.test(stack[0])) + stack.shift(); + + if (/\berror /.test(stack[0])) + stack.shift(); + + if (stack.length) + e.message += '\n' + stack.join('\n'); + if (window.console && console.debug) console.debug(e); @@ -640,28 +659,16 @@ 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(); - return frame ? ' ' + frame : ''; - }); - - if (!/^ at /.test(stack[0])) - stack.shift(); - - if (/\braise /.test(stack[0])) - stack.shift(); - - if (/\berror /.test(stack[0])) - stack.shift(); - - stack = stack.length ? '\n' + stack.join('\n') : ''; + if (!e.reported) { + if (L.ui) + L.ui.addNotification(e.name || _('Runtime error'), + E('pre', {}, e.message), 'danger'); + else + L.dom.content(document.querySelector('#maincontent'), + E('pre', { 'class': 'alert-message error' }, e.message)); - if (L.ui) - L.ui.showModal(e.name || _('Runtime error'), - E('pre', { 'class': 'alert-message error' }, e.message + stack)); - else - L.dom.content(document.querySelector('#maincontent'), - E('pre', { 'class': 'alert-message error' }, e + stack)); + e.reported = true; + } throw e; } |