1use thiserror::Error;
2
3pub type Result<T> = std::result::Result<T, Error>;
4
5#[derive(Debug, Error)]
6pub enum Error {
7 #[error("invalid input: {0}")]
8 Validation(String),
9 #[error("record not found: {0}")]
10 NotFound(String),
11 #[error("conflict: {0}")]
12 Conflict(String),
13 #[error("data directory is already open: {0}")]
14 Locked(String),
15 #[error("knowledge base is closed")]
16 Closed,
17 #[error("unsupported database schema {found}; maximum supported version is {supported}")]
18 SchemaVersion { found: i64, supported: i64 },
19 #[error("invalid vector: {0}")]
20 InvalidVector(String),
21 #[error("stale content: {0}")]
22 StaleRevision(String),
23 #[error("index unavailable: {0}")]
24 Index(String),
25 #[error("SQLite: {0}")]
26 Storage(#[from] rusqlite::Error),
27 #[error("I/O: {0}")]
28 Io(#[from] std::io::Error),
29 #[error("JSON: {0}")]
30 Json(#[from] serde_json::Error),
31}
32
33impl From<tantivy::TantivyError> for Error {
34 fn from(value: tantivy::TantivyError) -> Self {
35 Self::Index(value.to_string())
36 }
37}
38
39impl Error {
40 pub fn code(&self) -> &'static str {
41 match self {
42 Self::Validation(_) | Self::Json(_) => "validation",
43 Self::NotFound(_) => "not_found",
44 Self::Conflict(_) => "conflict",
45 Self::Locked(_) => "locked",
46 Self::Closed => "closed",
47 Self::SchemaVersion { .. } => "schema_version",
48 Self::InvalidVector(_) => "invalid_vector",
49 Self::StaleRevision(_) => "stale_revision",
50 Self::Index(_) => "index",
51 Self::Storage(_) => "storage",
52 Self::Io(_) => "io",
53 }
54 }
55}
56