use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PluginError {
pub code: String,
pub message: String,
pub details: Option<String>,
}
impl PluginError {
pub fn new(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
code: code.into(),
message: message.into(),
details: None,
}
}
pub fn with_details(
code: impl Into<String>,
message: impl Into<String>,
details: serde_json::Value,
) -> Self {
Self {
code: code.into(),
message: message.into(),
details: Some(details.to_string()),
}
}
pub fn details_value(&self) -> Option<serde_json::Value> {
self.details
.as_deref()
.and_then(|s| serde_json::from_str(s).ok())
}
}
impl fmt::Display for PluginError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}] {}", self.code, self.message)
}
}
impl std::error::Error for PluginError {}