Skip to content

Commit 94d0411

Browse files
committed
Make DbError constructors actually private
1 parent cae1221 commit 94d0411

3 files changed

Lines changed: 197 additions & 194 deletions

File tree

src/error.rs

Lines changed: 2 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use std::collections::HashMap;
1+
pub use ugh_privacy::DbError;
2+
23
use std::error;
34
use std::old_io::IoError;
45
use std::fmt;
5-
use std::result;
66

77
use openssl::ssl::error::SslError;
88
use phf;
@@ -466,183 +466,6 @@ pub enum ErrorPosition {
466466
}
467467
}
468468

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-
646469
/// An error encountered when communicating with the Postgres server
647470
#[derive(Clone, PartialEq, Eq, Debug)]
648471
pub enum Error {

src/lib.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ impl InnerConnection {
458458
conn.cancel_data.secret_key = secret_key;
459459
}
460460
ReadyForQuery { .. } => break,
461-
ErrorResponse { fields } => return DbError::new_connect(fields),
461+
ErrorResponse { fields } => return ugh_privacy::dberror_new_connect(fields),
462462
_ => return Err(ConnectError::BadResponse),
463463
}
464464
}
@@ -505,7 +505,7 @@ impl InnerConnection {
505505
debug_assert!(!self.desynchronized);
506506
match try_desync!(self, self.stream.read_message()) {
507507
NoticeResponse { fields } => {
508-
if let Ok(err) = DbError::new_raw(fields) {
508+
if let Ok(err) = ugh_privacy::dberror_new_raw(fields) {
509509
self.notice_handler.handle(err);
510510
}
511511
Ok(None)
@@ -567,13 +567,13 @@ impl InnerConnection {
567567
| AuthenticationSCMCredential
568568
| AuthenticationGSS
569569
| AuthenticationSSPI => return Err(ConnectError::UnsupportedAuthentication),
570-
ErrorResponse { fields } => return DbError::new_connect(fields),
570+
ErrorResponse { fields } => return ugh_privacy::dberror_new_connect(fields),
571571
_ => return Err(ConnectError::BadResponse)
572572
}
573573

574574
match try!(self.read_message()) {
575575
AuthenticationOk => Ok(()),
576-
ErrorResponse { fields } => return DbError::new_connect(fields),
576+
ErrorResponse { fields } => return ugh_privacy::dberror_new_connect(fields),
577577
_ => return Err(ConnectError::BadResponse)
578578
}
579579
}
@@ -600,7 +600,7 @@ impl InnerConnection {
600600
ParseComplete => {}
601601
ErrorResponse { fields } => {
602602
try!(self.wait_for_ready());
603-
return DbError::new(fields);
603+
return ugh_privacy::dberror_new(fields);
604604
}
605605
_ => bad_response!(self),
606606
}
@@ -716,7 +716,7 @@ impl InnerConnection {
716716
Sync]));
717717
let resp = match try!(self.read_message()) {
718718
CloseComplete => Ok(()),
719-
ErrorResponse { fields } => DbError::new(fields),
719+
ErrorResponse { fields } => ugh_privacy::dberror_new(fields),
720720
_ => bad_response!(self)
721721
};
722722
try!(self.wait_for_ready());
@@ -750,7 +750,7 @@ impl InnerConnection {
750750
BindComplete => {}
751751
ErrorResponse { fields } => {
752752
try!(self.wait_for_ready());
753-
return DbError::new(fields);
753+
return ugh_privacy::dberror_new(fields);
754754
}
755755
_ => bad_response!(self)
756756
}
@@ -763,15 +763,15 @@ impl InnerConnection {
763763
}
764764
ErrorResponse { fields } => {
765765
try!(self.wait_for_ready());
766-
return DbError::new(fields);
766+
return ugh_privacy::dberror_new(fields);
767767
}
768768
_ => bad_response!(self)
769769
};
770770
match try!(self.read_message()) {
771771
CommandComplete { .. } => {}
772772
ErrorResponse { fields } => {
773773
try!(self.wait_for_ready());
774-
return DbError::new(fields);
774+
return ugh_privacy::dberror_new(fields);
775775
}
776776
_ => bad_response!(self)
777777
}
@@ -828,7 +828,7 @@ impl InnerConnection {
828828
}
829829
ErrorResponse { fields } => {
830830
try!(self.wait_for_ready());
831-
return DbError::new(fields);
831+
return ugh_privacy::dberror_new(fields);
832832
}
833833
_ => {}
834834
}
@@ -1346,7 +1346,7 @@ impl<'conn> Statement<'conn> {
13461346
BindComplete => Ok(()),
13471347
ErrorResponse { fields } => {
13481348
try!(conn.wait_for_ready());
1349-
DbError::new(fields)
1349+
ugh_privacy::dberror_new(fields)
13501350
}
13511351
_ => {
13521352
conn.desynchronized = true;
@@ -1411,7 +1411,7 @@ impl<'conn> Statement<'conn> {
14111411
DataRow { .. } => {}
14121412
ErrorResponse { fields } => {
14131413
try!(conn.wait_for_ready());
1414-
return DbError::new(fields);
1414+
return ugh_privacy::dberror_new(fields);
14151415
}
14161416
CommandComplete { tag } => {
14171417
num = util::parse_update_count(tag);
@@ -1579,7 +1579,7 @@ impl<'stmt> Rows<'stmt> {
15791579
DataRow { row } => self.data.push_back(row),
15801580
ErrorResponse { fields } => {
15811581
try!(conn.wait_for_ready());
1582-
return DbError::new(fields);
1582+
return ugh_privacy::dberror_new(fields);
15831583
}
15841584
CopyInResponse { .. } => {
15851585
try!(conn.write_messages(&[
@@ -1901,7 +1901,7 @@ impl<'a> CopyInStatement<'a> {
19011901
BindComplete => {},
19021902
ErrorResponse { fields } => {
19031903
try!(conn.wait_for_ready());
1904-
return DbError::new(fields);
1904+
return ugh_privacy::dberror_new(fields);
19051905
}
19061906
_ => {
19071907
conn.desynchronized = true;
@@ -1977,7 +1977,7 @@ impl<'a> CopyInStatement<'a> {
19771977
CommandComplete { tag } => util::parse_update_count(tag),
19781978
ErrorResponse { fields } => {
19791979
try!(conn.wait_for_ready());
1980-
return DbError::new(fields);
1980+
return ugh_privacy::dberror_new(fields);
19811981
}
19821982
_ => {
19831983
conn.desynchronized = true;

0 commit comments

Comments
 (0)