1use rupg_common::SqlState;
6
7#[derive(Debug, Clone, PartialEq, Eq)]
9pub struct TypeError {
10 pub sqlstate: SqlState,
11 pub message: String,
12 pub detail: Option<String>,
13 pub hint: Option<String>,
14 pub context: Option<String>,
16}
17
18impl TypeError {
19 pub(crate) fn new(sqlstate: SqlState, message: String) -> TypeError {
20 TypeError { sqlstate, message, detail: None, hint: None, context: None }
21 }
22
23 pub(crate) fn syntax(type_name: &str, input: &str) -> TypeError {
25 TypeError::new(
26 SqlState::INVALID_TEXT_REPRESENTATION,
27 format!("invalid input syntax for type {type_name}: \"{input}\""),
28 )
29 }
30
31 pub(crate) fn range(input: &str, type_name: &str) -> TypeError {
33 TypeError::new(
34 SqlState::NUMERIC_VALUE_OUT_OF_RANGE,
35 format!("value \"{input}\" is out of range for type {type_name}"),
36 )
37 }
38}
39
40impl std::fmt::Display for TypeError {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 f.write_str(&self.message)
43 }
44}
45
46impl std::error::Error for TypeError {}
47
48impl From<TypeError> for rupg_common::Error {
50 fn from(error: TypeError) -> rupg_common::Error {
51 let mut out = rupg_common::Error::new(error.sqlstate, error.message);
52 if let Some(detail) = error.detail {
53 out = out.with_detail(detail);
54 }
55 if let Some(hint) = error.hint {
56 out = out.with_hint(hint);
57 }
58 out
59 }
60}