use std::time::Duration;
use surrealdb_types::{Error as TypesError, QueryError, ToSql};
use crate::LeafError;
use crate::fmt::SqlDuration;
#[derive(Debug, thiserror::Error)]
pub enum EngineError {
#[error("The database encountered unreachable logic: {0}")]
Unreachable(String),
#[error("Internal database error: {0}")]
Internal(String),
#[error("The query was not executed due to a cancelled transaction")]
QueryCancelled,
#[error("The query was not executed because it exceeded the timeout: {}", SqlDuration(*.0).to_sql())]
QueryTimedout(Duration),
}
impl EngineError {
#[cold]
#[track_caller]
pub fn unreachable<T: std::fmt::Display>(message: T) -> EngineError {
let location = std::panic::Location::caller();
EngineError::Unreachable(format!("{}:{}: {}", location.file(), location.line(), message))
}
}
impl LeafError for EngineError {
fn map_kind(self, message: String) -> TypesError {
match self {
EngineError::Unreachable(_) | EngineError::Internal(_) => TypesError::internal(message),
EngineError::QueryCancelled => TypesError::query(message, QueryError::Cancelled),
EngineError::QueryTimedout(duration) => TypesError::query(
message,
QueryError::TimedOut {
duration,
},
),
}
}
}