Socket
Socket
Sign inDemoInstall

errobj

Package Overview
Dependencies
2
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    errobj

☠️ Serialise errors to literal (JSONable) object


Version published
Weekly downloads
8.3K
increased by12.32%
Maintainers
1
Install size
80.0 kB
Created
Weekly downloads
 

Changelog

Source

3.0.1

  • Fix: error.cause should remain its original structure instead of being "stringified"

Readme

Source

errobj

☠️ Serialise errors to literal (JSONable) object

  • ✔︎ Designed for error loggers
  • ✔︎ Serialises errors to literal objects
  • ✔︎ Supports any properties attached to the error
  • ✔︎ Expands the error details with lineNumber, columnName, fileName, functionName, ...
  • ✔︎ Parses error cause
  • ✔︎ Accepts an enrichment object
  • ✔︎ Parses the stack trace (error-stack-parser)
  • ✔︎ Isomorphic

TL;DR

import { errobj } from 'errobj';

try {
	some broken code
} catch (error) {
	send(JSON.stringify(errobj(error)));
}
Arguments
  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
    • {Number} offset [optional] - Offset the parsed stack, and error position details. Good for middleware created error objects.
    • {Number} parsedStack [optional] - Add a parsed stack of the error with a certain depth

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) {
	fetch("/error-logger", {
		method: "POST",
		body: JSON.stringify(
			errobj(error, { message, url, lineNumber, columnNumber, level: "error" })
		),
	});

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

Examples

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 (stack)

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

option: parsedStack

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

{
	...
	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'
		}
	],
	...
}

Keywords

FAQs

Last updated on 11 May 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc