1 个不稳定版本
0.0.1 | 2023年8月15日 |
---|
#39 在 #exception
用于 surrealism
67KB
1K SLoC
Except-Plugin
- 作者:[email protected]
- 文档名称:Except-Plugin README
- 创建日期:2023年8月14日
- 更新日期:2023年8月14日
- 版本:0.0.1
- 电子邮件:[email protected]
LICEMSE
MIT
Except-Plugin 简介
exception-plugin 是 Rust 中常见的异常处理库,帮助开发者更好地控制程序。
默认支持异常
- SuperException:它是所有异常的最高级别实现,你可以从所有更具体的异常的恢复参数中获取它。虽然它是所有异常的父类,但实际上它是最低级别的异常。
- EasyException:与 SuperException 类似,它也没有特殊功能,但有两个额外的属性:错误文件和错误行。
- NullPointerException
- ArrayIndexOutOfBoundsException
- UnSupportedOpException:UnsupportedOpException 是一种广泛的错误,包括许多可能的原因,例如权限不足、无法访问的文件、IO 异常等。
- SQLException:SQLException 是在访问或操作数据库时可能发生的错误,例如没有表、空表数据、没有列等。
快速入门
use std::error::Error;
use std::{line, file};
use std::path::PathBuf;
use except_plugin::{
ExceptionLevel, ExceptionFactory, EasyException, EasyExceptionBuilder, NewFrom, SuperBuilderImpl,
DerefException, Exception, TargetParamImpl, CommonParamImpl, easy_e,
};
pub fn test_easy() -> Result<(), Box<dyn Error>> {
/// create a easy exception
/// you can use macro to create : easy_e!(666)
/// if you wanna use macro , you should add feature : macros
let e = ExceptionFactory::new::<EasyException, EasyExceptionBuilder>()
.set_code(500)
.set_level(ExceptionLevel::Warn)
.set_line(line!())
.set_path(PathBuf::from(file!()))
.build();
dbg!(&e);
Err(Box::new(
e
))
}
定义一个自定义异常
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use std::ops::Deref;
use std::time::Duration;
use except_plugin::{
SuperBuilderImpl, Exception, ExceptionLevel, exception_impl, display_err_impl, Exceptions, builder_impl,
NewFrom, FromBuilder, ExceptionFactory,
};
/// # step1 : create a new exception
///
/// mut contain 4 param!:
/// - code
/// - msg
/// - level
/// - timestamp
#[derive(Debug, Clone, PartialEq, Default)]
pub struct MyException {
code: u32,
msg: String,
level: ExceptionLevel,
timestamp: Duration,
/// here I define a new param!
define: String,
}
/// # step2 : use macro to impl Exception for MyException
/// if you wanna impl Exception trait , you must impl Error,Debug,Display,as followings:
/// ```rust
/// impl Error for MyException {}
/// impl Debug for MyException {}
/// impl Display for MyException {}
/// impl Exception for MyException { // fn ... }
/// ```
/// it is very complicated , so you can use exception_impl! and display_err_impl!
display_err_impl!(MyException);
exception_impl!(MyException,Exceptions::Define);
/// # step3 : impl the other param for define Exception
/// actually , now it is enough
/// but you need to continue!!!
impl MyException {
pub fn get_define(&self) -> &str { &self.define }
pub fn set_define(&mut self, value: &str) -> &mut Self {
self.define = String::from(value);
self
}
}
/// # step4 : create a builder for define Exception
/// builder is quite simple and you should add a param : `e_type: Exceptions`
#[derive(Debug, Clone, PartialEq, Default)]
pub struct MyExceptionBuilder {
code: u32,
msg: String,
level: ExceptionLevel,
timestamp: Duration,
/// here I define a new param!
define: String,
e_type: Exceptions,
}
impl MyExceptionBuilder {
pub fn get_define(&self) -> &str { &self.define }
pub fn set_define(&mut self, value: &str) -> &mut Self {
self.define = String::from(value);
self
}
}
/// # step5 : add builder_impl! macro for Define Exception
builder_impl!(MyExceptionBuilder,MyException);
/// # step6 : impl NewFrom and FromBuilder
impl NewFrom for MyException {
type Builder = MyExceptionBuilder;
fn new() -> Self::Builder {
MyExceptionBuilder::new()
}
fn from_super(e: Box<dyn Exception>) -> Self where Self: Sized {
MyException {
code: e.code(),
msg: String::from(e.msg()),
level: e.level(),
timestamp: e.timestamp(),
define: String::new(),
}
}
}
impl FromBuilder for MyException {
type Output = MyException;
type Input = MyExceptionBuilder;
fn from_builder(builder: &Self::Input) -> Self::Output {
Self::Output {
code: builder.code(),
msg: String::from(builder.msg()),
level: builder.level(),
timestamp: builder.timestamp(),
define: String::from(builder.get_define()),
}
}
}
/// # step 7: test
pub fn test_my_exception() -> () {
// [src\lib\define_exception.rs:110] my_e = MyException {
// code: 1000,
// msg: "",
// level: Info,
// timestamp: 0ns,
// define: "this is my exception",
// }
let my_e = ExceptionFactory::new::<MyException, MyExceptionBuilder>()
.set_code(1000)
.set_define("this is my exception")
.build();
dbg!(my_e);
}