Giter Club home page Giter Club logo

trycatch's Introduction

trycatch

An asynchronous try catch / exception handler with long stack traces for node.js

Background

See the "Background" from the long-stack-traces module.

Install

npm install trycatch

Basic Example

trycatch(function() {
	function f() {
		throw new Error('foo');
	}

	setTimeout(f, Math.random()*1000);
	setTimeout(f, Math.random()*1000);
}, function(err) {
	console.log("This is an asynchronous scoped error handler!\n", err.stack);
});

Output

$ node examples/setTimeout.js
This is an asynchronous scoped error handler!
 Error: foo
    at Object.f (/path/to/trycatch/examples/setTimeout.js:5:9)
    at Timer.callback (timers.js:83:39)
----------------------------------------
    at setTimeout
    at /path/to/trycatch/examples/setTimeout.js:8:2
    at Object.<anonymous> (/path/to/trycatch/examples/setTimeout.js:3:1)
    at Module._compile (module.js:404:26)
    at Object..js (module.js:410:10)
    at Module.load (module.js:336:31)
This is an asynchronous scoped error handler!
 Error: foo
    at Object.f (/path/to/trycatch/examples/setTimeout.js:5:9)
    at Timer.callback (timers.js:83:39)
----------------------------------------
    at setTimeout
    at /path/to/trycatch/examples/setTimeout.js:9:2
    at Object.<anonymous> (/path/to/trycatch/examples/setTimeout.js:3:1)
    at Module._compile (module.js:404:26)
    at Object..js (module.js:410:10)
    at Module.load (module.js:336:31)

Returning 500s on Server Request

http.createServer(function(req, res) {
	trycatch(function() {
		setTimeout(function() {
			throw new Error('Baloney!');
		}, 1000);
	}, function(err) {
		res.writeHead(500);
		res.end(err.stack);
	});
}).listen(8000);

Visit http://localhost:8000 and get your 500.

Nested trycatch

http.createServer(function(req, res) {
	trycatch(function() {
		process_request(req, res);
	}, function(err) {
		res.writeHead(500);
		res.end(err.stack);
	});
}).listen(8000);

function process_request(req, res) {
	// open db connection
	open_some_db();

	trycatch(function() {
		setTimeout(function() {
			throw new Error('whoopsy');
		}, 100);
	}, function(err) {
		// close the db connection
		close_some_db();

		// bubble the error
		throw err;
	});
}

This will allow your function to close the db before your 500 error.

trycatch's People

Contributors

crabdude avatar dvv avatar jeffbski avatar vmirage avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.