use thiserror::Error;
pub type FieldError = (String, String);
#[derive(Debug, Error)]
pub enum Error {
#[error("validation failed: {0:?}")]
Validation(Vec<FieldError>),
#[error("not found: {0}")]
ProjectNotFound(String),
#[error("not found: {0}")]
NamedNotFound(String),
#[error("not found")]
NotFound,
#[error("store writer task unavailable")]
WriterGone,
#[error(
"unrecognized database schema version(s) {found:?} (this build expects {expected}); \
the database at this path was not written by a known cliban"
)]
SchemaUnknown { found: Vec<i64>, expected: i64 },
#[error(transparent)]
Sqlite(#[from] rusqlite::Error),
#[error(transparent)]
Json(#[from] serde_json::Error),
}
impl Error {
pub fn validation(field: &str, message: &str) -> Self {
Error::Validation(vec![(field.to_string(), message.to_string())])
}
pub fn has_validation(&self, field: &str, needle: &str) -> bool {
match self {
Error::Validation(errs) => errs.iter().any(|(f, m)| f == field && m.contains(needle)),
_ => false,
}
}
}
pub type Result<T> = std::result::Result<T, Error>;