JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 5
  • Score
    100M100P100Q77308F
  • License MIT

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

Package Exports

  • @kabeep/exception
  • @kabeep/exception/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@kabeep/exception) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

expection

logo-dark

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


nodejs Version codecov License

English | 简体中文

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

inheritance-tree

class-inheritance

⚙️ Installation

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

🚀 Usage

Plain text or Error object

example

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

import Exception from '@kabeep/exception';

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

Custom Styles

example

import Exception from '@kabeep/exception';

// Use custom style with chalk color, hex and rgb
const stylish = ['51,51,51', 'bg:#f56c6c']

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

Custom Exceptions

example

import Exception from '@kabeep/exception';

// > Warning
class Warning extends Exception {
    constructor (message: any) {
        super(message, [' 51,51,51 ', 'bg:#e6a23c']);
    }
}

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

example

import Exception from '@kabeep/exception';

// > Info
class Info extends Exception {
    constructor (message: any) {
        super(message, ['51,51,51', 'bg:#409eff']);
    }

    toString () {
        return ` ${this.palette(['51,51,51', 'bg:#409eff'])(this.name)} ${this.message}`;
    }
}

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

// > Success
class Success extends Exception {
    constructor (message: any) {
        super(message, ['51,51,51', 'bg:#67c23a']);
    }

    toString () {
        return ` ${this.palette(['51,51,51', 'bg:#67c23a'])(this.name)} ${this.message}`;
    }
}

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

🤝 Contribution

Contributions via Pull Requests or Issues are welcome.

📄 License

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