<commit_before># tractor-error-handler
[![npm version](https://badge.fury.io/js/tractor-error-handler.svg)](https://badge.fury.io/js/tractor-error-handler)

A general HTTP request error handler for [**tractor**](https://github.com/TradeMe/tractor).

## Usage:

```javascript
import tractorErrorHandler from 'tractor-error-handler';
import { TractorError } from 'tractor-error-handler';

export default function myApiEndpoint (request, response) {
    if (somethingBad) {
        tractorErrorHandler.handle(response, new TractorError('something bad happened'));
    } else {
        response.sendStatus(200);
    }
}
```<commit_msg>Adding API docs to READEME<commit_after># tractor-error-handler
[![npm version](https://img.shields.io/npm/v/tractor-error-handler.svg)](https://img.shields.io/npm/v/tractor-error-handler.svg)

A general HTTP request error handler for [**tractor**](https://github.com/TradeMe/tractor).

## API:

### `TractorError`:

> Creates a new `TractorError`.

> #### Arguments:
> * `message: string` - the error message
> * `status?: number` - the HTTP status of the error

> #### Usage:
> ```javascript
let error = new TractorError('something bad happened', 500);
```

### `TractorError.isTractorError`:

> Checks if something is a `TractorError`.

> #### Arguments:
> * `e: any` - thing to test

> #### Usage:
> ```javascript
TractorError.isTractorError(new TractorError('something bad happened')); // true;
TractorError.isTractorError(new Error('something bad happened')); // false;
```

### `tractorErrorHandler.handle`:

> Sends an error back to the client

> #### Arguments:
> * `response: `[Response](http://expressjs.com/es/api.html#res) - the Express HTTP response object
> * `error: TractorError` - the TractorError that was thrown

> #### Usage:
> ```javascript
import tractorErrorHandler from 'tractor-error-handler';
import { TractorError } from 'tractor-error-handler';
export default function myApiEndpoint (request, response) {
    if (somethingBad) {
        tractorErrorHandler.handle(response, new TractorError('something bad happened'));
    } else {
        response.sendStatus(200);
    }
}
```
{'fraction_non_alphanumeric': 0.16050538137576043, 'fraction_numerical': 0.004211511464670099, 'mean_word_length': 4.701333333333333, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 20, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, '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': '4998744', 'n_tokens_mistral': 688, 'n_tokens_neox': 660, 'n_words': 155}