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 protocol::util;
use HdbResult;

use byteorder::{LittleEndian, ReadBytesExt};
use cesu8;
use std::fmt;
use std::io;

/// Severity of a server message
#[derive(Clone, Debug)]
pub enum Severity {
    /// An additional warning is sent from the server to the client,
    /// along with the regular response.
    Warning,
    /// The request sent to the server was not correct or could not be answered
    /// correctly.
    Error,
    /// A fatal, session-terminating error occured.
    Fatal,

    /// The request sent to the server could not be answered, for an unknown
    /// reason.
    __UNKNOWN__(i8),
}
impl Severity {
    #[doc(hidden)]
    pub fn from_i8(i: i8) -> Severity {
        match i {
            0 => Severity::Warning,
            1 => Severity::Error,
            2 => Severity::Fatal,
            i => Severity::__UNKNOWN__(i),
        }
    }
    /// Returns the number encoding of the severity.
    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(())
    }
}

/// Describes an error that is reported from the database.
pub struct ServerError {
    code: i32,
    position: i32,
    severity: Severity,
    sqlstate: Vec<u8>,
    text: String,
}
const BASE_SIZE: usize = 4 + 4 + 4 + 1 + 5;

impl ServerError {
    /// Returns the error code.
    pub fn code(&self) -> i32 {
        self.code
    }
    /// Returns the position in the line where the error occured.
    pub fn position(&self) -> i32 {
        self.position
    }
    /// Returns the Severity of the error.
    pub fn severity(&self) -> Severity {
        self.severity.clone()
    }
    /// Returns the SQL state of the error.
    pub fn sqlstate(&self) -> Vec<u8> {
        self.sqlstate.clone()
    }
    /// Returns the description of the error.
    pub fn text(&self) -> String {
        self.text.clone()
    }
}

#[doc(hidden)]
impl ServerError {
    pub fn new(
        code: i32,
        position: i32,
        severity: Severity,
        sqlstate: Vec<u8>,
        text: String,
    ) -> ServerError {
        ServerError {
            code,
            position,
            severity,
            sqlstate,
            text,
        }
    }

    pub fn size(&self) -> usize {
        BASE_SIZE + self.text.len()
    }

    pub fn parse(arg_size: i32, rdr: &mut io::BufRead) -> HdbResult<ServerError> {
        let code = rdr.read_i32::<LittleEndian>()?; // I4
        let position = rdr.read_i32::<LittleEndian>()?; // I4
        let text_length = rdr.read_i32::<LittleEndian>()?; // I4
        let severity = Severity::from_i8(rdr.read_i8()?); // I1
        let sqlstate = util::parse_bytes(5_usize, rdr)?; // B5
        let bytes = util::parse_bytes(text_length as usize, rdr)?; // B[text_length]
        let text = cesu8::from_cesu8(&bytes)?;
        let pad = arg_size - BASE_SIZE as i32 - text_length;
        util::skip_bytes(pad as usize, rdr)?;

        let hdberr = ServerError::new(code, position, severity, sqlstate, text.to_string());
        debug!("parse(): found hdberr with {}", hdberr.to_string());
        Ok(hdberr)
    }

    pub 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 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())
    }
}