# @soleil-se/http-error

Extension of `Error` to be able to set HTTP status and send error response.

## Install

```sh
npm i @soleil-se/http-error
```

## `HttpError`

**Extends**: `Error`

### `new HttpError(message, status)`

Creates an instance of HttpError.\
[Available status codes](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes).

| Param    | Type     | Description       |
| -------- | -------- | ----------------- |
| message  | `String` | Error message     |
| status   | `Number` | HTTP status code. |
| response | `Object` | HTTP response.    |

### `HttpError.sendError(res, e)`

Helper function to set status and send error.\
Static method of `HttpError`.

| Param    | Type       | Description                      |
| -------- | ---------- | -------------------------------- |
| response | `Response` | Response from SiteVision router. |
| error    | `Error`    | Error to be sent.                |

### Example

```javascript
import HttpError from '@soleil-se/http-error';


router.get('/', (req, res) => {
  try {
    if (notFound) {
      throw new HttpError('Something was not found', 404);
    }
  } catch (e) {
    HttpError.sendError(res, e);
  }
});
```

## `BadRequestError`

**Extends**: `HttpError`

### `new BadRequestError(message)`

Creates an instance of BadRequestError.\
Has HTTP status code 400.

| Param   | Type     |
| ------- | -------- |
| message | `String` |

### Example

```javascript
import { BadRequestError } from '@soleil-se/http-error';


throw new BadRequestError('400 - Bad request!');
```

## `UnauthorizedError`

**Extends**: `HttpError`

### `new UnauthorizedError(message)`

Creates an instance of UnauthorizedError.\
Has HTTP status code 401.

| Param   | Type     |
| ------- | -------- |
| message | `String` |

### Example

```javascript
import { UnauthorizedError } from '@soleil-se/http-error';


throw new UnauthorizedError('401 - Unauthorized!');
```

## `ForbiddenError`

**Extends**: `HttpError`

### `new ForbiddenError(message)`

Creates an instance of ForbiddenError.\
Has HTTP status code 403.

| Param   | Type     |
| ------- | -------- |
| message | `String` |

### Example

```javascript
import { ForbiddenError } from '@soleil-se/http-error';


throw new ForbiddenError('403 - Forbidden!');
```

## `NotFoundError`

**Extends**: `HttpError`

### `new NotFoundError(message)`

Creates an instance of NotFoundError.\
Has HTTP status code 404.

| Param   | Type     |
| ------- | -------- |
| message | `String` |

### Example

```javascript
import { NotFoundError } from '@soleil-se/http-error';


throw new NotFoundError('404 - Not found!');
```

## `InternalServerError`

**Extends**: `HttpError`

### `new InternalServerError()`

Creates an instance of InternalServerError.\
Has HTTP status code 500.

| Param   | Type     |
| ------- | -------- |
| message | `String` |

### Example

```javascript
import { InternalServerError } from '@soleil-se/http-error';


throw new InternalServerError('500 - Internal server error!');
```

## `ServiceUnavailableError`

**Extends**: `HttpError`

### `new ServiceUnavailableError()`

Creates an instance of InternalServerError.\
Has HTTP status code 503.

| Param   | Type     |
| ------- | -------- |
| message | `String` |

### Example

```javascript
import { ServiceUnavailableError } from '@soleil-se/http-error';


throw new ServiceUnavailableError('503 - Service unavailable error!');
```

## `sendError(res, e)`

Helper function to set status and send error.

| Param | Type       | Description                      |
| ----- | ---------- | -------------------------------- |
| res   | `Response` | Response from SiteVision router. |
| e     | `Error`    | Error to be sent.                |

### Example

```javascript
import { sendError } from '@soleil-se/http-error';


router.get('', (req, res) => {
  try {
    // Do stuff
  } catch (e) {
    sendError(res, e);
  }
});
```