From 7046252f96c0436500a1b45c379a6aab56ee9612 Mon Sep 17 00:00:00 2001 From: elsirion Date: Fri, 13 May 2022 16:42:32 +0000 Subject: [PATCH] Impl `std::error::Error` for `RpcError` to make it anyhow compatible --- cln-rpc/src/primitives.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/cln-rpc/src/primitives.rs b/cln-rpc/src/primitives.rs index 460a2ac8b..e8a5c504b 100644 --- a/cln-rpc/src/primitives.rs +++ b/cln-rpc/src/primitives.rs @@ -1,3 +1,4 @@ +use std::fmt::{Display, Formatter}; use anyhow::Context; use anyhow::{anyhow, Error, Result}; use serde::{Deserialize, Serialize}; @@ -671,3 +672,15 @@ pub struct RpcError { pub code: Option, pub message: String, } + +impl Display for RpcError { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + if let Some(code) = self.code { + write!(f, "Error code {}: {}", code, self.message) + } else { + write!(f, "Error: {}", self.message) + } + } +} + +impl std::error::Error for RpcError {} \ No newline at end of file