Gathering detailed insights and metrics for ts-httperror
Gathering detailed insights and metrics for ts-httperror
Gathering detailed insights and metrics for ts-httperror
Gathering detailed insights and metrics for ts-httperror
Create Http errors with custom schema for nodejs, express, etc(also work for javascript in browsers).
npm install ts-httperror
Typescript
Module System
Min. Node Version
Node Version
NPM Version
TypeScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
44 Commits
1 Watchers
1 Branches
1 Contributors
Updated on May 05, 2023
Latest Version
1.1.5
Package Id
ts-httperror@1.1.5
Unpacked Size
32.29 kB
Size
8.62 kB
File Count
13
NPM Version
9.6.6
Node Version
18.14.0
Published on
Jan 02, 2024
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Create Http errors with custom schema for nodejs, express, etc(also work for javascript in browsers).
Install ts-httperror with npm.
1 npm install ts-httperror
NOTE: All code is written with typescript. It's work the same for javascript just remove the types(interfaces, types).
create simple HttpError class.
1import { createHttpError } from 'ts-httperror'; 2 3// use createHttpError function to create HttpError class 4const HttpError = createHttpError(); 5 6// create error object 7const error = new HttpError({ status: 404, message: 'Blog not found.' }); 8console.log(error); 9/** 10 * Not_Found: Blog not found. 11 * at new HttpError (user:\Programing\Wep\ts-httperror\test.js:56:32) 12 * at Module._extensions..js (node:internal/modules/cjs/loader:1272:10) 13 * ... 14 * status :404, 15 * text:'Not Found' 16 * 17 */ 18 19// or you can throw it immediately 20throw new HttpError({ status: 404, message: 'Blog not found.' });
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.1import { createHttpError } from 'ts-httperror'; 2 3const HttpError = createHttpError(); 4 5const error1 = new HttpError(); 6console.log(error1.status); // => 500 7console.log(error1.message); // => Internal server error 8 9const error2 = new HttpError({ status: 404 }); 10console.log(error2.status); // => 404 11console.log(error2.message); // => The requested page could not be found but may be a vailable again in the future 12 13const error3 = new HttpError({ status: 400, message: 'Validation failed' }); 14console.log(error3.status); // => 400 15console.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.
1import { createHttpError } from 'ts-httperror'; 2const HttpError = createHttpError({ 3 // change the default status from 500 to 400 4 status: 400, 5}); 6 7const error = new HttpError(); 8console.log(error.status); // => 400 9console.log(error.message); // => The request cannot be fulfilled due to bad syntax
Create custom schema for the HttpError.
1import { createHttpError } from 'ts-httperror'; 2 3//create your schema interface 4interface Schema { 5 date: Date; 6 public?: boolean; 7} 8 9const HttpError = createHttpError<Schema>({ 10 // add default values for your schema 11 public: true, 12}); 13 14const error2 = new HttpError({ 15 status: 400, 16 date: new Date('2020-01-01'), 17 public: false, 18}); 19console.log(error2.date); // => 2020-01-01T00:00:00.000Z 20console.log(error2.public); // => false 21 22const error1 = new HttpError({ 23 status: 404, 24 message: 'Blog not found', 25 date: new Date('2020-01-01'), 26}); 27console.log(error1.date); // => 2020-01-01T00:00:00.000Z 28console.log(error1.public); // => true
You can also create a custom schema for every feature in the app
1// user/users.ts 2import { createHttpError } from 'ts-httperror'; 3 4interface Schema { 5 feature?: 'users'; 6 action: 'add' | 'update' | 'delete'; 7} 8 9const HttpError = createHttpError<Schema>({ 10 // add default values for your schema 11 feature: 'users', 12}); 13 14const error = new HttpError({ status: 400, action: 'add' }); 15console.log(error.feature); // => 'users' 16console.log(error.action); // => 'add'
1// blog/blog.ts 2import { createHttpError } from 'ts-httperror'; 3 4interface Schema { 5 feature?: 'blog'; 6 action: 'add' | 'update' | 'delete'; 7} 8 9const HttpError = createHttpError<Schema>({ 10 // add default values for your schema 11 feature: 'blog', 12}); 13 14const error = new HttpError({ status: 404, action: 'delete' }); 15console.log(error.feature); // => 'blog' 16console.log(error.action); // => 'delete'
isValid
- A static method use to check if given error is valid HttpError.1import { createHttpError } from 'ts-httperror'; 2 3const HttpError = createHttpError(); 4 5console.log(HttpError.isValid(new HttpError())); // => true 6console.log(HttpError.isValid(new Error())); // => false
toClient
- method use to return HttpError object without custom schema(custom properties).1import { createHttpError } from 'ts-httperror'; 2 3interface Schema { 4 feature?: 'blog'; 5 details: string; 6 userId?: string; 7 userAgent?: string; 8 action: 'add' | 'update' | 'delete'; 9} 10 11const HttpError = createHttpError<Schema>({ 12 feature: 'blog', 13}); 14 15const error = new HttpError({ 16 status: 404, 17 action: 'delete', 18 message: 'Blog not found', 19 details: 'Blog with id=1234 not found', 20 userId: '123', 21 userAgent: '...', 22}); 23 24console.log(error.toClient()); 25/** 26 * { 27 * status:404, 28 * name:'Not_Found', 29 * text:'Not Found', 30 * message:'Blog not found' 31 * } 32 */
Create custom HttpError class with your own properites and methods by extends the HttpError class.
1import { createHttpError, Options } from 'ts-httperror'; 2 3// create your schema for HttpError 4interface Schema { 5 date: Date; 6} 7 8// create HttpError class with your schema 9const HttpError = createHttpError<Schema>(); 10 11// create HttpErrorOptions type(options is the argument for HttpError constructor) 12type HttpErrorOptions = Options<Schema>; 13 14/** 15 * create CustomHttpErrorOptions type and make it extends the HttpErroroptions 16 * (options here is the argument for CustomHttpError constructor) 17 */ 18type CustomHttpErrorOptions = HttpErrorOptions & { 19 expose?: boolean; 20}; 21 22/** 23 * create CustomHttpError class and make it extend the HttpError class 24 */ 25class CustomHttpError extends HttpError { 26 public readonly expose: boolean; 27 28 constructor({ expose, status, message, date }: CustomHttpErrorOptions) { 29 super({status, message, date}); 30 this.expose = expose || this.status < 500; 31 } 32 33 public log = (): void => { 34 console.log(this); 35 }; 36} 37 38const error = new CustomHttpError({ 39 status: 400, 40 message: 'some error occurred', 41 date: new Date('2020-01-01'), 42 expose: true, 43}); 44 45console.log(HttpError.isValid(error)); // => true 46 47console.log(error.status); // => 400 48console.log(error.expose); // => true 49 50error.log(); // will log the error 51error.toClient(); // also work
Get type for options(HttpError constructor argument).
1import { createHttpError, Options } from 'ts-httperror'; 2 3interface Schema { 4 date?: Date; 5} 6 7const HttpError = createHttpError<Schema>(); 8 9type HttpErrorOptions = Options<Schema>; 10 11const options: HttpErrorOptions = { 12 status: 404, 13 date: new Date(), 14}; 15 16const error = new HttpError(options);
Get type for HttpError object by using Hydrate.
1import { createHttpError, Hydrate } from 'ts-httperror'; 2 3interface Schema { 4 date?: Date; 5} 6 7const HttpError = createHttpError<Schema>(); 8 9// return type for HttpError object 10type HttpErrorObject = Hydrate<Schema>; 11 12const error = new HttpError(); 13 14function logError(error: HttpErrorObject) { 15 console.log(error); 16} 17 18logError(error);
increase the readability to the error by using statuses
1import { createHttpError, statuses } from 'ts-httperror'; 2 3const HttpError = createHttpError(); 4 5new HttpError({ status: statuses.Bad_Request }); 6// same to => new HttpError({ status: 400 });
No vulnerabilities found.
No security vulnerabilities found.