# professor-chaos

> non-life-threatening chaos in your programs

Latest version **0.0.1** (published 2015-08-22) · ISC license · 0 weekly downloads

## Install

```sh
npm install professor-chaos
pnpm add professor-chaos
yarn add professor-chaos
bun add professor-chaos
```

## 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.0.1 |
| Published | 2015-08-22 |
| First published | 2015-08-22 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Chris Andrejewski |
| Maintainers | andrejewski |
| Keywords | chaos, random, crash, test, error |

## Links

- npm: https://www.npmjs.com/package/professor-chaos
- Repository: https://github.com/andrejewski/professor-chaos
- Homepage: https://github.com/andrejewski/professor-chaos#readme
- Issues: https://github.com/andrejewski/professor-chaos/issues
- npm.io page: https://npm.io/package/professor-chaos

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads
- [aws-elasticsearch-connector](https://npm.io/package/aws-elasticsearch-connector.md) — 37.4K weekly downloads

## Recent versions

- 0.0.1 (latest) — 2015-08-22

## README

![Professor Chaos](https://raw.githubusercontent.com/andrejewski/professor-chaos/master/professor.jpg)

# Professor Chaos

Professor Chaos is a tool to help unexpectently break your programs to see how they handle errors.

```sh
npm install professor-chaos
```

## Usage

Professor Chaos has two methods of destruction: **time** and **call** based error triggers.
These errors are instances of `ChaosError` as to be more easily identifiable.

```js
var pc = require('professor-chaos');

/*
  Calling `timer` will set an error to be thrown within a given timeframe.
*/
var clock = pc.timer(1000);

/*
  Calling `fault` creates a break point where an error will be thrown/handled
  within a random number of `hasError` calls between a given interval.
*/
var fault = pc.fault(10);

function handler(value1, value2, callback) {
  if(fault.hasError()) {
    fault.error('Random handler error', callback);
  } else {
    // do something
  }
}

```

## Methods

### timer

```js
/*
timer(max: number): number
timer(min: number, max: number): number
*/
```

Accepts a required maximum time `max` to wait to throw an error.
Accepts an optional minimum time `min` to wait to throw an error which defaults to zero.
Returns the `setTimeout` ID to allow you to use `clearTimeout` if necessary.

This function will asynchronously throw an error some time between `min` and `max`.

### fault

```js
/*
fault(max: number): Fault
fault(min: number, max: number): Fault
*/
```

Accepts a required maximum `max` to count to until throwing an error.
Accepts an optional minimum `min` to count to until throwing an error which defaults to zero.
Returns a `Fault` object.

This method creates a `Fault` object to be used inside the scope of a given function
 to create an error to then either throw or pass to a provided callback.

```js
/*
interface Fault {
  hasError(): boolean
  error(): void
  error(message: string): void
  error(callback: (error) => any): void
  error(message: string, callback: (error) => any): void
}
*/
```

The `hasError()` method returns whether or not an error if ready to be thrown.
The `hasError()` method is managed internally to see how often it is called
 and will signal `error()` to throw somewhere within the given `min` and `max` parameters.

The `error()` method accepts an optional error message which by default is awesome.
The `error()` method accepts an optional callback function which receives the
 error that would otherwise be thrown if no callback is provided.

Note: `error()` will continue to create random errors after the first and each
 will be just as unpredictable as the last.

## Contributing

We can always have more tests: if you find a bug, create an issue or be **fabulous**
and fix the problem and write the tests up yourself in a coherent pull request.

Run tests with the `npm test` command.

Follow me on [Twitter](http://chrisandrejewski.com/twitter) for updates or just for the lolz
and please check out my other [repositories](https://github.com/andrejewski) if I have earned it.
I thank you for reading.

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