Javascript exception tracebacks

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 &&
                   error.type =
    error.code = exception.code =
    error.message = exception.message
    if (exception.label) error.label = exception.label
    error.stack = []
    StackTrace.fromError(new Error(exception))
      .then(stackframes => { (frame, depth) {
          if (depth !== 0)
        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.

Leave a Comment