1.1.5 • Published 4 months ago

ts-httperror v1.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

ts-httperror

Create Http errors with custom schema for nodejs, express, etc(also work for javascript in browsers).

MIT License

Coverage Status

node

Installation

Install ts-httperror with npm.

  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.

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.
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.

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.

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

// 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'
// 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.
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).
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.

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).

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.

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

import { createHttpError, statuses } from 'ts-httperror';

const HttpError = createHttpError();

new HttpError({ status: statuses.Bad_Request });
// same to => new HttpError({ status: 400 });
1.1.5

4 months ago

1.1.4

11 months ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago