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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use crate::protocol::util;
use crate::HdbResult;
use byteorder::{LittleEndian, ReadBytesExt};
use cesu8;
use std::error::Error;
use std::fmt;
use std::io;
#[derive(Clone, Debug)]
pub enum Severity {
Warning,
Error,
Fatal,
__UNKNOWN__(i8),
}
impl Severity {
pub(crate) fn from_i8(i: i8) -> Severity {
match i {
0 => Severity::Warning,
1 => Severity::Error,
2 => Severity::Fatal,
i => Severity::__UNKNOWN__(i),
}
}
pub fn to_i8(&self) -> i8 {
match *self {
Severity::Warning => 0,
Severity::Error => 1,
Severity::Fatal => 2,
Severity::__UNKNOWN__(i) => i,
}
}
}
impl fmt::Display for Severity {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Severity::Warning => write!(f, "warning")?,
Severity::Error => write!(f, "error")?,
Severity::Fatal => write!(f, "fatal error")?,
Severity::__UNKNOWN__(i) => write!(f, "message of unknown severity ({})", i)?,
}
Ok(())
}
}
pub struct ServerError {
code: i32,
position: i32,
severity: Severity,
sqlstate: Vec<u8>,
text: String,
}
const BASE_SIZE: i32 = 4 + 4 + 4 + 1 + 5;
impl ServerError {
pub fn code(&self) -> i32 {
self.code
}
pub fn position(&self) -> i32 {
self.position
}
pub fn severity(&self) -> Severity {
self.severity.clone()
}
pub fn sqlstate(&self) -> Vec<u8> {
self.sqlstate.clone()
}
pub fn text(&self) -> String {
self.text.clone()
}
pub(crate) fn new(
code: i32,
position: i32,
severity: Severity,
sqlstate: Vec<u8>,
text: String,
) -> ServerError {
ServerError {
code,
position,
severity,
sqlstate,
text,
}
}
pub(crate) fn parse(no_of_args: i32, rdr: &mut io::BufRead) -> HdbResult<Vec<ServerError>> {
let mut server_errors = Vec::<ServerError>::new();
for _i in 0..no_of_args {
let code = rdr.read_i32::<LittleEndian>()?;
let position = rdr.read_i32::<LittleEndian>()?;
let text_length = rdr.read_i32::<LittleEndian>()?;
let severity = Severity::from_i8(rdr.read_i8()?);
let sqlstate = util::parse_bytes(5_usize, rdr)?;
let bytes = util::parse_bytes(text_length as usize, rdr)?;
let text = cesu8::from_cesu8(&bytes)?.to_string();
let pad = 8 - (BASE_SIZE + text_length) % 8;
util::skip_bytes(pad as usize, rdr)?;
let server_error = ServerError::new(code, position, severity, sqlstate, text);
debug!("ServerError::parse(): found server error {}", server_error);
server_errors.push(server_error);
}
Ok(server_errors)
}
pub(crate) fn to_string(&self) -> String {
format!(
"{} [code: {}, sql state: {}] at position {}: \"{}\"",
self.severity,
self.code,
String::from_utf8_lossy(&self.sqlstate),
self.position,
self.text
)
}
}
impl Error for ServerError {}
impl fmt::Display for ServerError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(
fmt,
r#"code: {}, sql state: {}, position: {}, msg: "{}""#,
self.code,
String::from_utf8_lossy(&self.sqlstate),
self.position(),
self.text
)
}
}
impl fmt::Debug for ServerError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "{}", self.to_string())
}
}