1 个不稳定版本
0.1.0 | 2022年2月15日 |
---|
#1746 在 Rust 模式
15KB
212 行
user-panic
根据错误生成自定义的恐慌消息。
通过调用自定义函数并使用 Yaml 文件来生成自定义结构来处理恐慌。
这允许为不同的错误提供不同的错误消息,并允许用户运行一些简单的修复(如果可能)。
输出示例
API 错误恐慌输出的示例
The Program Crashed
Error: There was an error during the API request
It seems like an error that can be fixed by you!
Please follow the following instructions to try and fix the Error
1: Try to check your Internet Connection.
2: Check if your API request quota has been exhausted.
1. Instructions on how
2. to check
3. API quota
If the error still persists
Contact the Developer at [email protected]
代码示例
要复制上述输出,您需要首先创建一个如下所示的 yaml 文件。
API:
message: There was an error during the API request
fix instructions:
- Try to check your Internet Connection.
- Check if your API request quota has been exhausted.
- - Instructions on how
- to check
- API quota
然后,您需要创建一个 构建脚本,确保在 cargo.toml 文件中 userpanic 在依赖和构建依赖项中存在
[dependencies]
user-panic = "0.1.0"
[build-dependencies]
user-panic = "0.1.0"
并按照如下方式创建 build.rs 文件
fn main() {
println!("cargo:rerun-if-changed=errors.yaml");
println!("cargo:rerun-if-changed=build.rs");
userpanic::panic_setup!("errors.yaml"); // Enter the yaml file path here
}
这将在 src 目录中创建 panic_strucs.rs
文件。该文件可以导入并用于 panic_any 来显示自定义的恐慌。
mod panic_structs;
use std::panic::panic_any;
use crate::panic_structs::API;
fn main(){
// This sets the custom hook for panic messages
userpanic::set_hooks(Some("If the error still persists\nContact the developer at [email protected]"));
// If None is passed then No developer info/message is shown.
panic_any(API);
}
依赖项
~275KB