# damn-it

> A very simple library which keeps track of errors that occur during runtime

Latest version **0.3.1** (published 2015-04-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install damn-it
pnpm add damn-it
yarn add damn-it
bun add damn-it
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.1 |
| Published | 2015-04-18 |
| First published | 2015-03-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Sina Sadrzadeh |
| Maintainers | sadrzadehsina |

## Links

- npm: https://www.npmjs.com/package/damn-it
- Repository: https://github.com/sadrzadehsina/damn-it
- Issues: https://github.com/sadrzadehsina/damn-it/issues
- npm.io page: https://npm.io/package/damn-it

## Dependencies (6)

- [xtend](https://npm.io/package/xtend.md) ~4.0.0
- [colors](https://npm.io/package/colors.md) ^1.0.3
- [resolve](https://npm.io/package/resolve.md) ^1.1.6
- [node-uuid](https://npm.io/package/node-uuid.md) ~1.4.3
- [underscore](https://npm.io/package/underscore.md) ^1.8.2
- [parse-stack](https://npm.io/package/parse-stack.md) ^0.1.3

## Recent versions

- 0.3.1 (latest) — 2015-04-18
- 0.3.0 — 2015-04-14
- 0.2.1 — 2015-04-13
- 0.2.0 — 2015-04-05
- 0.1.2 — 2015-04-01
- 0.1.1 — 2015-04-01
- 0.1.0 — 2015-03-31
- 0.0.6 — 2015-03-30
- 0.0.5 — 2015-03-29
- 0.0.4 — 2015-03-25
- 0.0.2 — 2015-03-24
- 0.0.1 — 2015-03-23

## README

## A very simple library which keeps track of errors that occur during runtime
[![NPM](https://nodei.co/npm/damn-it.png?downloads=true)](https://nodei.co/npm/damn-it/)  
[![Build Status](https://api.travis-ci.org/sadrzadehsina/damn-it.png)](https://github.com/sadrzadehsina/damn-it) [![Dependecies Status](https://david-dm.org/sadrzadehsina/damn-it.svg)](https://david-dm.org/sadrzadehsina/damn-it)
[![Coverage Status](https://coveralls.io/repos/sadrzadehsina/damn-it/badge.svg?branch=master)](https://coveralls.io/r/sadrzadehsina/damn-it?branch=master)

![](https://dl.dropboxusercontent.com/s/rc16hcg9gvd23pd/console_preview.png)

* [Install](#install)
* [Getting Started](#getting-started)
* [Options](#options)
* [Backends](#backends)
* [Test](#test)

### Install
Just like all node modules, you can run the following command to install it.
```bash
npm install damn-it
```
You can install it globally with `-g` switch. However I recommand you to install it for each of your node applications locally.

### Getting Started
The very first step of using `damn-it` is to **initialize** it.
```javascript
// instantiate and initialize it in one line
var damnIt = require("damn-it").initialize();

// or you can instantiate it first, then initialize it
var damnIt = require("damn-it");
damnIt.initialize();

```

I try to keep `damn-it` as simple at possible. I strongly think that you shouldn't be worry about your application errors. Although errors are very important, however I believe that there are other critical issuse to think of.

**_sream()_ or _silent()_** <br>
There is a very simple difference between these methods. Both of them save errors to the given backend. But the first one also log error in nodejs console, whereas, the later one just save error. Their usege are exactly the same.

**try-catch** <br>
You can use `damn-it` to handle your errors manually. If you have a _try-catch_ statement, you can save its catch error with `damn-it`.
```javascript
try {
    throw new Error("just a fake error!");
} catch(err) {
    damnIt.scream(err);
}
```

**express middleware** <br>
If your node application is based on express, you can handle your errors too.
```javascript
app.use(function( err, req, res, next ) {
  damnIt.scream(err);
  next(err);
});
```

**pure nodejs application** <br>
As you know the main purpose of `damn-it` is to save your application errors somewhere that you can see and review them easily. There is a method for error handling in nodejs which track all errors. Although it is not recommanded to use it, however for our purpose it is good.
```javascript
process.on('uncaughtException', function( err ) {
  damnIt.scream(err);
});
```
You can use it in express based applications too.

### Options
`initialize` method accept a wide range of options. There is also two helper methods namely, `set` and `get` that you can use to interact with `damn-it` options.

**backend (default="file")** <br>
It handle where errors should be save.

* backend="mysql" : save errors in a mysql table
* backend="file" : save errors in a given file name

**path (default="./errors.txt")** <br>
Given file path to save errors.

**type (default="json")** <br>
It handle the way errors are being save.

* type="json" : save errors in JSON format
* type="text" : save errors in plain text

You can set or change `damn-it` options in two different ways. <br>
In `intialize` method:
```javascript
var damnIt = require("damnIt").initialize({
   backend: "mysql",
   file: "logs.txt",
   type: "text"
});
```
Or with `set` method:
```javascript
var damnIt = require("damnIt").initialize();
damnIt.set("backend", "mysql");
damnIt.set("file", "logs.txt");
damnIt.set("type", "text");
```

### Backends
`damn-it` supports different type of backends. `File` and `Database`. You can switch between them anywhere in your application with `set` method.

**mysql backend** <br>
In order to use `mysql` backend, you should provide an open connection to your database. Moreover, you need a mysql driver to create a connection to mysql database. I suggest [node-mysql](https://github.com/felixge/node-mysql/) module which is powerful and easy to use.

I assume that you are using 'node-mysql'. First you should include it and create a connection.

```javascript
var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : '<host>',
  database : '<database_name>',
  user     : '<database_username>',
  password : '<database_password>'
});
```
Then you need to pass `connection` to `damn-it` module. You have to options as before. In the first place you can initialize `damn-it` with an option argument which contains your database connection.

```javascript
var damnIt = require("damn-it").initialize({
    backend: "mysql",
    db: connection
});
```
**Pay attention to `db` property. Its name should be `db`**.

Then every time you call `scream()` or `silent()` method, all errors will be saved into a table called `errors` in your database.

**file backend** <br>
There is nothing special about `file` backend. However you should know that if you choose it, all errors will be saved into a file called whatever you give as `file` property.

### Test
You can `test` this module with `npm test`.

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