Skip to main content

soil_rpc/v2/archive/
error.rs

1// This file is part of Soil.
2
3// Copyright (C) Soil contributors.
4// Copyright (C) Parity Technologies (UK) Ltd.
5// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
6
7//! Error helpers for `archive` RPC module.
8
9use jsonrpsee::types::error::ErrorObject;
10
11/// ChainHead RPC errors.
12#[derive(Debug, thiserror::Error)]
13pub enum Error {
14	/// Invalid parameter provided to the RPC method.
15	#[error("Invalid parameter: {0}")]
16	InvalidParam(String),
17	/// Runtime call failed.
18	#[error("Runtime call: {0}")]
19	RuntimeCall(String),
20	/// Failed to fetch leaves.
21	#[error("Failed to fetch leaves of the chain: {0}")]
22	FetchLeaves(String),
23}
24
25// Base code for all `archive` errors.
26const BASE_ERROR: i32 = 3000;
27/// Invalid parameter error.
28const INVALID_PARAM_ERROR: i32 = BASE_ERROR + 1;
29/// Runtime call error.
30const RUNTIME_CALL_ERROR: i32 = BASE_ERROR + 2;
31/// Failed to fetch leaves.
32const FETCH_LEAVES_ERROR: i32 = BASE_ERROR + 3;
33
34impl From<Error> for ErrorObject<'static> {
35	fn from(e: Error) -> Self {
36		let msg = e.to_string();
37
38		match e {
39			Error::InvalidParam(_) => ErrorObject::owned(INVALID_PARAM_ERROR, msg, None::<()>),
40			Error::RuntimeCall(_) => ErrorObject::owned(RUNTIME_CALL_ERROR, msg, None::<()>),
41			Error::FetchLeaves(_) => ErrorObject::owned(FETCH_LEAVES_ERROR, msg, None::<()>),
42		}
43		.into()
44	}
45}
46
47/// The error type for errors that can never happen.
48// NOTE: Can't use std::convert::Infallible because of the orphan-rule
49pub enum Infallible {}
50
51impl From<Infallible> for ErrorObject<'static> {
52	fn from(e: Infallible) -> Self {
53		match e {}
54	}
55}