use std::fmt;
use std::sync::Arc;
#[derive(Debug, Clone, thiserror::Error)]
pub enum Error {
#[error("{message}")]
Query {
message: String,
code: i64,
previous: Option<Arc<dyn std::error::Error + Send + Sync>>,
raw: Option<Arc<[u8]>>,
},
#[error("{0}")]
Validation(String),
#[error("{0}")]
Unsupported(String),
#[error("{0}")]
InvalidArgument(String),
#[error("{0}")]
InvalidValue(String),
#[error("{0}")]
Type(String),
#[error("{0}")]
NoExecutor(String),
#[error("{0}")]
Engine(String),
#[error("{message}")]
Json {
message: String,
code: i64,
},
}
impl PartialEq for Error {
fn eq(&self, other: &Self) -> bool {
self.php_class() == other.php_class() && self.to_string() == other.to_string() && self.code() == other.code()
}
}
impl Error {
pub fn new(message: impl Into<String>, code: impl Into<Code>) -> Self {
Error::Query { message: message.into(), code: code.into().0, previous: None, raw: None }
}
pub fn with_previous(
message: impl Into<String>,
code: impl Into<Code>,
previous: Arc<dyn std::error::Error + Send + Sync>,
) -> Self {
Error::Query { message: message.into(), code: code.into().0, previous: Some(previous), raw: None }
}
pub(crate) fn query(message: impl Into<String>) -> Self {
Error::new(message, 0)
}
pub(crate) fn query_bytes(message: Vec<u8>) -> Self {
match String::from_utf8(message) {
Ok(message) => Error::query(message),
Err(e) => {
let bytes = e.into_bytes();
Error::Query {
message: String::from_utf8_lossy(&bytes).into_owned(),
code: 0,
previous: None,
raw: Some(bytes.into()),
}
}
}
}
pub fn message_bytes(&self) -> std::borrow::Cow<'_, [u8]> {
match self {
Error::Query { raw: Some(raw), .. } => std::borrow::Cow::Borrowed(raw),
other => std::borrow::Cow::Owned(other.to_string().into_bytes()),
}
}
pub fn code(&self) -> i64 {
match self {
Error::Query { code, .. } | Error::Json { code, .. } => *code,
_ => 0,
}
}
pub fn previous(&self) -> Option<&Arc<dyn std::error::Error + Send + Sync>> {
match self {
Error::Query { previous, .. } => previous.as_ref(),
_ => None,
}
}
pub fn php_class(&self) -> &'static str {
match self {
Error::Query { .. } => "Utopia\\Query\\Exception",
Error::Validation(_) => "Utopia\\Query\\Exception\\ValidationException",
Error::Unsupported(_) => "Utopia\\Query\\Exception\\UnsupportedException",
Error::InvalidArgument(_) => "InvalidArgumentException",
Error::InvalidValue(_) => "ValueError",
Error::Type(_) => "TypeError",
Error::NoExecutor(_) => "BadMethodCallException",
Error::Engine(_) => "Error",
Error::Json { .. } => "JsonException",
}
}
pub fn is_query_exception(&self) -> bool {
matches!(self, Error::Query { .. } | Error::Validation(_) | Error::Unsupported(_))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Code(pub i64);
impl From<i64> for Code {
fn from(code: i64) -> Self {
Code(code)
}
}
impl From<&str> for Code {
fn from(code: &str) -> Self {
Code(match php_std::value::numeric_str(code) {
Some(_) => php_std::format::Arg::Str(code.as_bytes()).to_int(),
None => 0,
})
}
}
impl fmt::Display for Code {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
pub type Result<T, E = Error> = std::result::Result<T, E>;