Working with browser Javascript on mobile devices, I found myself needing to report exceptions back to a server. (It’s hard to see the browser console log on a mobile device without connecting it to a desktop machine).
Here’s what I did to make a suitable object that can be serialized.
function formatException(exception, callback) {
const error = {}
if (exception &&
exception.constructor &&
exception.constructor.name)
error.type = exception.constructor.name
error.code = exception.code
error.name = exception.name
error.message = exception.message
if (exception.label) error.label = exception.label
error.stack = []
StackTrace.fromError(new Error(exception))
.then(stackframes => {
stackframes.map(function (frame, depth) {
if (depth !== 0)
error.stack.push(frame.toString())
})
postObject({type: 'error', error})
})
.catch(er => console.error('stack?', exception, er))
}
...
catch (exception) {
formatException(exeception, console.log)
}
To use this you’ll need stacktrace.js loaded in your web page.