Skip to content

Commit fed246e

Browse files
committed
Error reform for tokio-postgres
1 parent 15a1b79 commit fed246e

File tree

11 files changed

+537
-495
lines changed

11 files changed

+537
-495
lines changed

postgres-shared/src/error/mod.rs

+102-30
Original file line numberDiff line numberDiff line change
@@ -307,59 +307,131 @@ pub enum ErrorPosition {
307307
},
308308
}
309309

310-
/// Reasons a new Postgres connection could fail.
310+
#[doc(hidden)]
311+
pub fn connect(e: Box<error::Error + Sync + Send>) -> Error {
312+
Error(Box::new(ErrorKind::ConnectParams(e)))
313+
}
314+
315+
#[doc(hidden)]
316+
pub fn tls(e: Box<error::Error + Sync + Send>) -> Error {
317+
Error(Box::new(ErrorKind::Tls(e)))
318+
}
319+
320+
#[doc(hidden)]
321+
pub fn db(e: DbError) -> Error {
322+
Error(Box::new(ErrorKind::Db(e)))
323+
}
324+
325+
#[doc(hidden)]
326+
pub fn io(e: io::Error) -> Error {
327+
Error(Box::new(ErrorKind::Io(e)))
328+
}
329+
330+
#[doc(hidden)]
331+
pub fn conversion(e: Box<error::Error + Sync + Send>) -> Error {
332+
Error(Box::new(ErrorKind::Conversion(e)))
333+
}
334+
311335
#[derive(Debug)]
312-
pub enum ConnectError {
313-
/// An error relating to connection parameters.
336+
enum ErrorKind {
314337
ConnectParams(Box<error::Error + Sync + Send>),
315-
/// An error from the Postgres server itself.
316-
Db(Box<DbError>),
317-
/// An error initializing the TLS session.
318338
Tls(Box<error::Error + Sync + Send>),
319-
/// An error communicating with the server.
339+
Db(DbError),
320340
Io(io::Error),
341+
Conversion(Box<error::Error + Sync + Send>),
321342
}
322343

323-
impl fmt::Display for ConnectError {
344+
/// An error communicating with the Postgres server.
345+
#[derive(Debug)]
346+
pub struct Error(Box<ErrorKind>);
347+
348+
impl fmt::Display for Error {
324349
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
325350
fmt.write_str(error::Error::description(self))?;
326-
match *self {
327-
ConnectError::ConnectParams(ref msg) => write!(fmt, ": {}", msg),
328-
ConnectError::Db(ref err) => write!(fmt, ": {}", err),
329-
ConnectError::Tls(ref err) => write!(fmt, ": {}", err),
330-
ConnectError::Io(ref err) => write!(fmt, ": {}", err),
351+
match *self.0 {
352+
ErrorKind::ConnectParams(ref err) => write!(fmt, ": {}", err),
353+
ErrorKind::Tls(ref err) => write!(fmt, ": {}", err),
354+
ErrorKind::Db(ref err) => write!(fmt, ": {}", err),
355+
ErrorKind::Io(ref err) => write!(fmt, ": {}", err),
356+
ErrorKind::Conversion(ref err) => write!(fmt, ": {}", err),
331357
}
332358
}
333359
}
334360

335-
impl error::Error for ConnectError {
361+
impl error::Error for Error {
336362
fn description(&self) -> &str {
337-
match *self {
338-
ConnectError::ConnectParams(_) => "Invalid connection parameters",
339-
ConnectError::Db(_) => "Error reported by Postgres",
340-
ConnectError::Tls(_) => "Error initiating SSL session",
341-
ConnectError::Io(_) => "Error communicating with the server",
363+
match *self.0 {
364+
ErrorKind::ConnectParams(_) => "invalid connection parameters",
365+
ErrorKind::Tls(_) => "TLS handshake error",
366+
ErrorKind::Db(_) => "database error",
367+
ErrorKind::Io(_) => "IO error",
368+
ErrorKind::Conversion(_) => "type conversion error",
342369
}
343370
}
344371

345372
fn cause(&self) -> Option<&error::Error> {
346-
match *self {
347-
ConnectError::ConnectParams(ref err) |
348-
ConnectError::Tls(ref err) => Some(&**err),
349-
ConnectError::Db(ref err) => Some(&**err),
350-
ConnectError::Io(ref err) => Some(err),
373+
match *self.0 {
374+
ErrorKind::ConnectParams(ref err) => Some(&**err),
375+
ErrorKind::Tls(ref err) => Some(&**err),
376+
ErrorKind::Db(ref err) => Some(err),
377+
ErrorKind::Io(ref err) => Some(err),
378+
ErrorKind::Conversion(ref err) => Some(&**err),
351379
}
352380
}
353381
}
354382

355-
impl From<io::Error> for ConnectError {
356-
fn from(err: io::Error) -> ConnectError {
357-
ConnectError::Io(err)
383+
impl Error {
384+
/// Returns the SQLSTATE error code associated with this error if it is a DB
385+
/// error.
386+
pub fn code(&self) -> Option<&SqlState> {
387+
self.as_db().map(|e| &e.code)
388+
}
389+
390+
/// Returns the inner error if this is a connection parameter error.
391+
pub fn as_connection(&self) -> Option<&(error::Error + 'static + Sync + Send)> {
392+
match *self.0 {
393+
ErrorKind::ConnectParams(ref err) => Some(&**err),
394+
_ => None,
395+
}
396+
}
397+
398+
/// Returns the `DbError` associated with this error if it is a DB error.
399+
pub fn as_db(&self) -> Option<&DbError> {
400+
match *self.0 {
401+
ErrorKind::Db(ref err) => Some(err),
402+
_ => None
403+
}
404+
}
405+
406+
/// Returns the inner error if this is a conversion error.
407+
pub fn as_conversion(&self) -> Option<&(error::Error + 'static + Sync + Send)> {
408+
match *self.0 {
409+
ErrorKind::Conversion(ref err) => Some(&**err),
410+
_ => None,
411+
}
412+
}
413+
414+
/// Returns the inner `io::Error` associated with this error if it is an IO
415+
/// error.
416+
pub fn as_io(&self) -> Option<&io::Error> {
417+
match *self.0 {
418+
ErrorKind::Io(ref err) => Some(err),
419+
_ => None,
420+
}
358421
}
359422
}
360423

361-
impl From<DbError> for ConnectError {
362-
fn from(err: DbError) -> ConnectError {
363-
ConnectError::Db(Box::new(err))
424+
impl From<io::Error> for Error {
425+
fn from(err: io::Error) -> Error {
426+
Error(Box::new(ErrorKind::Io(err)))
427+
}
428+
}
429+
430+
impl From<Error> for io::Error {
431+
fn from(err: Error) -> io::Error {
432+
match *err.0 {
433+
ErrorKind::Io(e) => e,
434+
_ => io::Error::new(io::ErrorKind::Other, err),
435+
}
364436
}
365437
}

postgres/src/error.rs

-112
This file was deleted.

0 commit comments

Comments
 (0)