MCPcopy Index your code
hub / github.com/CrabDude/trycatch

github.com/CrabDude/trycatch @v1.5.11

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.5.11 ↗ · + Follow
37 symbols 81 edges 13 files 0 documented · 0% updated 5y ago★ 2457 open issues
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

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.

Core symbols most depended-on inside this repo

trycatch
called by 25
lib/trycatch.js
addNonEnumerableValue
called by 8
lib/formatError.js
normalizeError
called by 3
lib/formatError.js
runInDomain
called by 3
lib/trycatch.js
isCoreSlice
called by 2
lib/formatError.js
isCoreError
called by 2
lib/formatError.js
handleException
called by 2
lib/trycatch.js
stringify
called by 2
lib/FormatStackTrace.js

Shape

Function 37

Languages

TypeScript100%

Modules by API surface

lib/trycatch.js13 symbols
lib/formatError.js10 symbols
test/event-emitter.test.js3 symbols
lib/FormatStackTrace.js3 symbols
examples/helloworld.js3 symbols
test/throw-non-error.test.js1 symbols
test/nested.test.js1 symbols
test/configure.test.js1 symbols
test/basic.test.js1 symbols
examples/setTimeout.js1 symbols

For agents

$ claude mcp add trycatch \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact