#error #report #error-reporting #appear #bug #english #unify

bin+lib easy-err

An easy Error wrapper

4 releases (1 stable)

new 1.0.1 Jan 18, 2025
0.3.2 Jan 4, 2025
0.2.1 Jan 1, 2025
0.1.0 Dec 31, 2024

#2 in #appear

Download history 443/week @ 2024-12-31 23/week @ 2025-01-07

466 downloads per month

MIT/Apache

11KB
123 lines

Easy-Err

简介 Introduce

  • 方便地将各种不同类型的错误统一为同种错误,实现了简单易用的报告功能。
    It is easy to unify various types of errors into the same error, enabling easy-to-use reporting.

  • 本文件中出现的所有英文使用微软翻译翻译,如有问题不关我事。(doge)
    All English language that appears in this document is translated using Microsoft Translator, if there is no problem. (doge)


怎么报告BUG How to report Bugs

BUG REPORT:
- 您的名字 (What's your name):
  如果您报告的BUG被修复,您的名字将会出现在贡献名单中,可以选择匿名。  
  If your reported bug is fixed, your name will appear in the contributors list, optionally anonymously.
- 怎么回事 (What's the matter):
  描述BUG出现的情况。  
  Describe the situation in which the bug occurred.
- 代码是啥 (What's the code):
  引发BUG的代码,或者复现此BUG的方法。  
  The code that caused the bug, or the method to reproduce the bug.

功能 Features

  • 格式化的错误输出 (Formatted output)
  • 错误类型间的转换 (Transitions between error types)
  • Result<T, E>处理 (Handling of 'Result<T, E>')

例子 Example

例子1 (Ex.1)

基础用法 (How to use me basically.)

use easy_err::Error;

// 自定义一个错误 (Custom an error.)
let custom_err = Error::custom("Here is a custom error.");

// 将错误写入日志文件 (Write the error into a file.)
custom_err.log_to("./log.txt");

log.txt中的输出 (Output in log.txt):

error:
	 time: <ErrorTime>
	  msg: Here is a custom error.

例子2 (Ex.2)

进阶用法 (How to use me more advanced.)

use std::fs;
use easy_err::{Error, ErrResult};

// 定义一个返回 `Result<T, easy_err::Error>` 的函数 (Define a function that returns the `Result<T, easy_err::Error>`.)
fn test() -> Result<(), Error> {
	fs::File::open("An obvious mistake.")?;
	Ok(())
}

// 调用 test() (Call the function.)
test().panic();

终端中的输出 (Output in Terminal):

thread 'main' panicked at F:\Projects\Rust\err\src\err.rs:196:25:

         time: 2025-01-18 15:40:11.947089800 +08:00
          msg: 系统找不到指定的文件。 (os error 2)

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\easy-err.exe` (exit code: 101)

例子3 (Ex.3)

进阶用法 (How to use me more advanced.)

use easy_err::*;

// 使用 `custom!()` 宏产生一个携带错误位置信息的自定义错误 ()
let custom_err = custom!("Here is a error.");
custom_err.report();

终端中的输出 (Output in Terminal):

error:
         time: 2025-01-18 15:42:36.892958700 +08:00
          msg: Here is a error.
        where: src/main.rs:4

依赖 Dependencies

  • colored:
    用于输出颜色字符串,在Error::report()中使用以格式化输出。
    Used to output a color string, used in 'Error::report()' to format the output.
  • chrono:
    用于获取系统时间,在Error中使用以保存错误发生时间。
    Used to get the system time, used in Error to save the time when the error occurred.

Dependencies

~1–8MB
~52K SLoC