use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use thiserror::Error;
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema, Error)]
#[error("{code} at {stage}: {message}")]
pub struct DashboardError {
pub code: String,
pub stage: String,
pub target_id: Option<String>,
pub message: String,
pub retryable: bool,
}
impl DashboardError {
pub fn new(
code: impl Into<String>,
stage: impl Into<String>,
target_id: Option<String>,
message: impl Into<String>,
retryable: bool,
) -> Self {
Self {
code: code.into(),
stage: stage.into(),
target_id,
message: message.into(),
retryable,
}
}
pub fn unsupported_method(method: impl AsRef<str>) -> Self {
Self::new(
"unsupported_method",
"protocol_parse",
None,
format!("unsupported dashboard IPC method {}", method.as_ref()),
false,
)
}
pub fn validation(
stage: impl Into<String>,
target_id: Option<String>,
message: impl Into<String>,
) -> Self {
Self::new("validation_failed", stage, target_id, message, false)
}
pub fn target_unavailable(
stage: impl Into<String>,
target_id: impl Into<String>,
message: impl Into<String>,
) -> Self {
Self::new(
"target_unavailable",
stage,
Some(target_id.into()),
message,
true,
)
}
}