1use std::str::Utf8Error;
4use std::string::FromUtf8Error;
5use thiserror::Error;
6
7use crate::SqlType;
8
9#[derive(Debug, Error)]
10pub enum FbError {
11 #[error("sql error {code}: {msg}")]
12 Sql { msg: String, code: i32 },
13
14 #[error("io error: {0}")]
15 Io(#[from] std::io::Error),
16
17 #[error("error: {0}")]
18 Other(String),
19}
20
21impl From<String> for FbError {
22 fn from(msg: String) -> Self {
23 Self::Other(msg)
24 }
25}
26
27impl From<&str> for FbError {
28 fn from(msg: &str) -> Self {
29 Self::Other(msg.to_string())
30 }
31}
32
33impl From<FromUtf8Error> for FbError {
34 fn from(e: FromUtf8Error) -> Self {
35 Self::Other(format!("Found column with an invalid UTF-8 string: {}", e))
36 }
37}
38
39impl From<Utf8Error> for FbError {
40 fn from(e: Utf8Error) -> Self {
41 Self::Other(format!("Found column with an invalid UTF-8 string: {}", e))
42 }
43}
44
45pub fn err_column_null(type_name: &str) -> FbError {
46 FbError::Other(format!(
47 "This is a null value. Use the Option<{}> to safe access this column and avoid errors",
48 type_name
49 ))
50}
51
52pub fn err_type_conv<T>(from: SqlType, to: &str) -> Result<T, FbError> {
53 Err(FbError::Other(format!(
54 "Can't convert {:?} column to {}",
55 from, to
56 )))
57}