@@ -13,6 +13,59 @@ use {Result, DbErrorNew};
13
13
14
14
mod sqlstate;
15
15
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
+
16
69
/// A Postgres error or notice.
17
70
#[ derive( Clone , PartialEq , Eq ) ]
18
71
pub struct DbError {
@@ -21,6 +74,9 @@ pub struct DbError {
21
74
/// localized translation of one of these.
22
75
pub severity : String ,
23
76
77
+ /// A parsed, nonlocalized version of `severity`. (PostgreSQL 9.6+)
78
+ pub parsed_severity : Option < Severity > ,
79
+
24
80
/// The SQLSTATE code for the error.
25
81
pub code : SqlState ,
26
82
@@ -88,6 +144,7 @@ pub struct DbError {
88
144
impl DbErrorNew for DbError {
89
145
fn new_raw ( fields : & mut ErrorFields ) -> io:: Result < DbError > {
90
146
let mut severity = None ;
147
+ let mut parsed_severity = None ;
91
148
let mut code = None ;
92
149
let mut message = None ;
93
150
let mut detail = None ;
@@ -124,12 +181,14 @@ impl DbErrorNew for DbError {
124
181
b'F' => file = Some ( field. value ( ) . to_owned ( ) ) ,
125
182
b'L' => line = Some ( try!( field. value ( ) . parse :: < u32 > ( ) . map_err ( |_| :: bad_response ( ) ) ) ) ,
126
183
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) ) ) ,
127
185
_ => { } ,
128
186
}
129
187
}
130
188
131
189
Ok ( DbError {
132
190
severity : try!( severity. ok_or_else ( || :: bad_response ( ) ) ) ,
191
+ parsed_severity : parsed_severity,
133
192
code : try!( code. ok_or_else ( || :: bad_response ( ) ) ) ,
134
193
message : try!( message. ok_or_else ( || :: bad_response ( ) ) ) ,
135
194
detail : detail,
0 commit comments