<commit_before># Whoops

<p align="center">
  <img src="https://i.imgur.com/93fMUWX.png" alt="whoops">
</p>

![Last version](https://img.shields.io/github/tag/Kikobeats/whoops.svg?style=flat-square)
[![Build Status](http://img.shields.io/travis/Kikobeats/whoops/master.svg?style=flat-square)](https://travis-ci.org/Kikobeats/whoops)
[![Coverage Status](https://img.shields.io/coveralls/Kikobeats/whoops.svg?style=flat-square)](https://coveralls.io/github/Kikobeats/whoops)
[![Dependency status](http://img.shields.io/david/Kikobeats/whoops.svg?style=flat-square)](https://david-dm.org/Kikobeats/whoops)
[![Dev Dependencies Status](http://img.shields.io/david/dev/Kikobeats/whoops.svg?style=flat-square)](https://david-dm.org/Kikobeats/whoops#info=devDependencies)
[![NPM Status](http://img.shields.io/npm/dm/whoops.svg?style=flat-square)](https://www.npmjs.org/package/whoops)
[![Donate](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square)](https://paypal.me/kikobeats)

> It makes simple throw qualified errors. Inspired in [errno](https://github.com/rvagg/node-errno) and [create-error-class](https://github.com/floatdrop/create-error-class).

## Why

- An easy way to create qualified errors.
- Using the standard Error interface in browser and NodeJS.
- Attach extra information, depending of your case of use.
- Pretty output inspired in [Elm](https://twitter.com/GregorySchier/status/732830868562182144).

Basically turns:

```js
var error = Error('ENOFILE, Something is wrong')
error.name = 'DAMNError'
error.code = 'ENOFILE'

throw error // => 'DAMNError: ENOFILE, Something is wrong'
```

into one line error declaration:

```js
var Whoops = require('Whoops');
var error = Whoops('DAMError', 'ENOFILE', 'Something is wrong');

throw error // => 'DAMNError: ENOFILE, Something is wrong'
```

Also you can create custom class errors for avoid write the name of the error
all the time:

```js
var DAMError = Whoops.create('DAMError')
```

Now you can avoid the first parameter in the inline declaration:

```js
var error = DAMError('ENOFILE', 'Something is wrong');
throw error // => 'DAMNError: ENOFILE, Something is wrong'
```

Constructor also can be executed in object mode for the cases where you need to
setup more properties associated with the error:

```js
var error = Whoops({
  name: 'DAMError', , ''
  code: 'ENOFILE'
  message: 'Something is wrong'
  path: 'filepath'
});

In this mode you can pass a generator function as message:

var error = Whoops({
  name: 'DAMError', , ''
  code: 'ENOFILE',
  file: 'damnfile'
  message: function() {
    return "Something is wrong with the file '" + this.file "'."
  }
});

throw error // => "DAMNError: ENOFILE, Something is wrong with the file 'damnfile'"
```

## Install

```bash
npm install whoops --save
```

## API

### .constructor([String|Object])

Create a new `Error`. You can use it using two different interfaces.

#### String Constructor

Following the schema `.constructor([name], [code], {message})`

#### Object Constructor

Whatever property that you pass in an object will be associated with the `Error`.

If you pass a function as `message` property will be executed in the context
of the `Error`.

For both constructor modes, if the `code` of the error is provided will be
concatenated and the begin of the `message`.

### .create({String})

Create a new qualified `Error`. All is the same than the normal constructor,
but in this case you don't have to provide the `name` of the error.

## Extra: Always throw/return an Error!

If you code implementation is

- **synchronous**, throws `Error`. If you just return the `Error` nothings happens!.
- **asynchronous**, returns `Error` in the first argument of the callback (or using promises).

About asynchronous code, is correct return a `Object` that is not a `Error` in the first argument of the callback to express unexpected behavior, but the `Object` doesn't have a type and definitely can't  follow a error interface for determinate a special behavior:

```js
callback('LOL something was wrong') // poor
callback({message: 'LOL something was wrong' } // poor, but better
callback(Whoops('LOL, something was wrong') // BEST!
```

Passing always an `Error` you can can associated different type of error with different behavior:

```js
switch (err.name) {
  case 'JSONError':
    console.log('your error logic here');
    break;
  default:
    console.log('undefined code');
    break;
};
```

## License

MIT © [<NAME>](http://www.kikobeats.com)<commit_msg>Update docs<commit_after># whoops

<p align="center">
  <img src="https://i.imgur.com/93fMUWX.png" alt="whoops">
</p>

![Last version](https://img.shields.io/github/tag/Kikobeats/whoops.svg?style=flat-square)
[![Build Status](http://img.shields.io/travis/Kikobeats/whoops/master.svg?style=flat-square)](https://travis-ci.org/Kikobeats/whoops)
[![Coverage Status](https://img.shields.io/coveralls/Kikobeats/whoops.svg?style=flat-square)](https://coveralls.io/github/Kikobeats/whoops)
[![Dependency status](http://img.shields.io/david/Kikobeats/whoops.svg?style=flat-square)](https://david-dm.org/Kikobeats/whoops)
[![Dev Dependencies Status](http://img.shields.io/david/dev/Kikobeats/whoops.svg?style=flat-square)](https://david-dm.org/Kikobeats/whoops#info=devDependencies)
[![NPM Status](http://img.shields.io/npm/dm/whoops.svg?style=flat-square)](https://www.npmjs.org/package/whoops)
[![Donate](https://img.shields.io/badge/donate-paypal-blue.svg?style=flat-square)](https://paypal.me/kikobeats)

> It makes simple throw qualified errors. Inspired in [errno](https://github.com/rvagg/node-errno) and [create-error-class](https://github.com/floatdrop/create-error-class).

## Why

- An easy way to create qualified errors.
- Using the standard `Error` interface in browser and NodeJS.
- Attach extra information, depending of your case of use.

This library is a compromise to provide a clean API for use `Error` native class.

## Install

```bash
npm install whoops --save
```

Basically it turns:

```js
var error = Error('Something is wrong')
error.name = 'DAMNError'
throw error
// => 'DAMNError: ENOFILE, Something is wrong'
```

Into a one line more productive declaration:

```js
var whoops = require('whoops');
var error = whoops('DAMError', 'Something is wrong');
throw error
// => 'DAMNError: Something is wrong'
```

## Creating an Error

This interface follow the one line principle. All the things that you can declare in this mode are:

#### message

Always that you want to create an `Error` need to provide a message for description.

```js
var userError = whoops('username already taken');
throw userError
// => 'Error: username already taken'
```

In the `Error` object you have:

```js
{
  name: 'Error',
  message: 'username already taken'
}
```

#### code

When you need to handle different errors, you need to distinguish the `Error`. For this purpose, you need yo associated a identifier that recognize each `Error`.

```js
var userError = whoops('ENAME', 'username already taken');
throw userError
// => 'Error: ENAME, username already taken'
```

In the error object you have:

```js
{
  name: 'Error',
  code: 'ENAME'
  message: 'username already taken'
}
```

## Creating qualified Errors

If you need to create a set of qualified errors associate with an `Error` name, you can create a specific error factory for this purpose.

```js
var USRError = whoops.create('USRError')
throw USRError('ENAME', 'username already taken')
// => 'USRError: ENAME, username already taken'
```

Notes how now `USRError` is an instance of `Error`.

## Attaching more information

Because `Error` is an `object`, you can attach more information.

For do that, you can feel more comfortable using the object interface:

```js
var error = USRError({
  code: 'ENAME',
  username: '@kikobeats',
  message: function() {
    return "username '" + this.username "' already taken"
  }
});

throw error
// => 'USRError: ENAME, username '@kikobeats' already taken'
```

Each property that you provide in this mode, will be attached to the final `Error`.

Also as you can see you can setup the message of the error dynamically.

This mode could be used without using qualified errors as well:

```js
var error = whoops({
  name: 'USRError',
  code: 'ENAME',
  username: '@kikobeats',
  message: function() {
    return "username '" + this.username "' already taken"
  }
});
```

## API

### .constructor([String|Object])

Create a new `Error`. You can use it using two different interfaces.

#### String Constructor

Following the schema `.constructor([code], {message})`

#### Object Constructor

Whatever property that you pass in an object will be associated with the `Error`.

If you pass a function as `message` property will be executed in the context
of the `Error`.

For both constructor modes, if the `code` of the error is provided will be
concatenated and the begin of the `message`.

### .create({String})

Create a new qualified `Error`. All is the same than the normal constructor,
but in this case you don't have to provide the `name` of the error.

## Extra: Always throw/return an Error!

If you code implementation is

- **synchronous**, throws `Error`. If you just return the `Error` nothings happens!.
- **asynchronous**, returns `Error` in the first argument of the callback (or using promises).

About asynchronous code, is correct return a `Object` that is not a `Error` in the first argument of the callback to express unexpected behavior, but the `Object` doesn't have a type and definitely can't  follow a error interface for determinate a special behavior:

```js
callback('LOL something was wrong') // poor
callback({message: 'LOL something was wrong' } // poor, but better
callback(whoops('LOL, something was wrong') // BEST!
```

Passing always an `Error` you can can associated different type of error with different behavior:

```js
switch (err.name) {
  case 'JSONError':
    console.log('your error logic here');
    break;
  default:
    console.log('undefined code');
    break;
};
```

## License

MIT © [<NAME>](http://www.kikobeats.com)
{'fraction_non_alphanumeric': 0.13605712019040064, 'fraction_numerical': 0.00218167393891313, 'mean_word_length': 4.590354767184036, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, 'https://': 25, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2415075', 'n_tokens_mistral': 3208, 'n_tokens_neox': 3071, 'n_words': 1097}