|
1 | | -use std::collections::HashMap; |
| 1 | +pub use ugh_privacy::DbError; |
| 2 | + |
2 | 3 | use std::error; |
3 | 4 | use std::old_io::IoError; |
4 | 5 | use std::fmt; |
5 | | -use std::result; |
6 | 6 |
|
7 | 7 | use openssl::ssl::error::SslError; |
8 | 8 | use phf; |
@@ -466,183 +466,6 @@ pub enum ErrorPosition { |
466 | 466 | } |
467 | 467 | } |
468 | 468 |
|
469 | | -/// A Postgres error or notice. |
470 | | -#[derive(Clone, PartialEq, Eq, Debug)] |
471 | | -pub struct DbError { |
472 | | - severity: String, |
473 | | - code: SqlState, |
474 | | - message: String, |
475 | | - detail: Option<String>, |
476 | | - hint: Option<String>, |
477 | | - position: Option<ErrorPosition>, |
478 | | - where_: Option<String>, |
479 | | - schema: Option<String>, |
480 | | - table: Option<String>, |
481 | | - column: Option<String>, |
482 | | - datatype: Option<String>, |
483 | | - constraint: Option<String>, |
484 | | - file: String, |
485 | | - line: u32, |
486 | | - routine: String |
487 | | -} |
488 | | - |
489 | | -impl DbError { |
490 | | - #[doc(hidden)] |
491 | | - pub fn new_raw(fields: Vec<(u8, String)>) -> result::Result<DbError, ()> { |
492 | | - let mut map: HashMap<_, _> = fields.into_iter().collect(); |
493 | | - Ok(DbError { |
494 | | - severity: try!(map.remove(&b'S').ok_or(())), |
495 | | - code: SqlState::from_code(try!(map.remove(&b'C').ok_or(()))), |
496 | | - message: try!(map.remove(&b'M').ok_or(())), |
497 | | - detail: map.remove(&b'D'), |
498 | | - hint: map.remove(&b'H'), |
499 | | - position: match map.remove(&b'P') { |
500 | | - Some(pos) => Some(ErrorPosition::Normal(try!(pos.parse().map_err(|_| ())))), |
501 | | - None => match map.remove(&b'p') { |
502 | | - Some(pos) => Some(ErrorPosition::Internal { |
503 | | - position: try!(pos.parse().map_err(|_| ())), |
504 | | - query: try!(map.remove(&b'q').ok_or(())) |
505 | | - }), |
506 | | - None => None |
507 | | - } |
508 | | - }, |
509 | | - where_: map.remove(&b'W'), |
510 | | - schema: map.remove(&b's'), |
511 | | - table: map.remove(&b't'), |
512 | | - column: map.remove(&b'c'), |
513 | | - datatype: map.remove(&b'd'), |
514 | | - constraint: map.remove(&b'n'), |
515 | | - file: try!(map.remove(&b'F').ok_or(())), |
516 | | - line: try!(map.remove(&b'L').and_then(|l| l.parse().ok()).ok_or(())), |
517 | | - routine: try!(map.remove(&b'R').ok_or(())), |
518 | | - }) |
519 | | - } |
520 | | - |
521 | | - #[doc(hidden)] |
522 | | - pub fn new_connect<T>(fields: Vec<(u8, String)>) -> result::Result<T, ConnectError> { |
523 | | - match DbError::new_raw(fields) { |
524 | | - Ok(err) => Err(ConnectError::DbError(err)), |
525 | | - Err(()) => Err(ConnectError::BadResponse), |
526 | | - } |
527 | | - } |
528 | | - |
529 | | - #[doc(hidden)] |
530 | | - pub fn new<T>(fields: Vec<(u8, String)>) -> Result<T> { |
531 | | - match DbError::new_raw(fields) { |
532 | | - Ok(err) => Err(Error::DbError(err)), |
533 | | - Err(()) => Err(Error::BadData), |
534 | | - } |
535 | | - } |
536 | | - |
537 | | - /// The field contents are ERROR, FATAL, or PANIC (in an error message), |
538 | | - /// or WARNING, NOTICE, DEBUG, INFO, or LOG (in a notice message), or a |
539 | | - /// localized translation of one of these. |
540 | | - pub fn severity(&self) -> &str { |
541 | | - &self.severity |
542 | | - } |
543 | | - |
544 | | - /// The SQLSTATE code for the error. |
545 | | - pub fn code(&self) -> &SqlState { |
546 | | - &self.code |
547 | | - } |
548 | | - |
549 | | - /// The primary human-readable error message. This should be accurate but |
550 | | - /// terse (typically one line). |
551 | | - pub fn message(&self) -> &str { |
552 | | - &self.message |
553 | | - } |
554 | | - |
555 | | - /// An optional secondary error message carrying more detail about the |
556 | | - /// problem. Might run to multiple lines. |
557 | | - pub fn detail(&self) -> Option<&str> { |
558 | | - self.detail.as_ref().map(|s| &**s) |
559 | | - } |
560 | | - |
561 | | - /// An optional suggestion what to do about the problem. This is intended |
562 | | - /// to differ from Detail in that it offers advice (potentially |
563 | | - /// inappropriate) rather than hard facts. Might run to multiple lines. |
564 | | - pub fn hint(&self) -> Option<&str> { |
565 | | - self.hint.as_ref().map(|s| &**s) |
566 | | - } |
567 | | - |
568 | | - /// An optional error cursor position into either the original query string |
569 | | - /// or an internally generated query. |
570 | | - pub fn position(&self) -> Option<&ErrorPosition> { |
571 | | - self.position.as_ref() |
572 | | - } |
573 | | - |
574 | | - /// An indication of the context in which the error occurred. Presently |
575 | | - /// this includes a call stack traceback of active procedural language |
576 | | - /// functions and internally-generated queries. The trace is one entry per |
577 | | - /// line, most recent first. |
578 | | - pub fn where_(&self) -> Option<&str> { |
579 | | - self.where_.as_ref().map(|s| &**s) |
580 | | - } |
581 | | - |
582 | | - /// If the error was associated with a specific database object, the name |
583 | | - /// of the schema containing that object, if any. (PostgreSQL 9.3+) |
584 | | - pub fn schema(&self) -> Option<&str> { |
585 | | - self.schema.as_ref().map(|s| &**s) |
586 | | - } |
587 | | - |
588 | | - /// If the error was associated with a specific table, the name of the |
589 | | - /// table. (Refer to the schema name field for the name of the table's |
590 | | - /// schema.) (PostgreSQL 9.3+) |
591 | | - pub fn table(&self) -> Option<&str> { |
592 | | - self.table.as_ref().map(|s| &**s) |
593 | | - } |
594 | | - |
595 | | - /// If the error was associated with a specific table column, the name of |
596 | | - /// the column. (Refer to the schema and table name fields to identify the |
597 | | - /// table.) (PostgreSQL 9.3+) |
598 | | - pub fn column(&self) -> Option<&str> { |
599 | | - self.column.as_ref().map(|s| &**s) |
600 | | - } |
601 | | - |
602 | | - /// If the error was associated with a specific data type, the name of the |
603 | | - /// data type. (Refer to the schema name field for the name of the data |
604 | | - /// type's schema.) (PostgreSQL 9.3+) |
605 | | - pub fn datatype(&self) -> Option<&str> { |
606 | | - self.datatype.as_ref().map(|s| &**s) |
607 | | - } |
608 | | - |
609 | | - /// If the error was associated with a specific constraint, the name of the |
610 | | - /// constraint. Refer to fields listed above for the associated table or |
611 | | - /// domain. (For this purpose, indexes are treated as constraints, even if |
612 | | - /// they weren't created with constraint syntax.) (PostgreSQL 9.3+) |
613 | | - pub fn constraint(&self) -> Option<&str> { |
614 | | - self.constraint.as_ref().map(|s| &**s) |
615 | | - } |
616 | | - |
617 | | - /// The file name of the source-code location where the error was reported. |
618 | | - pub fn file(&self) -> &str { |
619 | | - &self.file |
620 | | - } |
621 | | - |
622 | | - /// The line number of the source-code location where the error was |
623 | | - /// reported. |
624 | | - pub fn line(&self) -> u32 { |
625 | | - self.line |
626 | | - } |
627 | | - |
628 | | - /// The name of the source-code routine reporting the error. |
629 | | - pub fn routine(&self) -> &str { |
630 | | - &self.routine |
631 | | - } |
632 | | -} |
633 | | - |
634 | | -impl fmt::Display for DbError { |
635 | | - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
636 | | - write!(fmt, "{}: {}", self.severity, self.message) |
637 | | - } |
638 | | -} |
639 | | - |
640 | | -impl error::Error for DbError { |
641 | | - fn description(&self) -> &str { |
642 | | - &*self.message |
643 | | - } |
644 | | -} |
645 | | - |
646 | 469 | /// An error encountered when communicating with the Postgres server |
647 | 470 | #[derive(Clone, PartialEq, Eq, Debug)] |
648 | 471 | pub enum Error { |
|
0 commit comments