1.5.21 • Published 8 years ago

trycatch v1.5.21

Weekly downloads
1,038
License
-
Repository
github
Last release
8 years ago

trycatch

Build Status A domain-based asynchronous try/catch with (optional) long stack traces for node.js optimized for V8.

WARNING: trycatch replaces the built-in global Error object.

#Install

npm install trycatch

#Use

var trycatch = require('trycatch')
trycatch(function() {
  // do something error-prone
}, function(err) {
  console.log(err.stack);
})

###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.

#Options Optional Long-Stack-Traces:

// Because trycatch shims all native I/O calls,
// it must be required & configured with 'long-stack-traces' before any other modules.
var trycatch = require('trycatch')
trycatch.configure({'long-stack-traces': true})
trycatch(fnTry, fnCatch)

Colors:

var trycatch = require('trycatch')
trycatch.configure({
  colors: {
    // 'none' or falsy values will omit
    'node': 'none',
    'node_modules': false,
    'default': 'yellow'
  }
})
trycatch(fnTry, fnCatch)

Advanced Formatting:

var trycatch = require('trycatch')
trycatch.configure({
  format: function(line) {
    // Alter final output (falsy values will omit)
    return line
  }
})
trycatch(fnTry, fnCatch)

#Basic Example

var trycatch = require("trycatch"),
  _ = require('underscore')._

  trycatch(function() {
  _.map(['Error 1', 'Error 2'], function foo(v) {
    setTimeout(function() {
      throw new Error(v)
    }, 10)
  })
}, function(err) {
  console.log("Async error caught!\n", err.stack);
});

Output

npm.io

#Advanced Examples

See the /test and examples directories for more use cases.

uncaughtApplicationException

trycatch effectively wraps all application callbacks in try/catch blocks, preventing an exception in your application code from causing code in core to not execute. Effectively, this means that excpeitons originating in application code that normally would be passed as uncaughtException, can instead be handled via uncaughtApplicationException without requiring a restart:

process.on('uncaughtApplicationException', (err) => console.log(err.stack))

process.on('uncaughtException', (err) => {
  console.log(err.stack)
  // We are in an undefined state and need to restart
  handleSoftShutodwn()
})
1.5.21

8 years ago

1.5.20

9 years ago

1.5.19

9 years ago

1.5.18

9 years ago

1.5.17

9 years ago

1.5.16

9 years ago

1.5.15

9 years ago

1.5.14

9 years ago

1.5.13

9 years ago

1.5.12

9 years ago

1.5.11

10 years ago

1.5.10

10 years ago

1.5.9

10 years ago

1.5.8

10 years ago

1.5.7

10 years ago

1.5.6

10 years ago

1.5.5

10 years ago

1.5.4

10 years ago

1.5.3

10 years ago

1.5.2

10 years ago

1.5.1

10 years ago

1.5.0

10 years ago

1.4.4

10 years ago

1.4.3

10 years ago

1.4.2

10 years ago

1.4.1

10 years ago

1.4.0

10 years ago

1.3.3

10 years ago

1.3.2

10 years ago

1.3.1

10 years ago

1.3.0

10 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.6

10 years ago

1.0.5

10 years ago

1.0.4

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.2.13

11 years ago

0.2.12

11 years ago

0.2.11

11 years ago

0.2.10

11 years ago

0.2.9

11 years ago

0.2.8

11 years ago

0.2.7

11 years ago

0.2.6

11 years ago

0.2.5

11 years ago

0.2.4

11 years ago

0.2.3

11 years ago

0.2.2

11 years ago

0.2.1

11 years ago

0.1.6

11 years ago

0.2.0

11 years ago

0.1.5

11 years ago

0.1.4

11 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

12 years ago

0.0.11

12 years ago

0.0.9

12 years ago

0.0.8

12 years ago

0.0.7

13 years ago

0.0.6

13 years ago

0.0.5

13 years ago

0.0.4

13 years ago

0.0.3

13 years ago

0.0.2

13 years ago

0.0.1

13 years ago