Rust:区块链jsonrpsee库中的rpc错误类型

这个枚举定义了一系列错误类型,可能用于一个RPC(远程过程调用)框架或网络通信库。每个成员代表了可能出现的不同错误情况。这里是对每种错误的一个简单分析,以及它们的可能用途和哪些可能是常见的:

Error enum类型

  1. Call: 通常用于指示在远程调用过程中发生的失败,如远程函数执行失败。
  2. Transport: 通常表示网络连接问题或低级别协议层出错,这是网络通信中较常见的错误。
  3. Internal: 表示内部通信通道出现问题,可能是由于消息队列或内部状态管理问题。
  4. InvalidResponse: 当收到的响应与预期格式或数据不符时使用,通常涉及数据验证。
  5. RestartNeeded: 当后台任务被终止时使用,表明需要重启系统或服务。
  6. ParseError: 解析错误,常用于处理来自远程调用的数据解析失败,如JSON格式错误。
  7. InvalidSubscriptionId: 当使用无效的订阅ID时发生,可能涉及到事件监听或推送通知。
  8. InvalidRequestId: 当使用了无效的请求ID时发生,这通常是对请求进行追踪和匹配时的问题。
  9. UnregisteredNotification: 当客户端收到一个未注册方法的通知时使用。
  10. DuplicateRequestId: 当相同的请求ID被重复注册时使用,通常要求请求ID是唯一的。
  11. MethodAlreadyRegistered: 当尝试注册一个已经注册过的方法时使用。
  12. MethodNotFound: 当调用了一个未注册的方法时发生。
  13. SubscriptionNameConflict: 当订阅和取消订阅使用相同的方法名时发生。
  14. RequestTimeout: 请求超时,网络通信中较常见,表示远程调用没有在预期时间内完成。
  15. MaxSlotsExceeded: 当超过配置的最大请求槽位数时使用。
  16. AlreadyStopped: 尝试停止一个已经停止的服务器时使用。
  17. EmptyAllowList: 当基于HTTP头部验证的访问控制列表为空时使用。
  18. HttpHeaderRejected: 当HTTP头部验证失败时使用。
  19. ResourceAtCapacity: 当资源达到最大容量时发生。
  20. ResourceNameAlreadyTaken: 尝试注册的资源名称已经被占用时发生。
  21. ResourceNameNotFoundForMethod: 某个方法启动时找不到其所需的资源名称时使用。
  22. UninitializedMethod: 方法执行时其资源尚未初始化。
  23. MaxResourcesReached: 达到了注册资源的最大数量限制。
  24. Custom: 自定义错误,允许用户定义非标准错误。
  25. HttpNotImplemented: 在HTTP客户端中未实现的功能。
  26. EmptyBatchRequest: 空的批量请求不被允许。

在实际应用中,一些最常见的错误可能包括Call, Transport, ParseError, InvalidResponse, RequestTimeout,这些错误在网络通信和数据处理中经常遇到。其他错误如InvalidSubscriptionId, MethodNotFound, ResourceAtCapacity可能更具体地与RPC框架或特定应用程序逻辑相关。在设计错误处理策略时,开发者应该考虑到每种错误类型可能的发生频率以及其对用户体验和系统稳定性的影响。

jsonrpsee::core::Error源码:

