Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hitorisensei/errors.wrap

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hitorisensei/errors.wrap

This is a simple library for wrapping errors with additional context messages for rethrowing without losing the original error stack trace and message.

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

errors.wrap

This is a simple library for wrapping errors with additional context messages for rethrowing without losing the original error stack trace and message.

Example

Without wrapping

Code
function connectDatabase() {
    throw new Error('Database connection failed');
}

function startApp() {
    try {
        connectDatabase();
    } catch (err) {
        throw err;
    }
}


try {
    startApp();
} catch (err) {
    console.error(err);
}
Output
Error: Database connection failed
    at connectDatabase (test-nowrap.js:3:9)
    at startApp (test-nowrap.js:8:5)
    at Object.<anonymous> (test-nowrap.js:16:3)
    at Module._compile (node:internal/modules/cjs/loader:1112:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Module._load (node:internal/modules/cjs/loader:834:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

With wrapping

const { wrap } = require('@hitorisensei/errors.wrap');

function connectDatabase() {
  throw new Error('Database connection failed');
}

function startApp() {
  try {
    connectDatabase();
  } catch (err) {
    throw wrap(err, 'Cannot start app');
  }
}

try {
  startApp();
} catch (err) {
  console.error(err);
}
Output
Error: Cannot start app: Database connection failed
    at startApp (/Users/hitori/Projekty/wrap/test-wrap.js:11:11)
    at Object.<anonymous> (/Users/hitori/Projekty/wrap/test-wrap.js:16:3)
    at Module._compile (node:internal/modules/cjs/loader:1112:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Module._load (node:internal/modules/cjs/loader:834:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47
Error: Database connection failed
    at connectDatabase (/Users/hitori/Projekty/wrap/test-wrap.js:4:9)
    at startApp (/Users/hitori/Projekty/wrap/test-wrap.js:9:5)
    at Object.<anonymous> (/Users/hitori/Projekty/wrap/test-wrap.js:16:3)
    at Module._compile (node:internal/modules/cjs/loader:1112:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1166:10)
    at Module.load (node:internal/modules/cjs/loader:988:32)
    at Module._load (node:internal/modules/cjs/loader:834:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

Issues

  • Wrapped error stack trace is modified to include additional wrap hops, so it may break some tools that rely on stack trace format.

FAQs

Package last updated on 01 Mar 2023

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc