use crate::metadata::FormatConfig;
#[inline]
pub fn diagnostic_code<D: miette::Diagnostic + ?Sized>(diagnostic: &D) -> String {
diagnostic
.code()
.map(|c| c.to_string())
.unwrap_or_else(|| "UNKNOWN_ERROR".to_string())
}
#[inline]
pub fn diagnostic_help<D: miette::Diagnostic + ?Sized>(diagnostic: &D) -> Option<String> {
diagnostic.help().map(|h| h.to_string())
}
pub trait Error: std::error::Error + miette::Diagnostic {
fn to_json(&self, config: FormatConfig) -> Result<serde_json::Value, serde_json::Error>;
fn to_debug(&self) -> String
where
Self: std::fmt::Debug,
{
format!("{self:?}")
}
fn to_html(&self, config: FormatConfig) -> String;
fn to_graphql(&self, config: FormatConfig) -> Result<serde_json::Value, serde_json::Error>;
fn to_text(&self, config: FormatConfig) -> String;
fn to_jsonrpc(&self, config: FormatConfig) -> Result<serde_json::Value, serde_json::Error>;
fn http_status(&self) -> http::StatusCode;
fn http_headers(&self) -> Vec<(http::HeaderName, http::HeaderValue)>;
}