1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
use cosmic_hyperspace::err::{ErrKind, HyperErr};
use cosmic_space::err::SpaceErr;
use std::io::{Error, ErrorKind};
use std::string::FromUtf8Error;
use strum::ParseError;
pub trait PostErr: HyperErr + From<sqlx::Error> + From<ParseError> {
fn dupe() -> Self;
}
#[cfg(test)]
#[derive(Debug, Clone)]
pub struct TestErr {
pub message: String,
pub kind: ErrKind,
}
#[cfg(test)]
impl PostErr for TestErr {
fn dupe() -> Self {
Self {
kind: ErrKind::Dupe,
message: "Dupe".to_string(),
}
}
}
#[cfg(test)]
pub mod convert {
use crate::err::TestErr as Err;
use crate::HyperErr;
use bincode::ErrorKind;
use cosmic_hyperspace::err::ErrKind;
use cosmic_space::err::SpaceErr;
use mechtron_host::err::{DefaultHostErr, HostErr};
use sqlx::Error;
use std::io;
use std::str::Utf8Error;
use std::string::FromUtf8Error;
use strum::ParseError;
use tokio::sync::oneshot;
use tokio::time::error::Elapsed;
use wasmer::{CompileError, ExportError, InstantiationError, RuntimeError};
impl Err {
pub fn new<S: ToString>(message: S) -> Self {
Self {
message: message.to_string(),
kind: ErrKind::Default,
}
}
}
impl ToString for Err {
fn to_string(&self) -> String {
self.message.clone()
}
}
impl From<url::ParseError> for Err {
fn from(e: url::ParseError) -> Self {
Self {
kind: ErrKind::Default,
message: e.to_string(),
}
}
}
impl From<DefaultHostErr> for Err {
fn from(e: DefaultHostErr) -> Self {
Self {
kind: ErrKind::Default,
message: e.to_string(),
}
}
}
impl From<ascii::FromAsciiError<std::string::String>> for Err {
fn from(e: ascii::FromAsciiError<String>) -> Self {
Self {
kind: ErrKind::Default,
message: e.to_string(),
}
}
}
impl HyperErr for Err {
fn to_space_err(&self) -> SpaceErr {
SpaceErr::server_error(self.to_string())
}
fn new<S>(message: S) -> Self
where
S: ToString,
{
Err::new(message)
}
fn status_msg<S>(status: u16, message: S) -> Self
where
S: ToString,
{
Err::new(message)
}
fn status(&self) -> u16 {
if let ErrKind::Status(code) = self.kind {
code
} else {
500u16
}
}
fn kind(&self) -> ErrKind {
self.kind.clone()
}
fn with_kind<S>(kind: ErrKind, msg: S) -> Self
where
S: ToString,
{
Err {
kind,
message: msg.to_string(),
}
}
}
impl From<()> for Err {
fn from(_: ()) -> Self {
Err::new("empty")
}
}
impl From<ParseError> for Err {
fn from(e: ParseError) -> Self {
Err::new(e)
}
}
impl From<sqlx::Error> for Err {
fn from(e: Error) -> Self {
Err::new(e)
}
}
impl Into<SpaceErr> for Err {
fn into(self) -> SpaceErr {
SpaceErr::server_error(self.to_string())
}
}
impl From<oneshot::error::RecvError> for Err {
fn from(err: oneshot::error::RecvError) -> Self {
Err::new(err)
}
}
impl From<Elapsed> for Err {
fn from(err: Elapsed) -> Self {
Err::new(err)
}
}
impl From<String> for Err {
fn from(err: String) -> Self {
Err::new(err)
}
}
impl From<&'static str> for Err {
fn from(err: &'static str) -> Self {
Err::new(err)
}
}
impl From<SpaceErr> for Err {
fn from(err: SpaceErr) -> Self {
Err::new(err)
}
}
impl From<io::Error> for Err {
fn from(err: io::Error) -> Self {
Err::new(err)
}
}
impl From<acid_store::Error> for Err {
fn from(e: acid_store::Error) -> Self {
Err::new(e)
}
}
impl From<zip::result::ZipError> for Err {
fn from(a: zip::result::ZipError) -> Self {
Err::new(a)
}
}
impl From<Box<bincode::ErrorKind>> for Err {
fn from(e: Box<bincode::ErrorKind>) -> Self {
Err::new(e)
}
}
impl From<ExportError> for Err {
fn from(e: ExportError) -> Self {
Err::new(e)
}
}
impl From<Utf8Error> for Err {
fn from(e: Utf8Error) -> Self {
Err::new(e)
}
}
impl From<FromUtf8Error> for Err {
fn from(e: FromUtf8Error) -> Self {
Err::new(e)
}
}
impl From<InstantiationError> for Err {
fn from(_: InstantiationError) -> Self {
todo!()
}
}
impl HostErr for Err {
fn to_space_err(self) -> SpaceErr {
SpaceErr::server_error(self.to_string())
}
}
impl From<CompileError> for Err {
fn from(e: CompileError) -> Self {
Err::new(e)
}
}
impl From<RuntimeError> for Err {
fn from(e: RuntimeError) -> Self {
Err::new(e)
}
}
}