diesel_async/mysql/
error_helper.rs1use diesel::{result::DatabaseErrorKind, ConnectionError};
2use mysql_async::Error;
3
4pub(super) struct ErrorHelper(pub(super) Error);
5
6impl From<ErrorHelper> for diesel::result::Error {
7 fn from(ErrorHelper(e): ErrorHelper) -> Self {
8 match e {
9 Error::Server(e) => {
10 let kind = match e.code {
11 1062 | 1586 | 1859 => DatabaseErrorKind::UniqueViolation,
12 1216 | 1217 | 1451 | 1452 | 1830 | 1834 => {
13 DatabaseErrorKind::ForeignKeyViolation
14 }
15 1792 => DatabaseErrorKind::ReadOnlyTransaction,
16 1048 | 1364 => DatabaseErrorKind::NotNullViolation,
17 3819 => DatabaseErrorKind::CheckViolation,
18 _ => DatabaseErrorKind::Unknown,
19 };
20 diesel::result::Error::DatabaseError(kind, Box::new(e.message) as _)
21 }
22 e => diesel::result::Error::DatabaseError(
23 DatabaseErrorKind::Unknown,
24 Box::new(e.to_string()) as _,
25 ),
26 }
27 }
28}
29
30impl From<ErrorHelper> for diesel::result::ConnectionError {
31 fn from(ErrorHelper(e): ErrorHelper) -> Self {
32 match e {
33 Error::Driver(e) => ConnectionError::BadConnection(e.to_string()),
34 Error::Io(e) => ConnectionError::BadConnection(e.to_string()),
35 Error::Other(e) => ConnectionError::BadConnection(e.to_string()),
36 Error::Server(_) => ConnectionError::CouldntSetupConfiguration(ErrorHelper(e).into()),
37 Error::Url(e) => ConnectionError::InvalidConnectionUrl(e.to_string()),
38 }
39 }
40}