Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mensaah/exceptions_js
Create Javascript Custom Exceptions in one line
https://github.com/mensaah/exceptions_js
javscript
Last synced: 1 day ago
JSON representation
Create Javascript Custom Exceptions in one line
- Host: GitHub
- URL: https://github.com/mensaah/exceptions_js
- Owner: MeNsaaH
- License: mit
- Created: 2018-10-10T06:57:39.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2022-11-10T12:56:50.000Z (about 2 years ago)
- Last Synced: 2024-11-08T14:52:10.480Z (about 2 months ago)
- Topics: javscript
- Language: JavaScript
- Homepage:
- Size: 58.6 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
ExtendErrors
================Declaring Custom Exceptions in one line. Valid for all versions of javascript. It can find very good usage in Promises.
All standard Error methods apply to the custom Exception created, `CustomException.stack`, `CustomException.name`, `CustomException.toString()`, `CustomException.message` and others.Installation
-------------
```js
npm install extendErrors
```Usage
--------------
```js
import {Exception} from 'exception'
// const {Exception} = require('exception')
// passing the name parameter ensures the exception displays
// with the name
// Without name parameter the Exception is instantiated as
// Error: some exception message
// With name parameter, exception is instantiated as
// MyCustomError: some exception message
const MyCustomError = new Exception("MyCustomError")
const OtherError = new Exceptiontry{
// Some code to be tried
throw new MyCustomError("Something is not write");
}catch(err) {
if (err instanceof MyCustomError){
//
}
// Or more conveniently
if (err.is(MyCustomError)){
console.log(err.toString())
//some code
}
if (err.is("MyCustomError")) {
// Do Something
}}
```To create exceptions with similar traits, you can use the `Exceptions` class
```js
import {ExceptionList} from 'exception'// const {ExceptionList} = require('exception')
const NetworkErrors = ExceptionList(['SSLError', 'ConnectionLostError', 'SMTPError'])
try{
// Some block of code
throw new NetworkErrors.SSLError("Unable to establish SSL Connection")
}catch(err){
if err.is(NetworkErrors.SSLError){
// Do Something
}
if NetworkErrors.has(err){
// Do something
}
}```
For best practices, you should have all related exceptions in one file
```js
import {OtherException, PrimaryException} from '../my_exception'
// Some Error Handling Code
```Testing
--------
To test install all dependencies and then run test
```js
npm link
npm test
```