| 429 | } |
| 430 | |
| 431 | from_impl!(WrongType); |
| 432 | |
| 433 | /// A Postgres error or notice. |
| 434 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 435 | pub struct DbError { |
| 436 | severity: String, |
| 437 | parsed_severity: Option<Severity>, |
| 438 | code: SqlState, |
| 439 | message: String, |
| 440 | detail: Option<String>, |
| 441 | hint: Option<String>, |
| 442 | position: Option<ErrorPosition>, |
| 443 | where_: Option<String>, |
| 444 | schema: Option<String>, |
| 445 | table: Option<String>, |
| 446 | column: Option<String>, |
| 447 | datatype: Option<String>, |
| 448 | constraint: Option<String>, |
| 449 | file: Option<String>, |
| 450 | line: Option<u32>, |
| 451 | routine: Option<String>, |
| 452 | } |
| 453 | |
| 454 | impl DbError { |
| 455 | fn parse(fields: &mut ErrorFields<'_>) -> io::Result<DbError> { |
| 456 | let mut severity = None; |
| 457 | let mut parsed_severity = None; |
| 458 | let mut code = None; |
| 459 | let mut message = None; |
| 460 | let mut detail = None; |
| 461 | let mut hint = None; |
| 462 | let mut normal_position = None; |
| 463 | let mut internal_position = None; |
| 464 | let mut internal_query = None; |
| 465 | let mut where_ = None; |
| 466 | let mut schema = None; |
| 467 | let mut table = None; |
| 468 | let mut column = None; |
| 469 | let mut datatype = None; |
| 470 | let mut constraint = None; |
| 471 | let mut file = None; |
| 472 | let mut line = None; |
| 473 | let mut routine = None; |
| 474 | |
| 475 | while let Some(field) = fields.next()? { |
| 476 | let value = String::from_utf8_lossy(field.value_bytes()); |
| 477 | match field.type_() { |
| 478 | b'S' => severity = Some(value.into_owned()), |
| 479 | b'C' => code = Some(SqlState::from_code(&value)), |
| 480 | b'M' => message = Some(value.into_owned()), |
| 481 | b'D' => detail = Some(value.into_owned()), |
| 482 | b'H' => hint = Some(value.into_owned()), |
| 483 | b'P' => { |
| 484 | normal_position = Some(value.parse::<u32>().map_err(|_| { |
| 485 | io::Error::new(io::ErrorKind::InvalidInput, "`P` field did not contain an integer") |
| 486 | })?); |
| 487 | } |
| 488 | b'p' => { |