https://github.com/mdlavin/nested-error-stacks
A node.js module for creating Error objects with nested Errors in stacktraces
https://github.com/mdlavin/nested-error-stacks
error-handling javascript nodejs stack-traces
Last synced: 12 months ago
JSON representation
A node.js module for creating Error objects with nested Errors in stacktraces
- Host: GitHub
- URL: https://github.com/mdlavin/nested-error-stacks
- Owner: mdlavin
- License: mit
- Created: 2014-04-17T15:23:29.000Z (almost 12 years ago)
- Default Branch: master
- Last Pushed: 2022-03-28T20:28:20.000Z (about 4 years ago)
- Last Synced: 2024-10-29T20:55:57.014Z (over 1 year ago)
- Topics: error-handling, javascript, nodejs, stack-traces
- Language: JavaScript
- Size: 38.1 KB
- Stars: 94
- Watchers: 3
- Forks: 13
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Nested stacktraces for Node.js!
===============================
[](https://github.com/mdlavin/nested-error-stacks/actions?query=branch%3Amaster)
[](http://badge.fury.io/js/nested-error-stacks)
With this module, you can wrap a caught exception with extra context
for better debugging. For example, a network error's stack would normally look
like this:
Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
Using this module, you can wrap the Error with more context to get a stack
that looks like this:
NestedError: Failed to communicate with localhost:8080
at Socket. (/Users/mattlavin/Projects/nested-stacks/demo.js:6:18)
at Socket.EventEmitter.emit (events.js:95:17)
at net.js:440:14
at process._tickCallback (node.js:415:13)
Caused By: Error: connect ECONNREFUSED
at errnoException (net.js:904:11)
at Object.afterConnect [as oncomplete] (net.js:895:19)
How to wrap errors
------------------
Here is an example program that uses this module to add more context to errors:
```js
var NestedError = require('nested-error-stacks');
var net = require('net');
var client = net.connect({port: 8080});
client.on('error', function (err) {
var newErr = new NestedError("Failed to communicate with localhost:8080", err);
console.log(newErr.stack);
});
```
How to inherit
--------------
It is recommended to use explicit names for Error classes. You can do it
like this:
```js
var util = require('util');
var NestedError = require('nested-error-stacks');
function MyError(message, nested) {
NestedError.call(this, message, nested);
}
util.inherits(MyError, NestedError);
MyError.prototype.name = 'MyError';
```