Skip to content

Commit a1c5ab5

Browse files
committed
Support the new V error field.
1 parent 9d53e67 commit a1c5ab5

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/error/mod.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,59 @@ use {Result, DbErrorNew};
1313

1414
mod sqlstate;
1515

16+
/// The severity of a Postgres error or notice.
17+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
18+
pub enum Severity {
19+
/// PANIC
20+
Panic,
21+
/// FATAL
22+
Fatal,
23+
/// ERROR
24+
Error,
25+
/// WARNING
26+
Warning,
27+
/// NOTICE
28+
Notice,
29+
/// DEBUG
30+
Debug,
31+
/// INFO
32+
Info,
33+
/// LOG
34+
Log,
35+
}
36+
37+
impl fmt::Display for Severity {
38+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
39+
let s = match *self {
40+
Severity::Panic => "PANIC",
41+
Severity::Fatal => "FATAL",
42+
Severity::Error => "ERROR",
43+
Severity::Warning => "WARNING",
44+
Severity::Notice => "NOTICE",
45+
Severity::Debug => "DEBUG",
46+
Severity::Info => "INFO",
47+
Severity::Log => "LOG",
48+
};
49+
fmt.write_str(s)
50+
}
51+
}
52+
53+
impl Severity {
54+
fn from_str(s: &str) -> Option<Severity> {
55+
match s {
56+
"PANIC" => Some(Severity::Panic),
57+
"FATAL" => Some(Severity::Fatal),
58+
"ERROR" => Some(Severity::Error),
59+
"WARNING" => Some(Severity::Warning),
60+
"NOTICE" => Some(Severity::Notice),
61+
"DEBUG" => Some(Severity::Debug),
62+
"INFO" => Some(Severity::Info),
63+
"LOG" => Some(Severity::Log),
64+
_ => None,
65+
}
66+
}
67+
}
68+
1669
/// A Postgres error or notice.
1770
#[derive(Clone, PartialEq, Eq)]
1871
pub struct DbError {
@@ -21,6 +74,9 @@ pub struct DbError {
2174
/// localized translation of one of these.
2275
pub severity: String,
2376

77+
/// A parsed, nonlocalized version of `severity`. (PostgreSQL 9.6+)
78+
pub parsed_severity: Option<Severity>,
79+
2480
/// The SQLSTATE code for the error.
2581
pub code: SqlState,
2682

@@ -88,6 +144,7 @@ pub struct DbError {
88144
impl DbErrorNew for DbError {
89145
fn new_raw(fields: &mut ErrorFields) -> io::Result<DbError> {
90146
let mut severity = None;
147+
let mut parsed_severity = None;
91148
let mut code = None;
92149
let mut message = None;
93150
let mut detail = None;
@@ -124,12 +181,14 @@ impl DbErrorNew for DbError {
124181
b'F' => file = Some(field.value().to_owned()),
125182
b'L' => line = Some(try!(field.value().parse::<u32>().map_err(|_| ::bad_response()))),
126183
b'R' => routine = Some(field.value().to_owned()),
184+
b'V' => parsed_severity = Some(try!(Severity::from_str(field.value()).ok_or_else(::bad_response))),
127185
_ => {},
128186
}
129187
}
130188

131189
Ok(DbError {
132190
severity: try!(severity.ok_or_else(|| ::bad_response())),
191+
parsed_severity: parsed_severity,
133192
code: try!(code.ok_or_else(|| ::bad_response())),
134193
message: try!(message.ok_or_else(|| ::bad_response())),
135194
detail: detail,

0 commit comments

Comments
 (0)