# ts-httperror

> Create custom Http errors object for node js, express, etc in simple and efficient way.

Latest version **1.1.5** (published 2024-01-02) · MIT license · 0 weekly downloads

## Install

```sh
npm install ts-httperror
pnpm add ts-httperror
yarn add ts-httperror
bun add ts-httperror
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.5 |
| Published | 2024-01-02 |
| First published | 2023-02-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >= 6.x |
| Dependencies | 0 |
| Unpacked size | 32.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | bahaa adel |
| Maintainers | bahaa95 |
| Keywords | http, express, nodejs, error, httperror, httperrors, typescript, statuses, ts-httperror |

## Links

- npm: https://www.npmjs.com/package/ts-httperror
- Repository: https://github.com/bahaa95/ts-httperror
- Homepage: https://github.com/bahaa95/ts-httperror#readme
- Issues: https://github.com/bahaa95/ts-httperror/issues
- npm.io page: https://npm.io/package/ts-httperror

## 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.1.5 (latest) — 2024-01-02
- 1.1.4 — 2023-06-21
- 1.1.3 — 2023-05-05
- 1.1.2 — 2023-04-24
- 1.1.1 — 2023-03-17
- 1.1.0 — 2023-03-17
- 1.0.3 — 2023-02-10
- 1.0.2 — 2023-02-09
- 1.0.1 — 2023-02-04
- 1.0.0 — 2023-02-04

## README

# ts-httperror

Create Http errors with custom schema for nodejs, express, etc(also work for javascript in browsers).

[![MIT License](https://img.shields.io/badge/License-MIT-green.svg)](https://choosealicense.com/licenses/mit/)

[![Coverage Status](https://coveralls.io/repos/github/bahaa95/ts-httperror/badge.svg?branch=main)](https://coveralls.io/github/bahaa95/ts-httperror?branch=main)

[![node](https://img.shields.io/node/v/ts-httperror?color=green&label=node)](https://nodejs.org/en/download/)

## Installation

Install ts-httperror with npm.

```bash
  npm install ts-httperror
```

## Usage

**_NOTE:_** All code is written with typescript. It's work the same for javascript just remove the types(interfaces, types).

create simple HttpError class.

```typescript
import { createHttpError } from 'ts-httperror';

// use createHttpError function to create HttpError class
const HttpError = createHttpError();

// create error object
const error = new HttpError({ status: 404, message: 'Blog not found.' });
console.log(error);
/**
 * Not_Found: Blog not found.
 * at new HttpError (user:\Programing\Wep\ts-httperror\test.js:56:32)
 * at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
 * ...
 * status :404,
 * text:'Not Found'
 * 
 */

// or you can throw it immediately
throw new HttpError({ status: 404, message: 'Blog not found.' });
```

### HttpError constructor options

HttpError constructor has an argument options object with properties

- `status` - the status code for error default is `500`. We can also change the defualt status code from `500` to any status code. We will see this soon.
- `message` - error message default is message for the status code.

```typescript
import { createHttpError } from 'ts-httperror';

const HttpError = createHttpError();

const error1 = new HttpError();
console.log(error1.status); // => 500
console.log(error1.message); // => Internal server error

const error2 = new HttpError({ status: 404 });
console.log(error2.status); // => 404
console.log(error2.message); // => The requested page could not be found but may be a vailable again in the future

const error3 = new HttpError({ status: 400, message: 'Validation failed' });
console.log(error3.status); // => 400
console.log(error3.message); // => Validation failed
```

Change the defualt status from 500 to any other status. To do that just add the status property to createHttpError function.

```typescript
import { createHttpError } from 'ts-httperror';
const HttpError = createHttpError({
  // change the default status from 500 to 400
  status: 400,
});

const error = new HttpError();
console.log(error.status); // => 400
console.log(error.message); // => The request cannot be fulfilled due to bad syntax
```

### Create HttpError with custom schema

Create custom schema for the HttpError.

```typescript
import { createHttpError } from 'ts-httperror';

//create your schema interface
interface Schema {
  date: Date;
  public?: boolean;
}

const HttpError = createHttpError<Schema>({
  // add default values for your schema
  public: true,
});

const error2 = new HttpError({
  status: 400,
  date: new Date('2020-01-01'),
  public: false,
});
console.log(error2.date); // => 2020-01-01T00:00:00.000Z
console.log(error2.public); // => false

