# trycatch

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

Latest version **1.5.21** (published 2016-01-06) · 0 weekly downloads

## Install

```sh
npm install trycatch
pnpm add trycatch
yarn add trycatch
bun add trycatch
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.5.21 |
| Published | 2016-01-06 |
| First published | 2011-09-29 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.8.14 |
| Dependencies | 2 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Adam Crabtree |
| Maintainers | crabdude |
| Keywords | error, exception, try, catch, stack, trace |

## Links

- npm: https://www.npmjs.com/package/trycatch
- Repository: https://github.com/CrabDude/trycatch
- Homepage: http://github.com/CrabDude/trycatch
- Issues: http://github.com/CrabDude/trycatch/issues
- npm.io page: https://npm.io/package/trycatch

## Dependencies (2)

- [hookit](https://npm.io/package/hookit.md) ^1.1.3
- [ansi-styles](https://npm.io/package/ansi-styles.md) 1.x.x

## Alternatives

- [@sentry/react-native](https://npm.io/package/@sentry/react-native.md) — 2.6M weekly downloads
- [@ardatan/aggregate-error](https://npm.io/package/@ardatan/aggregate-error.md) — 708.1K weekly downloads
- [custom-error-generator](https://npm.io/package/custom-error-generator.md) — 2.0K weekly downloads
- [@technik-sde/prosemirror-recreate-transform](https://npm.io/package/@technik-sde/prosemirror-recreate-transform.md) — 1.5K weekly downloads
- [@suchipi/error-utils](https://npm.io/package/@suchipi/error-utils.md) — 78 weekly downloads

## Recent versions

- 1.5.21 (latest) — 2016-01-06
- 1.5.20 — 2015-09-16
- 1.5.19 — 2015-09-03
- 1.5.18 — 2015-09-03
- 1.5.17 — 2015-09-02
- 1.5.16 — 2015-09-01
- 1.5.15 — 2015-09-01
- 1.5.14 — 2015-09-01
- 1.5.13 — 2015-09-01
- 1.5.12 — 2015-08-31
- 1.5.11 — 2014-04-05
- 1.5.10 — 2014-04-05
- 1.5.9 — 2014-04-05
- 1.5.8 — 2014-04-05
- 1.5.7 — 2014-04-02
- … 60 more at https://npm.io/package/trycatch/versions

## README

trycatch
=======
[![Build Status](https://travis-ci.org/CrabDude/trycatch.png)](https://travis-ci.org/CrabDude/trycatch)
A domain-based asynchronous try/catch with (optional) long stack traces for node.js [optimized for V8](https://github.com/joyent/node/wiki/Best-practices-and-gotchas-with-v8).

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

#Install
```
npm install trycatch
```

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


###Returning 500s on Server Request

```javascript
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:
```javascript
// 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:
```javascript
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:
```javascript
var trycatch = require('trycatch')
trycatch.configure({
  format: function(line) {
    // Alter final output (falsy values will omit)
    return line
  }
})
trycatch(fnTry, fnCatch)
```

#Basic Example
```javascript
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

![](https://raw.github.com/CrabDude/trycatch/master/screenshot.png)


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

```node
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()
})
```

---
_Source: https://npm.io/package/trycatch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
