1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use diesel::result::*;

use nature_common::*;

pub struct DbError;

impl DbError {
    // put it aside because can't find diesel's Timeout Error
    pub fn from(err: Error) -> NatureError {
        Self::from_with_msg(err, "")
    }

    pub fn from_with_msg(err: Error, msg: &str) -> NatureError {
        match err {
            Error::DatabaseError(kind, info) => match kind {
                DatabaseErrorKind::UniqueViolation => NatureError::DaoDuplicated(msg.to_string()),
                DatabaseErrorKind::__Unknown => NatureError::EnvironmentError(format!("{:?}", info)),
                _ => NatureError::SystemError(format!("{:?}", info)),
            }
            _ => NatureError::SystemError(err.to_string()),
        }
    }
}