lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README | LICENSE

error.rs (1937B)


      1 use alloc::string::{String, ToString};
      2 use core::fmt::{Display, Formatter};
      3 use serde::Serialize;
      4 
      5 #[derive(Debug, Clone, Serialize)]
      6 pub enum SqlError {
      7     InvalidArgument(String),
      8     NotFound(String),
      9     SerializationError(String),
     10     InvalidQuery(String),
     11     Internal,
     12     UnsupportedPlatform,
     13 }
     14 
     15 impl SqlError {
     16     pub fn code(&self) -> &'static str {
     17         match self {
     18             SqlError::InvalidArgument(_) => "ERR_INVALID_ARGUMENT",
     19             SqlError::NotFound(_) => "ERR_NOT_FOUND",
     20             SqlError::SerializationError(_) => "ERR_SERIALIZATION",
     21             SqlError::InvalidQuery(_) => "ERR_INVALID_QUERY",
     22             SqlError::Internal => "ERR_INTERNAL",
     23             SqlError::UnsupportedPlatform => "ERR_UNSUPPORTED_PLATFORM",
     24         }
     25     }
     26 
     27     pub fn to_json(&self) -> serde_json::Value {
     28         serde_json::json!({ "code": self.code(), "message": self.to_string() })
     29     }
     30 }
     31 
     32 impl Display for SqlError {
     33     fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
     34         match self {
     35             SqlError::InvalidArgument(value) => write!(f, "invalid argument: {value}"),
     36             SqlError::NotFound(value) => write!(f, "{value} not found"),
     37             SqlError::SerializationError(value) => write!(f, "serialization error: {value}"),
     38             SqlError::InvalidQuery(value) => write!(f, "invalid query: {value}"),
     39             SqlError::Internal => f.write_str("internal error"),
     40             SqlError::UnsupportedPlatform => f.write_str("unsupported on this platform"),
     41         }
     42     }
     43 }
     44 
     45 #[cfg(feature = "std")]
     46 impl std::error::Error for SqlError {}
     47 
     48 impl From<serde_json::Error> for SqlError {
     49     fn from(e: serde_json::Error) -> Self {
     50         SqlError::SerializationError(e.to_string())
     51     }
     52 }
     53 
     54 #[cfg(all(feature = "native", feature = "std"))]
     55 impl From<rusqlite::Error> for SqlError {
     56     fn from(e: rusqlite::Error) -> Self {
     57         SqlError::InvalidQuery(e.to_string())
     58     }
     59 }