☠️ Serialise errors to literal (JSONable) object


import errobj from 'errobj';

try {
    some broken code
} catch (error) {


  1. {Error} (error) An error to be serialised
  2. {Object} (enrichment) [_optional_] - This object's field values will be assigned to the serialised error
  3. {Object} (options) [optional, _nullable_] - See details below

Example: Sending uncaught error to an HTTP error logger

const errobj = require('errobj');

const original_onerror = window.onerror; // play nicely
window.onerror = function(message, url, lineNumber, columnNumber, error) {
            method: 'POST',
            body: JSON.stringify(
                errobj(error, {message, url, lineNumber, columnNumber, level: 'error'})

    original_onerror(message, url, lineNumber, columnNumber, error);


The serialised error

    name: 'RangeError',
    message: 'Nothing',
    stack: 'ReferenceError: something is not defined\nat change (index.html:46)\nat index.html:53\nat index.html:56',
    lineNumber: '46',
    columnNumber: '12',
    fileName: 'index.html',
    functionName: 'change',
    source: 'at change (index.html:46)',
    level: 'error'

Add fields to the parsed object

errobj(error, {flow: 'registration'});

option: offset

function verboseLog(message) {
    const error = new Error(message);
    send(errobj(error, null, {offset: 1}));

option: parsedStack

errobj(error, null, {parsedStack: true});

    parsedStack: [
            lineNumber: 46,
            fileName: 'index.html',
            functionName: 'change',
            source: 'at change (index.html:46)'
            lineNumber: 53,
            fileName: 'index.html',
            source: 'at index.html:53'
            lineNumber: 56,
            fileName: 'index.html',
            source: 'at index.html:56'

Bundled version

Environments which exclude node_modules from the transpiling pipeline should include the "browser" entry instead of "main". This exposes a bundled ES5 commonjs module.

Also available for explicit import:

const errobj = require('errobj/dist');