Skip to main content

trino_rust_client/
error.rs

1use reqwest::header::HeaderName;
2use reqwest::StatusCode;
3use serde::Deserialize;
4use thiserror::Error;
5
6use crate::models::QueryError;
7
8#[derive(Error, Debug)]
9pub enum Error {
10    #[error("invalid catalog")]
11    InvalidCatalog,
12    #[error("catalog not found")]
13    CatalogNotFound,
14    #[error("invalid schema")]
15    InvalidSchema,
16    #[error("schema not found")]
17    SchemaNotFound,
18    #[error("schema already exists")]
19    SchemaAlreadyExists,
20    #[error("invalid source")]
21    InvalidSource,
22    #[error("invalid user")]
23    InvalidUser,
24    #[error("invalid properties")]
25    InvalidProperties,
26    #[error("invalid table property: {0}")]
27    InvalidTableProperty(String),
28    #[error("table not found")]
29    TableNotFound,
30    #[error("table already exists")]
31    TableAlreadyExists,
32    #[error("duplicate header")]
33    DuplicateHeader(HeaderName),
34    #[error("invalid empty auth")]
35    EmptyAuth,
36    #[error("forbidden: {message}")]
37    Forbidden { message: String },
38    #[error("basic auth can not be used with http")]
39    BasicAuthWithHttp,
40    #[error("http error, reason: {0}")]
41    HttpError(#[source] Box<reqwest::Error>),
42    #[error("http not ok, code: {0}, reason: {1}")]
43    HttpNotOk(StatusCode, String),
44    #[error("query error, reason: {0}")]
45    QueryError(Box<QueryError>),
46    #[error("inconsistent data")]
47    InconsistentData,
48    #[error("empty data")]
49    EmptyData,
50    #[error("reach max attempt: {0}")]
51    ReachMaxAttempt(usize),
52    #[error("invalid host: {0}")]
53    InvalidHost(String),
54    #[error("internal error: {0}")]
55    InternalError(String),
56}
57
58impl From<reqwest::Error> for Error {
59    fn from(err: reqwest::Error) -> Self {
60        Error::HttpError(Box::new(err))
61    }
62}
63
64impl From<QueryError> for Error {
65    fn from(err: QueryError) -> Self {
66        Error::QueryError(Box::new(err))
67    }
68}
69
70pub type Result<T> = std::result::Result<T, Error>;
71
72#[derive(Debug, Deserialize)]
73pub struct TrinoRetryResult {
74    pub id: String,
75    #[serde(rename = "infoUri")]
76    pub info_uri: String,
77    pub stats: TrinoStats,
78    pub error: Option<TrinoError>,
79    #[serde(rename = "updateType")]
80    pub update_type: Option<String>,
81    #[serde(rename = "updateCount")]
82    pub update_count: Option<u64>,
83}
84
85#[derive(Debug, Deserialize)]
86pub struct TrinoStats {
87    pub state: String,
88}
89
90#[derive(Debug, Deserialize)]
91pub struct TrinoError {
92    pub message: String,
93    #[serde(rename = "errorCode")]
94    pub error_code: i64,
95    #[serde(rename = "errorName")]
96    pub error_name: String,
97    #[serde(rename = "errorType")]
98    pub error_type: String,
99    #[serde(rename = "errorLocation")]
100    pub error_location: Option<TrinoErrorLocation>,
101}
102
103#[derive(Debug, Deserialize)]
104pub struct TrinoErrorLocation {
105    #[serde(rename = "lineNumber")]
106    pub line_number: i64,
107    #[serde(rename = "columnNumber")]
108    pub column_number: i64,
109}
110
111impl From<TrinoError> for Error {
112    fn from(error: TrinoError) -> Self {
113        match error.error_name.as_str() {
114            // CATALOG ERRORS
115            "CATALOG_NOT_FOUND" => Error::CatalogNotFound,
116            "MISSING_CATALOG_NAME" => Error::InvalidCatalog,
117
118            // SCHEMA ERRORS
119            "SCHEMA_NOT_FOUND" => Error::SchemaNotFound,
120            "MISSING_SCHEMA_NAME" => Error::InvalidSchema,
121            "SCHEMA_ALREADY_EXISTS" => Error::SchemaAlreadyExists,
122
123            // TABLE ERRORS
124            "INVALID_TABLE_PROPERTY" => Error::InvalidTableProperty(error.message),
125            "TABLE_NOT_FOUND" => Error::TableNotFound,
126            "TABLE_ALREADY_EXISTS" => Error::TableAlreadyExists,
127
128            // OTHER ERRORS
129            _ => Error::InternalError(format!(
130                "Trino error: {} - {}",
131                error.error_name, error.message
132            )),
133        }
134    }
135}