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
//! Error type for the connection

use std::str::Utf8Error;
use std::string::FromUtf8Error;
use thiserror::Error;

use crate::SqlType;

#[derive(Debug, Error)]
pub enum FbError {
    #[error("sql error {code}: {msg}")]
    Sql { msg: String, code: i32 },

    #[error("io error: {0}")]
    Io(#[from] std::io::Error),

    #[error("error: {0}")]
    Other(String),
}

impl From<String> for FbError {
    fn from(msg: String) -> Self {
        Self::Other(msg)
    }
}

impl From<&str> for FbError {
    fn from(msg: &str) -> Self {
        Self::Other(msg.to_string())
    }
}

impl From<FromUtf8Error> for FbError {
    fn from(e: FromUtf8Error) -> Self {
        Self::Other(format!("Found column with an invalid UTF-8 string: {}", e))
    }
}

impl From<Utf8Error> for FbError {
    fn from(e: Utf8Error) -> Self {
        Self::Other(format!("Found column with an invalid UTF-8 string: {}", e))
    }
}

pub fn err_column_null(type_name: &str) -> FbError {
    FbError::Other(format!(
        "This is a null value. Use the Option<{}> to safe access this column and avoid errors",
        type_name
    ))
}

pub fn err_type_conv<T>(from: SqlType, to: &str) -> Result<T, FbError> {
    Err(FbError::Other(format!(
        "Can't convert {:?} column to {}",
        from, to
    )))
}