🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more

stack-chain

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install
s

stack-chain

API for combining call site modifiers

2.0.0
latest
100

Supply Chain Security

100

Vulnerability

100

Quality

76

Maintenance

100

License

Version published
Weekly downloads
2.9M
2.49%
Maintainers
1
Weekly downloads
 
Created
Issues
2

What is stack-chain?

The stack-chain npm package allows developers to customize and extend the stack traces in Node.js. It provides a way to manipulate the call stack, add custom frames, and format stack traces to make debugging easier.

What are stack-chain's main functionalities?

Customizing Stack Traces

This feature allows you to customize the stack trace by modifying the frames. In this example, the file name in each frame is replaced with 'custom-file.js'.

const stackChain = require('stack-chain');

stackChain.extend.attach(function (error, frames) {
  frames.forEach(frame => {
    frame.getFileName = function () {
      return 'custom-file.js';
    };
  });
  return frames;
});

// Trigger an error to see the custom stack trace
try {
  throw new Error('Test error');
} catch (error) {
  console.log(error.stack);
}

Adding Custom Frames

This feature allows you to add custom frames to the stack trace. In this example, a custom frame with specific file name, line number, column number, and function name is added to the stack trace.

const stackChain = require('stack-chain');

stackChain.extend.attach(function (error, frames) {
  frames.push({
    getFileName: () => 'custom-frame.js',
    getLineNumber: () => 42,
    getColumnNumber: () => 21,
    getFunctionName: () => 'customFunction'
  });
  return frames;
});

// Trigger an error to see the custom frame in the stack trace
try {
  throw new Error('Test error');
} catch (error) {
  console.log(error.stack);
}

Formatting Stack Traces

This feature allows you to format the stack trace according to your needs. In this example, the stack trace is formatted to show the function name, file name, line number, and column number in a custom format.

const stackChain = require('stack-chain');

stackChain.format.attach(function (error, frames) {
  return frames.map(frame => {
    return `${frame.getFunctionName()} at ${frame.getFileName()}:${frame.getLineNumber()}:${frame.getColumnNumber()}`;
  }).join('\n');
});

// Trigger an error to see the formatted stack trace
try {
  throw new Error('Test error');
} catch (error) {
  console.log(error.stack);
}

Other packages similar to stack-chain

FAQs

Package last updated on 23 Sep 2017

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