use std::{error::Error, fmt, sync::Arc};
use gpui::SharedString;
#[derive(Clone)]
pub struct QueryError {
message: SharedString,
source: Option<Arc<dyn Error + Send + Sync>>,
}
impl QueryError {
#[must_use]
pub fn msg(message: impl Into<SharedString>) -> Self {
Self {
message: message.into(),
source: None,
}
}
#[must_use]
pub fn cancelled() -> Self {
Self::msg("the task was cancelled")
}
#[must_use]
pub fn message(&self) -> &SharedString {
&self.message
}
#[must_use]
pub fn source_error(&self) -> Option<&(dyn Error + Send + Sync + 'static)> {
self.source.as_deref()
}
#[must_use]
pub fn downcast_ref<E: Error + 'static>(&self) -> Option<&E> {
self.source.as_deref()?.downcast_ref::<E>()
}
}
impl<E: Error + Send + Sync + 'static> From<E> for QueryError {
fn from(error: E) -> Self {
Self {
message: error.to_string().into(),
source: Some(Arc::new(error)),
}
}
}
impl fmt::Display for QueryError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(&self.message)
}
}
impl fmt::Debug for QueryError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("QueryError")
.field("message", &self.message)
.finish_non_exhaustive()
}
}
impl PartialEq for QueryError {
fn eq(&self, other: &Self) -> bool {
self.message == other.message
}
}