Derive Macro ErrorCode

Source
#[derive(ErrorCode)]
{
    // Attributes available to this derive:
    #[error_code]
}
Expand description

§ErrorCode 宏

ErrorCode 宏用于为枚举变体定义错误码,并生成一个 error_code 方法。

§使用方法

  • 在枚举上添加 #[derive(ErrorCode)]
  • 使用 #[error_code(type = "u16", default = 300)] 指定错误码类型和默认值。
  • 在每个变体上使用 #[error_code(value)] 设置具体的错误码。

§示例

#[derive(ErrorCode)]
#[error_code(type = "u16", default = 300)]
enum MyError {
    #[error_code(100)]
    InvalidInput,
    #[error_code(200)]
    NotFound,
}