Skip to content

Commit a64ca5f

Browse files
committed
Remove failure from PostgresDbError::new
Closes rust-postgres#43
1 parent 5561e7b commit a64ca5f

2 files changed

Lines changed: 54 additions & 37 deletions

File tree

src/error.rs

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
//! Postgres errors
22
33
use std::collections::HashMap;
4-
use std::from_str::FromStr;
54
use std::io;
65
use std::fmt;
76

87
use openssl::ssl::error;
98
use phf::PhfMap;
109

10+
use PostgresResult;
1111
use types::PostgresType;
1212

1313
macro_rules! make_errors(
@@ -480,33 +480,49 @@ pub struct PostgresDbError {
480480

481481
impl PostgresDbError {
482482
#[doc(hidden)]
483-
pub fn new(fields: Vec<(u8, String)>) -> PostgresDbError {
483+
pub fn new_raw(fields: Vec<(u8, String)>) -> Result<PostgresDbError, ()> {
484484
let mut map: HashMap<_, _> = fields.into_iter().collect();
485-
PostgresDbError {
486-
severity: map.pop(&('S' as u8)).unwrap(),
487-
code: PostgresSqlState::from_code(map.pop(&('C' as u8)).unwrap().as_slice()),
488-
message: map.pop(&('M' as u8)).unwrap(),
489-
detail: map.pop(&('D' as u8)),
490-
hint: map.pop(&('H' as u8)),
491-
position: match map.pop(&('P' as u8)) {
492-
Some(pos) => Some(Position(FromStr::from_str(pos.as_slice()).unwrap())),
493-
None => match map.pop(&('p' as u8)) {
485+
Ok(PostgresDbError {
486+
severity: try!(map.pop(&b'S').ok_or(())),
487+
code: PostgresSqlState::from_code(try!(map.pop(&b'C').ok_or(())).as_slice()),
488+
message: try!(map.pop(&b'M').ok_or(())),
489+
detail: map.pop(&b'D'),
490+
hint: map.pop(&b'H'),
491+
position: match map.pop(&b'P') {
492+
Some(pos) => Some(Position(try!(from_str(pos.as_slice()).ok_or(())))),
493+
None => match map.pop(&b'p') {
494494
Some(pos) => Some(InternalPosition {
495-
position: FromStr::from_str(pos.as_slice()).unwrap(),
496-
query: map.pop(&('q' as u8)).unwrap()
495+
position: try!(from_str(pos.as_slice()).ok_or(())),
496+
query: try!(map.pop(&b'q').ok_or(()))
497497
}),
498498
None => None
499499
}
500500
},
501-
where_: map.pop(&('W' as u8)),
502-
schema: map.pop(&('s' as u8)),
503-
table: map.pop(&('t' as u8)),
504-
column: map.pop(&('c' as u8)),
505-
datatype: map.pop(&('d' as u8)),
506-
constraint: map.pop(&('n' as u8)),
507-
file: map.pop(&('F' as u8)).unwrap(),
508-
line: FromStr::from_str(map.pop(&('L' as u8)).unwrap().as_slice()).unwrap(),
509-
routine: map.pop(&('R' as u8)).unwrap()
501+
where_: map.pop(&b'W'),
502+
schema: map.pop(&b's'),
503+
table: map.pop(&b't'),
504+
column: map.pop(&b'c'),
505+
datatype: map.pop(&b'd'),
506+
constraint: map.pop(&b'n'),
507+
file: try!(map.pop(&b'F').ok_or(())),
508+
line: try!(map.pop(&b'L').and_then(|l| from_str(l.as_slice())).ok_or(())),
509+
routine: map.pop(&b'R').unwrap()
510+
})
511+
}
512+
513+
#[doc(hidden)]
514+
pub fn new_connect<T>(fields: Vec<(u8, String)>) -> Result<T, PostgresConnectError> {
515+
match PostgresDbError::new_raw(fields) {
516+
Ok(err) => Err(PgConnectDbError(err)),
517+
Err(()) => Err(PgConnectBadResponse),
518+
}
519+
}
520+
521+
#[doc(hidden)]
522+
pub fn new<T>(fields: Vec<(u8, String)>) -> PostgresResult<T> {
523+
match PostgresDbError::new_raw(fields) {
524+
Ok(err) => Err(PgDbError(err)),
525+
Err(()) => Err(PgBadData),
510526
}
511527
}
512528
}

src/lib.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,8 @@ use std::fmt;
8080
use error::{InvalidUrl,
8181
MissingPassword,
8282
MissingUser,
83-
PgConnectDbError,
8483
PgConnectStreamError,
8584
PgConnectBadResponse,
86-
PgDbError,
8785
PgInvalidColumn,
8886
PgStreamDesynchronized,
8987
PgStreamError,
@@ -428,7 +426,7 @@ impl InnerPostgresConnection {
428426
}
429427
ReadyForQuery { .. } => break,
430428
ErrorResponse { fields } =>
431-
return Err(PgConnectDbError(PostgresDbError::new(fields))),
429+
return PostgresDbError::new_connect(fields),
432430
_ => return Err(PgConnectBadResponse),
433431
}
434432
}
@@ -449,7 +447,10 @@ impl InnerPostgresConnection {
449447
loop {
450448
match try_desync!(self, self.stream.read_message()) {
451449
NoticeResponse { fields } => {
452-
self.notice_handler.handle(PostgresDbError::new(fields))
450+
match PostgresDbError::new_raw(fields) {
451+
Ok(err) => self.notice_handler.handle(err),
452+
Err(()) => {}
453+
}
453454
}
454455
NotificationResponse { pid, channel, payload } => {
455456
self.notifications.push(PostgresNotification {
@@ -500,7 +501,7 @@ impl InnerPostgresConnection {
500501
| AuthenticationSCMCredential
501502
| AuthenticationGSS
502503
| AuthenticationSSPI => return Err(UnsupportedAuthentication),
503-
ErrorResponse { fields } => return Err(PgConnectDbError(PostgresDbError::new(fields))),
504+
ErrorResponse { fields } => return PostgresDbError::new_connect(fields),
504505
_ => {
505506
self.desynchronized = true;
506507
return Err(PgConnectBadResponse);
@@ -509,7 +510,7 @@ impl InnerPostgresConnection {
509510

510511
match try_pg_conn!(self.read_message_()) {
511512
AuthenticationOk => Ok(()),
512-
ErrorResponse { fields } => Err(PgConnectDbError(PostgresDbError::new(fields))),
513+
ErrorResponse { fields } => return PostgresDbError::new_connect(fields),
513514
_ => {
514515
self.desynchronized = true;
515516
return Err(PgConnectBadResponse);
@@ -543,7 +544,7 @@ impl InnerPostgresConnection {
543544
ParseComplete => {}
544545
ErrorResponse { fields } => {
545546
try!(self.wait_for_ready());
546-
return Err(PgDbError(PostgresDbError::new(fields)));
547+
return PostgresDbError::new(fields);
547548
}
548549
_ => bad_response!(self),
549550
}
@@ -629,7 +630,7 @@ impl InnerPostgresConnection {
629630
ReadyForQuery { .. } => break,
630631
ErrorResponse { fields } => {
631632
try!(self.wait_for_ready());
632-
return Err(PgDbError(PostgresDbError::new(fields)));
633+
return PostgresDbError::new(fields);
633634
}
634635
_ => {}
635636
}
@@ -691,7 +692,7 @@ impl InnerPostgresConnection {
691692
}
692693
ErrorResponse { fields } => {
693694
try!(self.wait_for_ready());
694-
return Err(PgDbError(PostgresDbError::new(fields)));
695+
return PostgresDbError::new(fields);
695696
}
696697
_ => {}
697698
}
@@ -1184,7 +1185,7 @@ impl<'conn> PostgresStatement<'conn> {
11841185
BindComplete => Ok(()),
11851186
ErrorResponse { fields } => {
11861187
try!(conn.wait_for_ready());
1187-
Err(PgDbError(PostgresDbError::new(fields)))
1188+
PostgresDbError::new(fields)
11881189
}
11891190
_ => {
11901191
conn.desynchronized = true;
@@ -1251,7 +1252,7 @@ impl<'conn> PostgresStatement<'conn> {
12511252
DataRow { .. } => {}
12521253
ErrorResponse { fields } => {
12531254
try!(conn.wait_for_ready());
1254-
return Err(PgDbError(PostgresDbError::new(fields)));
1255+
return PostgresDbError::new(fields);
12551256
}
12561257
CommandComplete { tag } => {
12571258
num = util::parse_update_count(tag);
@@ -1357,7 +1358,7 @@ impl<'stmt> PostgresRows<'stmt> {
13571358
ReadyForQuery { .. } => break,
13581359
ErrorResponse { fields } => {
13591360
try!(conn.wait_for_ready());
1360-
return Err(PgDbError(PostgresDbError::new(fields)));
1361+
return PostgresDbError::new(fields);
13611362
}
13621363
_ => {}
13631364
}
@@ -1381,7 +1382,7 @@ impl<'stmt> PostgresRows<'stmt> {
13811382
DataRow { row } => self.data.push(row),
13821383
ErrorResponse { fields } => {
13831384
try!(conn.wait_for_ready());
1384-
return Err(PgDbError(PostgresDbError::new(fields)));
1385+
return PostgresDbError::new(fields);
13851386
}
13861387
CopyInResponse { .. } => {
13871388
try_pg!(conn.write_messages([
@@ -1622,7 +1623,7 @@ impl<'a> PostgresCopyInStatement<'a> {
16221623
BindComplete => {},
16231624
ErrorResponse { fields } => {
16241625
try!(conn.wait_for_ready());
1625-
return Err(PgDbError(PostgresDbError::new(fields)));
1626+
return PostgresDbError::new(fields);
16261627
}
16271628
_ => {
16281629
conn.desynchronized = true;
@@ -1692,7 +1693,7 @@ impl<'a> PostgresCopyInStatement<'a> {
16921693
CommandComplete { tag } => util::parse_update_count(tag),
16931694
ErrorResponse { fields } => {
16941695
try!(conn.wait_for_ready());
1695-
return Err(PgDbError(PostgresDbError::new(fields)));
1696+
return PostgresDbError::new(fields);
16961697
}
16971698
_ => {
16981699
conn.desynchronized = true;

0 commit comments

Comments
 (0)