scss 复制代码
/// Error type.
#[derive(Debug, thiserror::Error)]
pub enum Error {
	/// Error that occurs when a call failed.
	#[error("{0}")]
	Call(#[from] CallError),
	/// Networking error or error on the low-level protocol layer.
	#[error("Networking or low-level protocol error: {0}")]
	Transport(#[source] anyhow::Error),
	/// Frontend/backend channel error.
	#[error("Frontend/backend channel error: {0}")]
	Internal(#[from] futures_channel::mpsc::SendError),
	/// Invalid response,
	#[error("Invalid response: {0}")]
	InvalidResponse(Mismatch<String>),
	/// The background task has been terminated.
	#[error("The background task been terminated because: {0}; restart required")]
	RestartNeeded(String),
	/// Failed to parse the data.
	#[error("Parse error: {0}")]
	ParseError(#[from] serde_json::Error),
	/// Invalid subscription ID.
	#[error("Invalid subscription ID")]
	InvalidSubscriptionId,
	/// Invalid request ID.
	#[error("Invalid request ID")]
	InvalidRequestId,
	/// Client received a notification with an unregistered method
	#[error("Unregistered notification method")]
	UnregisteredNotification(String),
	/// A request with the same request ID has already been registered.
	#[error("A request with the same request ID has already been registered")]
	DuplicateRequestId,
	/// Method was already registered.
	#[error("Method: {0} was already registered")]
	MethodAlreadyRegistered(String),
	/// Method with that name has not yet been registered.
	#[error("Method: {0} has not yet been registered")]
	MethodNotFound(String),
	/// Subscribe and unsubscribe method names are the same.
	#[error("Cannot use the same method name for subscribe and unsubscribe, used: {0}")]
	SubscriptionNameConflict(String),
	/// Request timeout
	#[error("Request timeout")]
	RequestTimeout,
	/// Configured max number of request slots exceeded.
	#[error("Configured max number of request slots exceeded")]
	MaxSlotsExceeded,
	/// Attempted to stop server that is already stopped.
	#[error("Attempted to stop server that is already stopped")]
	AlreadyStopped,
	/// List passed into access control based on HTTP header verification.
	#[error("Must set at least one allowed value for the {0} header")]
	EmptyAllowList(&'static str),
	/// Access control verification of HTTP headers failed.
	#[error("HTTP header: `{0}` value: `{1}` verification failed")]
	HttpHeaderRejected(&'static str, String),
	/// Failed to execute a method because a resource was already at capacity
	#[error("Resource at capacity: {0}")]
	ResourceAtCapacity(&'static str),
	/// Failed to register a resource due to a name conflict
	#[error("Resource name already taken: {0}")]
	ResourceNameAlreadyTaken(&'static str),
	/// Failed to initialize resources for a method at startup
	#[error("Resource name `{0}` not found for method `{1}`")]
	ResourceNameNotFoundForMethod(&'static str, &'static str),
	/// Trying to claim resources for a method execution, but the method resources have not been initialized
	#[error("Method `{0}` has uninitialized resources")]
	UninitializedMethod(Box<str>),
	/// Failed to register a resource due to a maximum number of resources already registered
	#[error("Maximum number of resources reached")]
	MaxResourcesReached,
	/// Custom error.
	#[error("Custom error: {0}")]
	Custom(String),
	/// Not implemented for HTTP clients.
	#[error("Not implemented")]
	HttpNotImplemented,
	/// Empty batch request.
	#[error("Empty batch request is not allowed")]
	EmptyBatchRequest,
}
相关推荐
mit6.8243 小时前
论容器化 | 分析Go和Rust做医疗的后端服务
docker·golang·rust
Source.Liu7 小时前
【unitrix】 4.21 类型级二进制数基本结构体(types.rs)
rust
SoniaChen338 小时前
Rust基础-part2-变量和可变类型
开发语言·后端·rust
寻月隐君8 小时前
Rust 错误处理终极指南:从 panic! 到 Result 的优雅之道
后端·rust·github
CHANG_THE_WORLD1 天前
Rustup 安装加速:使用国内镜像源解决下载慢问题
rust·rustup
萧曵 丶1 天前
Rust 仿射类型(Affine Types)
rust·仿射类型
寻月隐君1 天前
Rust核心利器:枚举(Enum)与模式匹配(Match),告别空指针,写出优雅健壮的代码
后端·rust·github
泊浮目2 天前
生产级Rust代码品鉴(一)RisingWave一条SQL到运行的流程
大数据·后端·rust
得物技术2 天前
从Rust模块化探索到DLB 2.0实践|得物技术
rust
寻月隐君2 天前
不止于后端:Rust 在 Web 开发中的崛起之路 (2024数据解读)
后端·rust·github