Substrate Developer Hub

Substrate Developer Hub

  • 教程
  • 知识库
  • 进阶菜谱
  • API 文档
  • Languages icon简体中文
    • English
    • 协助翻译

›Runtime

开始

  • 总览
  • 安装
  • 在 Windows 系统开始
  • 词汇表

学习 Substrate

  • Extrinsics
  • 交易池
  • 账户摘要
  • 会话密钥
  • 交易权重
  • 链下功能

Runtime

  • Runtime 总览
  • Runtime 的基本类型
  • FRAME
  • Pallets
  • Runtime宏
  • Runtime 元数据
  • Runtime 存储
  • Runtime 来源
  • Runtime 执行流程
  • Runtime事件
  • Runtime 错误
  • 交易费用
  • 链下工作机
  • 调试
  • Runtime 测试
  • 链上随机生成
  • Runtime 升级

智能合约

  • 总览
  • ink! 智能合约
  • ink! 概念
  • ink! 开发
  • EVM 模块
  • ink! 常问问题

整合

  • Polkadot-JS
  • 客户端库
  • 链规范
  • Subkey 工具
  • 内存分析

进阶

  • SCALE 编解码器
  • 共识机制
  • 区块导入过程
  • 执行器
  • 密码学
  • 存储
  • SS58 地址格式

贡献

  • 协助翻译
Translate

Runtime 错误

Runtime代码应该显式且优雅地处理所有错误情况,也就是说runtime代码 必须是“不抛出”的,用Rust术语来讲,就是决不能"panic" 。 A common idiom for writing non-throwing Rust code is to write functions that return Result types. Result枚举类型具有一个名为 Err的变量,该变量可让函数传达执行失败的信息,以避免程序“panic”。 Dispatchable calls in the FRAME system for runtime development must return a DispatchResult type that should be a DispatchError if the dispatchable function encountered an error.

每个FRAME pallet都可以使用decl_error! 宏来自定义 DispatchError的内容。

// Errors inform users that something went wrong.
decl_error! {
    pub enum Error for Module<T: Config> {
        /// Error names should be descriptive.
        InvalidParameter,
        /// Errors should have helpful documentation associated with them.
        OutOfSpace,
    }
}

为了能在pallet中触发自定义错误,pallet的module部分必须要配置Error 类型。

decl_module! {
    pub struct Module<T: Config> for enum Call where origin: T::Origin {
        // Errors must be initialized if they are used by the pallet.
        type Error = Error<T>;

        /* --snip-- */
  }
}

Pallet 模版演示了如何正确处理可调用函数内错误的一些方法。 The FRAME Support module also includes a helpful ensure! macro that can be used to check pre-conditions and emit an errors if they are not met.

frame_support::ensure!(param < T::MaxVal::get(), Error::<T>::InvalidParameter);

后续步骤

进一步学习

  • 了解更多关于Substrate runtime开发中使用的 宏的信息。

参考文档

  • decl_error! 宏
  • decl_module! 宏
← Runtime事件交易费用 →
  • 后续步骤
    • 进一步学习
    • 参考文档
Substrate Developer Hub
开发者中心
教程知识库进阶菜谱API 文档
社区
社区主页通讯Substrate 技术聊天室Substrate 研讨会Stack Overflow推特聚会活动
更多
Substrate Builders 计划BlogSubstrate GitHub开发者中心 GitHub隐私政策使用条款Cookie 设置
Copyright © 2021 Parity Technologies