Skip to content

Commit

Permalink
add data field to RpcError
Browse files Browse the repository at this point in the history
  • Loading branch information
daywalker90 authored and nepet committed Nov 1, 2023
1 parent 28fd70a commit 39b7d6f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
18 changes: 14 additions & 4 deletions cln-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ pub mod model;
pub mod notifications;
pub mod primitives;

use crate::model::IntoRequest;
pub use crate::{
model::{Request, Response},
notifications::Notification,
primitives::RpcError,
};
use crate::model::IntoRequest;

///
pub struct ClnRpc {
Expand Down Expand Up @@ -66,11 +66,13 @@ impl ClnRpc {
let req = serde_json::to_value(req).map_err(|e| RpcError {
code: None,
message: format!("Error parsing request: {}", e),
data: None,
})?;
let req2 = req.clone();
self.write.send(req).await.map_err(|e| RpcError {
code: None,
message: format!("Error passing request to lightningd: {}", e),
data: None,
})?;

let mut response = self
Expand All @@ -80,10 +82,12 @@ impl ClnRpc {
.ok_or_else(|| RpcError {
code: None,
message: "no response from lightningd".to_string(),
data: None,
})?
.map_err(|_| RpcError {
code: None,
message: "reading response from socket".to_string(),
data: None,
})?;
trace!("Read response {:?}", response);

Expand All @@ -95,6 +99,7 @@ impl ClnRpc {
serde_json::from_value(response).map_err(|e| RpcError {
code: None,
message: format!("Malformed response from lightningd: {}", e),
data: None,
})
} else if let Some(e) = response.get("error") {
let e: RpcError = serde_json::from_value(e.clone()).unwrap();
Expand All @@ -103,12 +108,17 @@ impl ClnRpc {
Err(RpcError {
code: None,
message: format!("Malformed response from lightningd: {}", response),
data: None,
})
}
}

pub async fn call_typed<R: IntoRequest>(&mut self, request: R) -> Result<R::Response, RpcError> {
Ok(self.call(request.into())
pub async fn call_typed<R: IntoRequest>(
&mut self,
request: R,
) -> Result<R::Response, RpcError> {
Ok(self
.call(request.into())
.await?
.try_into()
.expect("CLN will reply correctly"))
Expand All @@ -121,7 +131,7 @@ where
T: Clone,
{
// TODO Find a better way to check, possibly without cloning
let f = f.clone();
let f = f.clone();
f.is_none() || f.unwrap().is_empty()
}

Expand Down
2 changes: 2 additions & 0 deletions cln-rpc/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use anyhow::{anyhow, Error, Result};
use bitcoin::hashes::Hash as BitcoinHash;
use serde::{Deserialize, Serialize};
use serde::{Deserializer, Serializer};
use serde_json::Value;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
use std::string::ToString;
Expand Down Expand Up @@ -776,6 +777,7 @@ impl<'de> Deserialize<'de> for Routehint {
pub struct RpcError {
pub code: Option<i32>,
pub message: String,
pub data: Option<Value>,
}

impl Display for RpcError {
Expand Down

0 comments on commit 39b7d6f

Please sign in to comment.