受到 sindresorhus 工作的激励,我决定把自己在 cli 开发中最多重复的封装工作开源出来。
我不喜欢无序,当意外情况出现,往往由我们考虑不足导致,因此我鼓励身边的人进行更完整的错误收集工作。
Exception 的目标是尽可能的将 例外(Exception) 变为 预期(Expectation)。
它允许 Error 对象以更美化与直观的方式将异常信息和栈信息抛出, 也可以作为 Notify 在工作流中打印关键性信息。
npm install @kabeep/exception --save
yarn add @kabeep/exception
pnpm add @kabeep/exception
import Exception from '@kabeep/exception';
// Plain text
throw new Exception('Argument example');
// or Error object
throw new Exception(new Error('Argument example'));
import Exception from '@kabeep/exception';
(
async () => {
throw new Exception('Promise example');
}
)().catch(console.log);
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)
);
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);
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());
"dim.italic.underline"
"magenta.cyan"
"bgMagenta.bgCyan"
"#fff.bg#333333"
"(51,51,51).bg(24,124,255)"
"cyan.bgDarkblue"
- chalk - 终端字符串样式
- chalk-pipe - 创建粉笔风格方案与更简单的风格字符串
欢迎通过 Pull Requests 或 Issues 来贡献你的想法和代码。
本项目采用 MIT 许可证。详情请见 LICENSE 文件。