const error1 = new HttpError({
  status: 404,
  message: 'Blog not found',
  date: new Date('2020-01-01'),
});
console.log(error1.date); // => 2020-01-01T00:00:00.000Z
console.log(error1.public); // => true
```

You can also create a custom schema for every feature in the app

```typescript
// user/users.ts
import { createHttpError } from 'ts-httperror';

interface Schema {
  feature?: 'users';
  action: 'add' | 'update' | 'delete';
}

const HttpError = createHttpError<Schema>({
  // add default values for your schema
  feature: 'users',
});

const error = new HttpError({ status: 400, action: 'add' });
console.log(error.feature); // => 'users'
console.log(error.action); // => 'add'
```

```typescript
// blog/blog.ts
import { createHttpError } from 'ts-httperror';

interface Schema {
  feature?: 'blog';
  action: 'add' | 'update' | 'delete';
}

const HttpError = createHttpError<Schema>({
  // add default values for your schema
  feature: 'blog',
});

const error = new HttpError({ status: 404, action: 'delete' });
console.log(error.feature); // => 'blog'
console.log(error.action); // => 'delete'
```

### Api

- `isValid` - A static method use to check if given error is valid HttpError.

```typescript
import { createHttpError } from 'ts-httperror';

const HttpError = createHttpError();

console.log(HttpError.isValid(new HttpError())); // => true
console.log(HttpError.isValid(new Error())); // => false
```

- `toClient` - method use to return HttpError object without custom schema(custom properties).

```typescript
import { createHttpError } from 'ts-httperror';

interface Schema {
  feature?: 'blog';
  details: string;
  userId?: string;
  userAgent?: string;
  action: 'add' | 'update' | 'delete';
}

const HttpError = createHttpError<Schema>({
  feature: 'blog',
});

const error = new HttpError({
  status: 404,
  action: 'delete',
  message: 'Blog not found',
  details: 'Blog with id=1234 not found',
  userId: '123',
  userAgent: '...',
});

console.log(error.toClient());
/**
 * {
 *  status:404,
 *  name:'Not_Found',
 *  text:'Not Found',
 *  message:'Blog not found'
 * }
 */
```

### Custom HttpError Class

Create custom HttpError class with your own properites and methods by extends the HttpError class.

```typescript
import { createHttpError, Options } from 'ts-httperror';

// create your schema for HttpError
interface Schema {
  date: Date;
}

// create HttpError class with your schema
const HttpError = createHttpError<Schema>();

// create HttpErrorOptions type(options is the argument for HttpError constructor)
type HttpErrorOptions = Options<Schema>;

/**
 * create CustomHttpErrorOptions type and make it extends the HttpErroroptions
 * (options here is the argument for CustomHttpError constructor)
 */
type CustomHttpErrorOptions = HttpErrorOptions & {
  expose?: boolean;
};

/**
 * create CustomHttpError class and make it extend the HttpError class
 */
class CustomHttpError extends HttpError {
  public readonly expose: boolean;

  constructor({ expose, status, message, date }: CustomHttpErrorOptions) {
    super({status, message, date});
    this.expose = expose || this.status < 500;
  }

  public log = (): void => {
    console.log(this);
  };
}

const error = new CustomHttpError({
  status: 400,
  message: 'some error occurred',
  date: new Date('2020-01-01'),
  expose: true,
});

console.log(HttpError.isValid(error)); // => true

console.log(error.status); // => 400
console.log(error.expose); // => true

error.log(); // will log the error
error.toClient(); // also work
```

### Other features

Get type for options(HttpError constructor argument).

```typescript
import { createHttpError, Options } from 'ts-httperror';

interface Schema {
  date?: Date;
}

const HttpError = createHttpError<Schema>();

type HttpErrorOptions = Options<Schema>;

const options: HttpErrorOptions = {
  status: 404,
  date: new Date(),
};

const error = new HttpError(options);
```

Get type for HttpError object by using Hydrate.

```typescript
import { createHttpError, Hydrate } from 'ts-httperror';

interface Schema {
  date?: Date;
}

const HttpError = createHttpError<Schema>();

// return type for HttpError object
type HttpErrorObject = Hydrate<Schema>;

const error = new HttpError();

function logError(error: HttpErrorObject) {
  console.log(error);
}

logError(error);
```

increase the readability to the error by using statuses

```typescript
import { createHttpError, statuses } from 'ts-httperror';

const HttpError = createHttpError();

new HttpError({ status: statuses.Bad_Request });
// same to => new HttpError({ status: 400 });
```

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