errorspack
v1.1.4
Published
an package for new errors, custom error and handle errors
Downloads
99
Maintainers
Readme
ERRORS PACK
An error pack built with pure JavaScript With help of chalk.
FEATURES
- Built-In Errors
- Create Custom Errors
- Handle errors
- Http Errors
- Warning
INSTALLING
for starting you have to install the package by running
npm i errorspack
import the package by typing
const Error = require("errorspack")
on your file.
FUNCTIONS / BUILT-IN ERRORS
There are built in errors in this pack
SOME BUILT-IN ERRORS AND FUCTIONS ARE
- NonStringError() - Which is for non string value for example here is a demo code:
const Errors = require("errorspack");
const Name = {
name: Sam
}
if(typeof(Name) !== "string") {
throw new Errors.nonStringError("Type of name must be number");
}
and the output will be:
TypeError: Type of name must be string
- NonIntegerError() - which is for non integer value for example here ia another demo code:
const Errors = require("errorspack");
const Age = {
age: "26"
}
if(typeof(Age) !== "number") {
throw new Errors.nonIntegerError("Type of age should be an Integer");
}
and the output will be:
TypeError: Type of name must be integer/number value
- NonBooleanError() - which is for non boolean value (true/false)
HOW TO CREATE CUSTOM ERROR?
custom error can be created by a class.
you have to create a class by named anything you like. After name you have to add
extends. extends will be: extends CustomError
for more easier here is an example:
const { CustomError } = require('errorspack');
const Info = {
name:
}
class MyError extends CustomError {
constructor(
name = 'MyError',
statusCode = '404',
description = 'it\'s my error',
isOperational: true
) {
super(name, statusCode, description, isOperational)
}
if(typeof(Info.name === 'undifined')) {
throw new MyError();
}
}
VERSION 2.1
WARNING
Please note that version 0.2.0 has some changes if you have any errors with old method then please use
<packagename>.builtInErrors.<ErrorName>
ERROR HANDLING WITH ERRORSPACK
To handle a error with errorspack is very simple. First you have to create a variable with any name and the value will be a constructor.
const { Handler, uncaughtExpectationHandler, UnhandledPromiseHandler } = require('errorspack');
const Hanling = new Handler();
In handler function you have to put
'UNCAUGHT_EXPECTATION', 'UNHANDLED_REJECTION'
And the code will be:
const { Handler, uncaughtExpectationHandler, UnhandledPromiseHandler } = require('errorspack');
const Hanling = new Handler('UNCAUGHT_EXPECTATION', 'UNHANDLED_REJECTION');
Now you have to do is call the handler Here is a code:
const { Handler, uncaughtExpectationHandler, UnhandledPromiseHandler } = require('errorspack');
const Hanling = new Handler('UNCAUGHT_EXPECTATION', 'UNHANDLED_REJECTION');
unhandledPromiseHandler();
uncaughtExpectationHandler('An error caught');
NOTE: unhandledPromiseHandler()
is unhandledRejection
ANTI-CRASH
Anti-Crash is same as handler but it is more easier
const { antiCrash } = require('errorspack');
antiCrash('Handled error');
easy to use :)
VERSION 1.0.0
1.0.0 is just released we have now http error
there are 3 differents http error
- Api400ErrorBadRequest()
- Api404ErrorNotFound()
- Api500ErrorInternalServer()
these errors are the child of BaseError
.
NOTE: BASE ERROR CAN'T BE USED BY YOU its simple to throw all the errors just:
throw new Api404ErrorNotFound();
it doesn't have any extra parameters so you can't edit the message its for every http errors.
VERSION 1.1
In version 1.1 there are warning
now you can create warning. It used console.warn()
but it is more flexible and easy.
using is like throwing error
throw new Warn('message here');
YOU'VE JUST LEARNED BASIC ABOUT ERRORSPACK!
STAY UPDATED