# tree-house-errors

> NodeJS default error definitions with an error parser utility function

Latest version **1.2.4** (published 2019-06-15) · ISC license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install tree-house-errors
pnpm add tree-house-errors
yarn add tree-house-errors
bun add tree-house-errors
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.2.4 |
| Published | 2019-06-15 |
| First published | 2018-03-19 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 5 |
| Unpacked size | 45 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 3 |
| Maintainers | ben-vr, dgyesbreghs, knor-el-snor, octagonal, samover, willemhorsten |
| Keywords | tree house, treehouse, errors |

## Links

- npm: https://www.npmjs.com/package/tree-house-errors
- Repository: https://github.com/icapps/tree-house-errors
- Homepage: https://github.com/icapps/tree-house-errors#readme
- Issues: https://github.com/icapps/tree-house-errors/issues
- npm.io page: https://npm.io/package/tree-house-errors

## Dependencies (5)

- [joi](https://npm.io/package/joi.md) ~14.3.1
- [i18n](https://npm.io/package/i18n.md) ~0.8.3
- [uuid](https://npm.io/package/uuid.md) ~3.3.2
- [http-status](https://npm.io/package/http-status.md) ~1.3.2
- [express-validation](https://npm.io/package/express-validation.md) ~1.0.2

## 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.2.4 (latest) — 2019-06-15
- 1.2.3 — 2019-04-04
- 1.2.2 — 2019-01-31
- 1.2.1 — 2018-11-05
- 1.2.0 — 2018-05-09
- 1.1.2 — 2018-05-02
- 1.1.1 — 2018-05-02
- 1.1.0 — 2018-04-24
- 1.0.3 — 2018-04-16
- 1.0.2 — 2018-04-16
- 1.0.1 — 2018-03-19
- 1.0.0 — 2018-03-19

## README

# Treehouse errors

Custom NodeJS error classes and definitions with an error parser utility function

[![npm version](https://badge.fury.io/js/tree-house-errors.svg)](https://badge.fury.io/js/tree-house-errors)
[![Dependencies](https://david-dm.org/icapps/tree-house-errors.svg)](https://david-dm.org/icapps/tree-house-errors.svg)
[![Build Status](https://travis-ci.org/icapps/tree-house-errors.svg?branch=master)](https://travis-ci.org/icapps/tree-house-errors)
[![Coverage Status](https://coveralls.io/repos/github/icapps/tree-house-errors/badge.svg)](https://coveralls.io/github/icapps/tree-house-errors) [![Greenkeeper badge](https://badges.greenkeeper.io/icapps/tree-house-errors.svg)](https://greenkeeper.io/)

## Installation

Install via npm

```shell
npm install tree-house-errors
```

or via yarn

```shell
yarn add tree-house-errors
```

## Error types

### ApiError

Base error class which extends from the `Error` class.

```javascript
// All keys are required
const error = {
  code: 'BAD_REQUEST_DUE_TO',
  message: 'This is a bad request',
}

// All keys are optional
const optionalArgs = {
  message: 'Overwrite the message for custom error',
  detail: 'Extra details containing pertinent information',
  stack: 'stacktrace...',
}

throw new ApiError(400, error, optionalArgs);
```

### BadRequestError

extends from ApiError with a preset of status code 400 and BAD_REQUEST as error.

```javascript
throw new BadRequestError(); // or
throw new BadRequestError(error, optionalArgs);
```

### NotFoundError

extends from ApiError with a preset of status code 404 and RESOURCE_NOT_FOUND as error.

```javascript
throw new NotFoundError(); // or
throw new NotFoundError(error, optionalArgs);
```

### ForbiddenError

extends from ApiError with a preset of status code 403 and FORBIDDEN as error.

```javascript
throw new ForbiddenError(); // or
throw new ForbiddenError(error, optionalArgs);
```

### InternalServerError

extends from ApiError with a preset of status code 500 and INTERNAL_ERROR as error.

```javascript
throw new InternalServerError(); // or
throw new InternalServerError(error, optionalArgs);
```

### UnauthorizedError

extends from ApiError with a preset of status code 401 and UNAUTHORIZED as error.

```javascript
throw new UnauthorizedError(); // or
throw new UnauthorizedError(error, optionalArgs);
```

### ValidationError

extends from ApiError with a preset of status code 400 and INVALID_INPUT as error.

```javascript
throw new ValidationError(); // or
throw new ValidationError(error, optionalArgs);
```

### AuthenticationError

extends from ApiError with a preset of status code 400 and AUTHENTICATION_FAILED as error.

```javascript
throw new AuthenticationError(); // or
throw new AuthenticationError(error, optionalArgs);
```

## Error definitions

Predefined error types that can be used over multiple projects with a message and code per type. The current list provides following errors:

```javascript
INTERNAL_ERROR:         { code: 'INTERNAL_ERROR',i18n: 'internal_error',message: 'An unkown error occurred' },
INVALID_INPUT:          { code: 'INVALID_INPUT', i18n: 'invalid_input', message: 'Invalid input provided' },
AUTHENTICATION_FAILED:  { code: 'AUTHENTICATION_FAILED', i18n: 'authentication_failed', message: 'Authentication failed' },
BAD_REQUEST:            { code: 'BAD_REQUEST', i18n: 'bad_request', message: 'Bad request' },
MISSING_HEADERS:        { code: 'MISSING_HEADERS', i18n: 'missing_headers', message: 'Missing headers' },
UNAUTHORIZED:           { code: 'UNAUTHORIZED', i18n: 'unauthorized', message: 'Unauthorized' },
FORBIDDEN:              { code: 'FORBIDDEN', i18n: 'forbidden', message: 'No access' },
RESOURCE_NOT_FOUND:     { code: 'RESOURCE_NOT_FOUND', i18n: 'resource_not_found', message: 'Resource not found' },
```

Example

```javascript
import { errorConfig as errors } from 'tree-house-errors'
throw new ApiError(400, errors.BAD_REQUEST);
```

## Error parsing

### parseErrors(error, i18nOptions (optional))

Parse any data into an error object with all properties needed for jsonade parser. Also parses `express-validation` errors.

```javascript
const error = new BadRequestError(...);
const parsedError = parseErrors(error);

// jsonade serializer afterwards (optional)
serializer.serialize([parsedError]);
```

With i18n support (optional):

```javascript
const error = new BadRequestError(...);
const parsedError = parseErrors(error, {
  defaultLocale: 'en',          // Optional (defaults to 'en')
  language: 'nl',               // Optional (defaults to 'en')
  path: __dirname = '/locales',
});

// jsonade serializer afterwards (optional)
serializer.serialize([parsedError]);
```

> The `parseErrors` function will load the i18n configuration once, and reuse the same instance afterwards. It is not possible to overwrite the configuration after the first call. This has to do with performance and caching of translations.

## Tests

- You can run `yarn test` to run all tests
- You can run `yarn test:coverage` to run all tests with coverage report

## Bugs

When you find issues, please report them:

- web: [https://github.com/icapps/tree-house-errors/issues](https://github.com/icapps/tree-house-errors/issues)

Be sure to include all of the output from the npm command that didn't work as expected. The npm-debug.log file is also helpful to provide.

## Authors

See the list of [contributors](https://github.com/icapps/tree-house-errors/contributors) who participated in this project.

## License

This project is licensed under the ISC License - see the [LICENSE.md](LICENSE.md) file for details

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