concordium_rust_sdk/
endpoints.rs1use crate::types;
4use derive_more::From;
5use thiserror::Error;
6use tonic::metadata::errors::InvalidMetadataValue;
7pub use tonic::transport::{Endpoint, Error};
8
9#[derive(Error, Debug)]
10pub enum RPCError {
12 #[error("Call failed: {0}")]
13 CallError(#[from] tonic::Status),
14 #[error(transparent)]
15 InvalidMetadata(#[from] InvalidMetadataValue),
16 #[error("Error parsing JSON result: {0}")]
17 ParseError(#[from] anyhow::Error),
18}
19
20impl From<serde_json::Error> for RPCError {
21 fn from(x: serde_json::Error) -> Self {
22 Self::ParseError(x.into())
23 }
24}
25
26impl From<semver::Error> for RPCError {
27 fn from(x: semver::Error) -> Self {
28 Self::ParseError(x.into())
29 }
30}
31
32impl RPCError {
33 pub fn is_invalid_argument(&self) -> bool {
38 match self {
39 RPCError::CallError(e) => {
40 matches!(e.code(), tonic::Code::InvalidArgument)
41 }
42 RPCError::InvalidMetadata(_) => false,
43 RPCError::ParseError(_) => false,
44 }
45 }
46
47 pub fn is_duplicate(&self) -> bool {
50 match self {
51 RPCError::CallError(e) => {
52 matches!(e.code(), tonic::Code::AlreadyExists)
53 }
54 RPCError::InvalidMetadata(_) => false,
55 RPCError::ParseError(_) => false,
56 }
57 }
58}
59
60#[derive(Error, Debug)]
61pub enum QueryError {
64 #[error("RPC error: {0}")]
65 RPCError(#[from] RPCError),
67 #[error("Requested object not found.")]
68 NotFound,
70}
71
72impl QueryError {
73 pub fn is_not_found(&self) -> bool {
75 match self {
76 QueryError::RPCError(c) => {
77 if let RPCError::CallError(ce) = c {
78 ce.code() == tonic::Code::NotFound
79 } else {
80 false
81 }
82 }
83 QueryError::NotFound => true,
84 }
85 }
86}
87
88impl From<tonic::Status> for QueryError {
89 fn from(s: tonic::Status) -> Self {
90 Self::RPCError(s.into())
91 }
92}
93
94impl From<InvalidMetadataValue> for QueryError {
95 fn from(s: InvalidMetadataValue) -> Self {
96 Self::RPCError(s.into())
97 }
98}
99
100pub type RPCResult<A> = Result<A, RPCError>;
103
104pub type QueryResult<A> = Result<A, QueryError>;
107
108#[derive(Clone, Copy, Debug, From)]
111pub enum BlocksAtHeightInput {
112 Absolute {
113 height: types::AbsoluteBlockHeight,
115 },
116 Relative {
118 genesis_index: types::GenesisIndex,
120 height: types::BlockHeight,
122 restrict: bool,
126 },
127}