error.rs
5.13 KB
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
use std::{fmt::Display, pin::Pin, sync::Arc};
use actix_rt::blocking::BlockingError;
use actix_web::{http::{StatusCode, header::ToStrError}, ResponseError};
use async_std::channel::SendError;
use diesel::result;
use diffy::{ParsePatchError, ApplyError};
use image::ImageError;
use mime::FromStrError;
use r2d2;
use rexiv2::Rexiv2Error;
use crate::{Pool, models::image::Image};
type ParentError = Option<Pin<Box<dyn std::error::Error>>>;
#[derive(Debug)]
pub struct Error {
source :ParentError,
message :String,
status :Option<StatusCode>
}
unsafe impl Send for Error {}
pub(crate) type Result<T> = std::result::Result<T, Error>;
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source.as_deref()
}
}
impl ResponseError for Error {
fn status_code(&self) -> StatusCode {
self.status.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error { source: Some(source), message, status } =>
write!( f
, "[{}] {}: {}"
, status.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
, message
, source ),
Error { source: None, message, status } =>
write!( f
, "[{}] {}"
, status.unwrap_or(StatusCode::INTERNAL_SERVER_ERROR)
, message ),
}
}
}
impl Error {
pub(crate) fn new(message :&str, status :StatusCode) -> Self {
Self { source: None
, message: String::from(message)
, status: Some(status)
}
}
}
impl From<result::Error> for Error {
fn from(source: result::Error) -> Self {
Self { source: Some(Box::pin(source))
, message: String::from("Diesel Result Error")
, status: Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
impl From<r2d2::Error> for Error {
fn from(source: r2d2::Error) -> Self {
Self { source: Some(Box::pin(source))
, message: String::from("R2D2 Pool Error")
, status: Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
impl From<std::io::Error> for Error {
fn from(source: std::io::Error) -> Self {
Self { source: Some(Box::pin(source))
, message: String::from("IO Error")
, status: Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
impl From<std::str::Utf8Error> for Error {
fn from(source: std::str::Utf8Error) -> Self {
Self { source: Some(Box::pin(source))
, message: String::from("UTF8 Error")
, status: Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
impl From<ParsePatchError> for Error {
fn from(source: ParsePatchError) -> Self {
Self { source: Some(Box::pin(source))
, message: String::from("Diffy Error")
, status: Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
impl From<ApplyError> for Error {
fn from(source: ApplyError) -> Self {
Self { source: Some(Box::pin(source))
, message: String::from("Diffy Error")
, status: Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
impl From<uuid::Error> for Error {
fn from(source: uuid::Error) -> Self {
Self { source: Some(Box::pin(source))
, message: String::from("UUID error")
, status: Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
impl From<FromStrError> for Error {
fn from(source: FromStrError) -> Self {
Self { source: Some(Box::pin(source))
, message: String::from("Mime error")
, status: Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
impl From<ToStrError> for Error {
fn from(source: ToStrError) -> Self {
Self { source: Some(Box::pin(source))
, message: String::from("Header error")
, status: Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
impl From<SendError<(Arc<Pool>, Image)>> for Error {
fn from(source: SendError<(Arc<Pool>, Image)>) -> Self {
Self { source: Some(Box::pin(source))
, message: String::from("Image Worker send error")
, status: Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
impl From<ImageError> for Error {
fn from(source: ImageError) -> Self {
Self { source: Some(Box::pin(source))
, message: String::from("Image processing error")
, status: Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
impl From<Rexiv2Error> for Error {
fn from(source: Rexiv2Error) -> Self {
Self { source: Some(Box::pin(source))
, message: String::from("Exiv error")
, status: Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
impl From<BlockingError<Error>> for Error {
fn from(source: BlockingError<Error>) -> Self {
Self { source: Some(Box::pin(source))
, message: String::from("web::block error")
, status: Some(StatusCode::INTERNAL_SERVER_ERROR)
}
}
}