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,
}
相关推荐
DongLi011 天前
rustlings 学习笔记 -- exercises/05_vecs
rust
番茄灭世神2 天前
Rust学习笔记第2篇
rust·编程语言
shimly1234562 天前
(done) 速通 rustlings(20) 错误处理1 --- 不涉及Traits
rust
shimly1234562 天前
(done) 速通 rustlings(19) Option
rust
@atweiwei2 天前
rust所有权机制详解
开发语言·数据结构·后端·rust·内存·所有权
shimly1234562 天前
(done) 速通 rustlings(24) 错误处理2 --- 涉及Traits
rust
shimly1234562 天前
(done) 速通 rustlings(23) 特性 Traits
rust
shimly1234563 天前
(done) 速通 rustlings(17) 哈希表
rust
shimly1234563 天前
(done) 速通 rustlings(15) 字符串
rust
shimly1234563 天前
(done) 速通 rustlings(22) 泛型
rust