autoschematic_core/
error.rs1use std::fmt::{self, Debug};
2
3use std::path::PathBuf;
4use std::{error::Error, fmt::Display, sync::PoisonError};
5
6use serde::{Deserialize, Serialize};
7
8use crate::connector::Connector;
9
10#[derive(Default, Debug, Serialize, Deserialize, Clone)]
11pub struct ErrorMessage {
12 pub msg: String,
13}
14
15impl From<anyhow::Error> for ErrorMessage {
16 fn from(value: anyhow::Error) -> Self {
17 ErrorMessage {
18 msg: format!("{value:#}"),
19 }
20 }
21}
22
23impl<C> From<PoisonError<std::sync::RwLockReadGuard<'_, C>>> for ErrorMessage {
24 fn from(value: PoisonError<std::sync::RwLockReadGuard<'_, C>>) -> Self {
25 ErrorMessage { msg: value.to_string() }
26 }
27}
28
29impl From<PoisonError<std::sync::MutexGuard<'_, Box<dyn Connector + 'static>>>> for ErrorMessage {
30 fn from(value: PoisonError<std::sync::MutexGuard<'_, Box<dyn Connector + 'static>>>) -> Self {
31 ErrorMessage { msg: value.to_string() }
32 }
33}
34
35impl Error for ErrorMessage {}
36
37impl Display for ErrorMessage {
38 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
39 Display::fmt(&self.msg, f)
40 }
41}
42
43#[derive(Debug)]
44pub enum AutoschematicErrorType {
45 InvalidConnectorString(String),
47
48 InvalidConnectorVersion {
50 shortname: String,
51 conn_ver: String,
52 host_ver: String,
53 },
54
55 InvalidKeystoreString(String),
57
58 InvalidLockString(String),
60
61 InvalidAddr(PathBuf),
63
64 InvalidOp(PathBuf, String),
66
67 InternalError(anyhow::Error),
69}
70
71#[derive(Debug)]
72pub struct AutoschematicError {
73 pub kind: AutoschematicErrorType,
74}
75
76impl fmt::Display for AutoschematicError {
77 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78 match &self.kind {
79 AutoschematicErrorType::InvalidConnectorString(name) => {
80 write!(f, "Invalid Connector String: {name}")
81 }
82 AutoschematicErrorType::InvalidKeystoreString(name) => {
83 write!(f, "Invalid Keystore String: {name}")
84 }
85 AutoschematicErrorType::InvalidLockString(name) => {
86 write!(f, "Invalid Lock String: {name}")
87 }
88 AutoschematicErrorType::InvalidAddr(addr) => {
89 write!(f, "Invalid Address: {}", addr.display())
90 }
91 AutoschematicErrorType::InvalidOp(addr, op) => {
92 write!(f, "Invalid ConnectorOp for addr {} : {}", addr.display(), op)
93 }
94 AutoschematicErrorType::InternalError(e) => write!(f, "Internal Error: {e:#}"),
95 AutoschematicErrorType::InvalidConnectorVersion {
96 shortname,
97 conn_ver,
98 host_ver,
99 } => {
100 write!(
101 f,
102 "Protocol version for connector {} doesn't match host: connector={}, host={}",
103 shortname, conn_ver, host_ver
104 )
105 }
106 }
107 }
108}
109
110impl std::error::Error for AutoschematicError {}
111
112impl From<anyhow::Error> for AutoschematicError {
113 fn from(err: anyhow::Error) -> Self {
114 AutoschematicError {
115 kind: AutoschematicErrorType::InternalError(err),
116 }
117 }
118}
119
120impl From<tokio::task::JoinError> for AutoschematicError {
121 fn from(err: tokio::task::JoinError) -> Self {
122 AutoschematicError {
123 kind: AutoschematicErrorType::InternalError(err.into()),
124 }
125 }
126}
127
128impl<T: Send + Sync + 'static> From<tokio::sync::mpsc::error::SendError<T>> for AutoschematicError {
129 fn from(err: tokio::sync::mpsc::error::SendError<T>) -> Self {
130 AutoschematicError {
131 kind: AutoschematicErrorType::InternalError(err.into()),
132 }
133 }
134}
135
136impl From<regex::Error> for AutoschematicError {
137 fn from(err: regex::Error) -> Self {
138 AutoschematicError {
139 kind: AutoschematicErrorType::InternalError(err.into()),
140 }
141 }
142}
143
144impl From<std::io::Error> for AutoschematicError {
153 fn from(err: std::io::Error) -> Self {
154 AutoschematicError {
155 kind: AutoschematicErrorType::InternalError(err.into()),
156 }
157 }
158}
159
160impl From<git2::Error> for AutoschematicError {
161 fn from(err: git2::Error) -> Self {
162 AutoschematicError {
163 kind: AutoschematicErrorType::InternalError(err.into()),
164 }
165 }
166}
167
168impl From<serde_json::Error> for AutoschematicError {
169 fn from(err: serde_json::Error) -> Self {
170 AutoschematicError {
171 kind: AutoschematicErrorType::InternalError(err.into()),
172 }
173 }
174}
175
176impl From<hyper::header::ToStrError> for AutoschematicError {
177 fn from(err: hyper::header::ToStrError) -> Self {
178 AutoschematicError {
179 kind: AutoschematicErrorType::InternalError(err.into()),
180 }
181 }
182}
183
184impl From<tokio::sync::TryLockError> for AutoschematicError {
185 fn from(err: tokio::sync::TryLockError) -> Self {
186 AutoschematicError {
187 kind: AutoschematicErrorType::InternalError(err.into()),
188 }
189 }
190}
191
192impl From<documented::Error> for AutoschematicError {
193 fn from(err: documented::Error) -> Self {
194 AutoschematicError {
195 kind: AutoschematicErrorType::InternalError(err.into()),
196 }
197 }
198}
199
200#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
209pub struct Position {
210 pub line: usize,
211 pub col: usize,
212}
213
214#[derive(Clone, Debug, PartialEq, Eq)]
215pub struct SpannedError {
216 pub message: String,
217 pub severity: String,
218 pub position_start: Position,
219}