trycatch

An asynchronous domain-based exception handler with long stack traces for node.js

  • 所有者: CrabDude/trycatch
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

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

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 exceptions 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()
})

主要指标

概览
名称与所有者CrabDude/trycatch
主编程语言JavaScript
编程语言JavaScript (语言数: 2)
平台
许可证MIT License
所有者活动
创建于2011-09-27 22:37:33
推送于2020-07-15 19:50:12
最后一次提交2019-12-10 14:23:30
发布数54
最新版本名称v1.5.11 (发布于 2014-04-04 18:06:42)
第一版名称v0.1.1 (发布于 2012-11-08 17:51:26)
用户参与
星数245
关注者数14
派生数17
提交数214
已启用问题?
问题数41
打开的问题数7
拉请求数8
打开的拉请求数2
关闭的拉请求数4
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?