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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
use std::error::Error;
use std::fmt::{self, Debug, Display, Formatter};
use atoi::atoi;
use smallvec::alloc::borrow::Cow;
use crate::error::DatabaseError;
use crate::postgres::message::{Notice, PgSeverity};
pub struct PgDatabaseError(pub(crate) Notice);
impl PgDatabaseError {
#[inline]
pub fn severity(&self) -> PgSeverity {
self.0.severity()
}
#[inline]
pub fn code(&self) -> &str {
self.0.code()
}
#[inline]
pub fn message(&self) -> &str {
self.0.message()
}
#[inline]
pub fn detail(&self) -> Option<&str> {
self.0.get(b'D')
}
#[inline]
pub fn hint(&self) -> Option<&str> {
self.0.get(b'H')
}
#[inline]
pub fn position(&self) -> Option<PgErrorPosition<'_>> {
self.0
.get_raw(b'P')
.and_then(atoi)
.map(PgErrorPosition::Original)
.or_else(|| {
let position = self.0.get_raw(b'p').and_then(atoi)?;
let query = self.0.get(b'q')?;
Some(PgErrorPosition::Internal { position, query })
})
}
pub fn r#where(&self) -> Option<&str> {
self.0.get(b'W')
}
pub fn schema(&self) -> Option<&str> {
self.0.get(b's')
}
pub fn table(&self) -> Option<&str> {
self.0.get(b't')
}
pub fn column(&self) -> Option<&str> {
self.0.get(b'c')
}
pub fn data_type(&self) -> Option<&str> {
self.0.get(b'd')
}
pub fn constraint(&self) -> Option<&str> {
self.0.get(b'n')
}
pub fn file(&self) -> Option<&str> {
self.0.get(b'F')
}
pub fn line(&self) -> Option<usize> {
self.0.get_raw(b'L').and_then(atoi)
}
pub fn routine(&self) -> Option<&str> {
self.0.get(b'R')
}
}
#[derive(Debug, Eq, PartialEq)]
pub enum PgErrorPosition<'a> {
Original(usize),
Internal {
position: usize,
query: &'a str,
},
}
impl Debug for PgDatabaseError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.debug_struct("PgDatabaseError")
.field("severity", &self.severity())
.field("code", &self.code())
.field("message", &self.message())
.field("detail", &self.detail())
.field("hint", &self.hint())
.field("position", &self.position())
.field("where", &self.r#where())
.field("schema", &self.schema())
.field("table", &self.table())
.field("column", &self.column())
.field("data_type", &self.data_type())
.field("constraint", &self.constraint())
.field("file", &self.file())
.field("line", &self.line())
.field("routine", &self.routine())
.finish()
}
}
impl Display for PgDatabaseError {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
f.write_str(self.message())
}
}
impl Error for PgDatabaseError {}
impl DatabaseError for PgDatabaseError {
fn message(&self) -> &str {
self.message()
}
fn code(&self) -> Option<Cow<'_, str>> {
Some(Cow::Borrowed(self.code()))
}
#[doc(hidden)]
fn as_error(&self) -> &(dyn Error + Send + Sync + 'static) {
self
}
#[doc(hidden)]
fn as_error_mut(&mut self) -> &mut (dyn Error + Send + Sync + 'static) {
self
}
#[doc(hidden)]
fn into_error(self: Box<Self>) -> Box<dyn Error + Send + Sync + 'static> {
self
}
fn is_transient_in_connect_phase(&self) -> bool {
[
"53300",
"57P03",
]
.contains(&self.code())
}
fn constraint(&self) -> Option<&str> {
self.constraint()
}
}