Skip to content

๐ŸŽจ Exception is a custom error library for Node.js that provides a more flexible and customizable way of handling errors.

License

Notifications You must be signed in to change notification settings

kabeep/exception

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

59 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

logo-dark

Exception is a custom error library for Node.js that provides a more flexible and customizable way of handling errors.


NodeJS License NPM Codecov Codacy CircleCI

English | ็ฎ€ไฝ“ไธญๆ–‡

Alt

inheritance-tree

Inheritance and the prototype chain

๐Ÿ“– Introduction

Inspired by the work of sindresorhus, I decided to open source the most repetitive encapsulation work I do in CLI development.

I do not like disorder. Often, unexpected situations arise due to our insufficient consideration. Therefore, I encourage those around me to engage in more comprehensive error collection work.

The goal of Exception is to transform unexpected occurrences into anticipated outcomes as much as possible.

It allows Error objects to throw exception and stack information in a more aesthetically pleasing and intuitive manner, and can also serve as Notify to output critical information in workflows.

โš™๏ธ Installation

npm install @kabeep/exception --save
yarn add @kabeep/exception
pnpm add @kabeep/exception

๐Ÿš€ Usage

Plain text or Error object

example

Plain-text-or-Error-object

import Exception from '@kabeep/exception';

// Plain text
throw new Exception('Argument example');

// or Error object
throw new Exception(new Error('Argument example'));

Using in Asynchronous Contexts

example

Using-in-Asynchronous-Contexts

import Exception from '@kabeep/exception';

(
    async () => {
        throw new Exception('Promise example');
    }
)().catch(console.log);

Custom Styles

example

Custom-Styles

import Exception from '@kabeep/exception';

// Use custom style with hex or rgb
const stylish = '(51,51,51).bg#f56c6c';

console.log(
    new Exception('Stylish example', stylish)
);

Custom Exceptions

example

Custom-Exceptions

import Exception from '@kabeep/exception';

// > Warning
class Warning extends Exception {
    constructor (message: string | Error) {
        super(message, '( 51 ,51, 51 ).bg#e6a23c');
    }
}

const warn = new Warning('Inherited example');
// Warning: Inherited example [Without style]
console.log(`${warn}`);
console.log(warn);

Print Key Information

example

Print-Key-Information

import Exception from '@kabeep/exception';

// > Info
const infoStyle = '(51,51,51).bg#409eff';

class Info extends Exception {
    constructor (message) {
        super(message, infoStyle);
    }

    toString () {
        return this.info(styles);
    }
}

const tip = new Info('Inherited example');
// Without stack
console.log(`${tip}`);

// > Success
const successStyle = '(51,51,51).bg#67c23a';

class Success extends Exception {
    constructor (message) {
        super(message, successStyle);
    }

    toString () {
        return this.info(styles);
    }
}

const pass = new Success('Inherited example');
// Without stack
console.log(pass.toString());

๐ŸŽจ Supported styles

"dim.italic.underline"
"magenta.cyan"
"bgMagenta.bgCyan"
"#fff.bg#333333"
"(51,51,51).bg(24,124,255)"
"cyan.bgDarkblue"

๐Ÿ”— Related

  • chalk - Terminal string styling done right
  • chalk-pipe - Create chalk style schemes with simpler style strings

๐Ÿค Contribution

Contributions via Pull Requests or Issues are welcome.

๐Ÿ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

About

๐ŸŽจ Exception is a custom error library for Node.js that provides a more flexible and customizable way of handling errors.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published