referrerpolicy=no-referrer-when-downgrade

sc_rpc_spec_v2/bitswap/
error.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! Error helpers for the `bitswap` RPC module.
20
21use jsonrpsee::types::error::ErrorObject;
22
23/// Bitswap RPC errors.
24#[derive(Debug, thiserror::Error)]
25pub enum Error {
26	/// Invalid CID parameter.
27	#[error("Invalid CID: {0}")]
28	InvalidCid(String),
29	/// Transaction not found.
30	#[error("Transaction not found")]
31	NotFound,
32	/// Node is performing major sync.
33	#[error("Node is major syncing")]
34	MajorSyncing,
35	/// Internal error. Never emitted in practice.
36	///
37	/// Do not render the wrapped error to not expose the internal state to the remote caller.
38	#[error("Internal error")]
39	Internal(#[from] sp_blockchain::Error),
40	/// Caller passed more CIDs than `bitswap_unstable_stream` will accept.
41	#[error("Too many CIDs: max {max}, got {got}")]
42	TooManyCids {
43		/// Maximum number of CIDs accepted in a single request.
44		max: usize,
45		/// Number of CIDs the caller passed.
46		got: usize,
47	},
48	/// Caller passed an empty `cids` array.
49	#[error("Input cids array is empty")]
50	EmptyCids,
51	/// Caller passed the same CID twice (string-equal or decoding to the same digest).
52	#[error("Input contains duplicate CIDs")]
53	DuplicateCids,
54}
55
56/// Bitswap JSON-RPC error categories, according to the spec.
57///
58/// Note: `-32811 FailRetry` is part of the per-CID error matrix in the spec but is not emitted by
59/// this implementation, which only distinguishes permanent failure (`Fail`) and backoff-eligible
60/// transient failure (`FailRetryBackoff`).
61#[derive(Debug)]
62enum ErrorCode {
63	/// Standard JSON-RPC invalid-params. Used per-CID for malformed/unsupported CIDs.
64	InvalidParams = -32602,
65	/// Top-level: `cids` length exceeds implementation maximum.
66	TooManyCids = -32801,
67	/// Top-level: `cids` array is empty.
68	EmptyCids = -32802,
69	/// Top-level: `cids` contains duplicates (string-equal or digest-equal).
70	DuplicateCids = -32803,
71	/// Per-CID permanent failure (e.g. data not found). Must not retry.
72	Fail = -32810,
73	/// Per-CID transient failure; can retry with a backoff of 1-5 seconds.
74	FailRetryBackoff = -32812,
75}
76
77impl From<Error> for ErrorObject<'static> {
78	fn from(e: Error) -> Self {
79		let msg = e.to_string();
80		let code = match e {
81			Error::InvalidCid(_) => ErrorCode::InvalidParams,
82			Error::NotFound => ErrorCode::Fail,
83			Error::MajorSyncing => ErrorCode::FailRetryBackoff,
84			// This error is never emitted in practice and is only needed to cover all
85			// compile-time variants that `BlockBackend::indexed_transaction` returns.
86			// It is unclear what error category to use in case of internal errors, let's use
87			// `FailRetryBackoff`.
88			Error::Internal(_) => ErrorCode::FailRetryBackoff,
89			Error::TooManyCids { .. } => ErrorCode::TooManyCids,
90			Error::EmptyCids => ErrorCode::EmptyCids,
91			Error::DuplicateCids => ErrorCode::DuplicateCids,
92		};
93		ErrorObject::owned(code as i32, msg, None::<()>)
94	}
95}