clightningrpc_plugin/
errors.rs

1//! plugin error JSONRPCv2.0 compliant module implementation
2use std::fmt;
3
4use serde::Serialize;
5
6/// Type defining JSONRPCv2.0 compliant plugin errors defined here
7/// https://www.jsonrpc.org/specification#error_object
8#[derive(Debug, Clone, Serialize)]
9pub struct PluginError {
10    code: i32,
11    #[serde(rename = "message")]
12    msg: String,
13    data: Option<serde_json::Value>,
14}
15
16impl PluginError {
17    #[allow(dead_code)]
18    pub fn new(code: i32, msg: &str, data: Option<serde_json::Value>) -> Self
19    where
20        Self: Sized,
21    {
22        PluginError {
23            code,
24            msg: msg.to_string(),
25            data,
26        }
27    }
28}
29
30impl fmt::Display for PluginError {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        write!(f, "code: {}, msg: {}", self.code, self.msg)
33    }
34}
35
36impl From<serde_json::Error> for PluginError {
37    fn from(e: serde_json::Error) -> Self {
38        PluginError {
39            code: -1,
40            msg: format!("{e}"),
41            data: None,
42        }
43    }
44}