1use std::io;
9use std::num::TryFromIntError;
10
11use thiserror::Error;
12
13use crate::Group;
14use crate::Id;
15use crate::Vertex;
16
17#[derive(Debug, Error)]
19pub enum DagError {
20 #[error("{0:?} cannot be found")]
22 VertexNotFound(Vertex),
23
24 #[error("{0:?} cannot be found")]
26 IdNotFound(Id),
27
28 #[error("NeedSlowPath: {0}")]
30 NeedSlowPath(String),
31
32 #[error("ProgrammingError: {0}")]
35 Programming(String),
36
37 #[error("bug: {0}")]
39 Bug(String),
40
41 #[error(transparent)]
43 Backend(Box<BackendError>),
44
45 #[error("out of space for group {0:?}")]
47 IdOverflow(Group),
48
49 #[error(transparent)]
51 IntOverflow(#[from] TryFromIntError),
52}
53
54#[derive(Debug, Error)]
55pub enum BackendError {
56 #[error("{0}")]
57 Generic(String),
58
59 #[error(transparent)]
60 Io(#[from] std::io::Error),
61
62 #[cfg(any(test, feature = "indexedlog-backend"))]
63 #[error(transparent)]
64 IndexedLog(#[from] indexedlog::Error),
65
66 #[error(transparent)]
69 Other(#[from] anyhow::Error),
70}
71
72impl From<BackendError> for DagError {
73 fn from(err: BackendError) -> DagError {
74 DagError::Backend(Box::new(err))
75 }
76}
77
78#[cfg(any(test, feature = "indexedlog-backend"))]
79impl From<indexedlog::Error> for DagError {
80 fn from(err: indexedlog::Error) -> DagError {
81 DagError::Backend(Box::new(BackendError::from(err)))
82 }
83}
84
85impl From<io::Error> for DagError {
86 fn from(err: io::Error) -> DagError {
87 DagError::Backend(Box::new(BackendError::from(err)))
88 }
89}
90
91pub fn bug<T>(message: impl ToString) -> crate::Result<T> {
93 Err(DagError::Bug(message.to_string()))
94}
95
96pub fn programming<T>(message: impl ToString) -> crate::Result<T> {
98 Err(DagError::Programming(message.to_string()))
99}
100
101pub trait NotFoundError {
102 fn not_found_error(&self) -> DagError;
103
104 fn not_found<T>(&self) -> crate::Result<T> {
105 Err(self.not_found_error())
106 }
107}
108
109impl NotFoundError for Id {
110 fn not_found_error(&self) -> DagError {
111 ::fail::fail_point!("dag-not-found-id");
112 DagError::IdNotFound(self.clone())
113 }
114}
115
116impl NotFoundError for Vertex {
117 fn not_found_error(&self) -> DagError {
118 ::fail::fail_point!("dag-not-found-vertex");
119 DagError::VertexNotFound(self.clone())
120 }
121}