|
1 | 1 | //! Errors. |
2 | 2 |
|
3 | 3 | use fallible_iterator::FallibleIterator; |
4 | | -use postgres_protocol::message::backend::ErrorFields; |
5 | | -use std::error; |
| 4 | +use postgres_protocol::message::backend::{ErrorFields, ErrorResponseBody}; |
6 | 5 | use std::convert::From; |
| 6 | +use std::error; |
7 | 7 | use std::fmt; |
8 | 8 | use std::io; |
9 | 9 |
|
@@ -214,36 +214,29 @@ impl DbError { |
214 | 214 | } |
215 | 215 |
|
216 | 216 | Ok(DbError { |
217 | | - severity: severity.ok_or_else(|| { |
218 | | - io::Error::new(io::ErrorKind::InvalidInput, "`S` field missing") |
219 | | - })?, |
| 217 | + severity: severity |
| 218 | + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "`S` field missing"))?, |
220 | 219 | parsed_severity: parsed_severity, |
221 | | - code: code.ok_or_else(|| { |
222 | | - io::Error::new(io::ErrorKind::InvalidInput, "`C` field missing") |
223 | | - })?, |
224 | | - message: message.ok_or_else(|| { |
225 | | - io::Error::new(io::ErrorKind::InvalidInput, "`M` field missing") |
226 | | - })?, |
| 220 | + code: code |
| 221 | + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "`C` field missing"))?, |
| 222 | + message: message |
| 223 | + .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "`M` field missing"))?, |
227 | 224 | detail: detail, |
228 | 225 | hint: hint, |
229 | 226 | position: match normal_position { |
230 | 227 | Some(position) => Some(ErrorPosition::Normal(position)), |
231 | | - None => { |
232 | | - match internal_position { |
233 | | - Some(position) => { |
234 | | - Some(ErrorPosition::Internal { |
235 | | - position: position, |
236 | | - query: internal_query.ok_or_else(|| { |
237 | | - io::Error::new( |
238 | | - io::ErrorKind::InvalidInput, |
239 | | - "`q` field missing but `p` field present", |
240 | | - ) |
241 | | - })?, |
242 | | - }) |
243 | | - } |
244 | | - None => None, |
245 | | - } |
246 | | - } |
| 228 | + None => match internal_position { |
| 229 | + Some(position) => Some(ErrorPosition::Internal { |
| 230 | + position: position, |
| 231 | + query: internal_query.ok_or_else(|| { |
| 232 | + io::Error::new( |
| 233 | + io::ErrorKind::InvalidInput, |
| 234 | + "`q` field missing but `p` field present", |
| 235 | + ) |
| 236 | + })?, |
| 237 | + }), |
| 238 | + None => None, |
| 239 | + }, |
247 | 240 | }, |
248 | 241 | where_: where_, |
249 | 242 | schema: schema, |
@@ -324,6 +317,14 @@ pub fn db(e: DbError) -> Error { |
324 | 317 | Error(Box::new(ErrorKind::Db(e))) |
325 | 318 | } |
326 | 319 |
|
| 320 | +#[doc(hidden)] |
| 321 | +pub fn __db(e: ErrorResponseBody) -> Error { |
| 322 | + match DbError::new(&mut e.fields()) { |
| 323 | + Ok(e) => Error(Box::new(ErrorKind::Db(e))), |
| 324 | + Err(e) => Error(Box::new(ErrorKind::Io(e))), |
| 325 | + } |
| 326 | +} |
| 327 | + |
327 | 328 | #[doc(hidden)] |
328 | 329 | pub fn io(e: io::Error) -> Error { |
329 | 330 | Error(Box::new(ErrorKind::Io(e))) |
@@ -401,7 +402,7 @@ impl Error { |
401 | 402 | pub fn as_db(&self) -> Option<&DbError> { |
402 | 403 | match *self.0 { |
403 | 404 | ErrorKind::Db(ref err) => Some(err), |
404 | | - _ => None |
| 405 | + _ => None, |
405 | 406 | } |
406 | 407 | } |
407 | 408 |
|
|
0 commit comments