|
1 | 1 | //! Postgres errors |
2 | 2 |
|
3 | 3 | use std::collections::HashMap; |
| 4 | +use std::error; |
4 | 5 | use std::io; |
5 | 6 | use std::fmt; |
6 | 7 | use std::result; |
7 | 8 |
|
8 | | -use openssl::ssl::error; |
| 9 | +use openssl::ssl::error as sslerror; |
9 | 10 | use phf; |
10 | 11 |
|
11 | 12 | use Result; |
@@ -375,13 +376,49 @@ pub enum ConnectError { |
375 | 376 | /// The Postgres server does not support SSL encryption |
376 | 377 | NoSslSupport, |
377 | 378 | /// There was an error initializing the SSL session |
378 | | - SslError(error::SslError), |
| 379 | + SslError(sslerror::SslError), |
379 | 380 | /// There was an error communicating with the server |
380 | 381 | PgConnectStreamError(io::IoError), |
381 | 382 | /// The server sent an unexpected response |
382 | 383 | PgConnectBadResponse, |
383 | 384 | } |
384 | 385 |
|
| 386 | +impl error::Error for ConnectError { |
| 387 | + fn description(&self) -> &str { |
| 388 | + match *self { |
| 389 | + InvalidUrl(_) => "Invalid URL", |
| 390 | + MissingUser => "User missing in URL", |
| 391 | + SocketError(_) => "Unable to open connection to server", |
| 392 | + PgConnectDbError(_) => "An error from the Postgres server itself", |
| 393 | + MissingPassword => "The server requested a password but none was provided", |
| 394 | + UnsupportedAuthentication => { |
| 395 | + "The server requested an unsupported authentication method" |
| 396 | + } |
| 397 | + NoSslSupport => "The server does not support SSL", |
| 398 | + SslError(_) => "Error initiating SSL session", |
| 399 | + PgConnectStreamError(_) => "Error communicating with server", |
| 400 | + PgConnectBadResponse => "The server returned an unexpected response", |
| 401 | + } |
| 402 | + } |
| 403 | + |
| 404 | + fn detail(&self) -> Option<String> { |
| 405 | + match *self { |
| 406 | + InvalidUrl(ref msg) => Some(msg.clone()), |
| 407 | + _ => None, |
| 408 | + } |
| 409 | + } |
| 410 | + |
| 411 | + fn cause(&self) -> Option<&error::Error> { |
| 412 | + match *self { |
| 413 | + SocketError(ref err) => Some(err as &error::Error), |
| 414 | + PgConnectDbError(ref err) => Some(err as &error::Error), |
| 415 | + SslError(ref err) => Some(err as &error::Error), |
| 416 | + PgConnectStreamError(ref err) => Some(err as &error::Error), |
| 417 | + _ => None |
| 418 | + } |
| 419 | + } |
| 420 | +} |
| 421 | + |
385 | 422 | impl fmt::Show for ConnectError { |
386 | 423 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
387 | 424 | match *self { |
@@ -531,6 +568,16 @@ impl fmt::Show for DbError { |
531 | 568 | } |
532 | 569 | } |
533 | 570 |
|
| 571 | +impl error::Error for DbError { |
| 572 | + fn description(&self) -> &str { |
| 573 | + &*self.message |
| 574 | + } |
| 575 | + |
| 576 | + fn detail(&self) -> Option<String> { |
| 577 | + self.detail.clone() |
| 578 | + } |
| 579 | +} |
| 580 | + |
534 | 581 | /// An error encountered when communicating with the Postgres server |
535 | 582 | #[deriving(Clone, PartialEq, Eq)] |
536 | 583 | pub enum Error { |
@@ -591,3 +638,45 @@ impl fmt::Show for Error { |
591 | 638 | } |
592 | 639 | } |
593 | 640 | } |
| 641 | + |
| 642 | +impl error::Error for Error { |
| 643 | + fn description(&self) -> &str { |
| 644 | + match *self { |
| 645 | + PgDbError(_) => "An error reported by the Postgres server", |
| 646 | + PgStreamError(_) => "An error communicating with the Postgres server", |
| 647 | + PgStreamDesynchronized => { |
| 648 | + "Communication with the server has desynchronized due to an earlier IO error" |
| 649 | + } |
| 650 | + PgWrongConnection => { |
| 651 | + "A statement was executed with a connection with which it was not prepared" |
| 652 | + } |
| 653 | + PgWrongParamCount { .. } => "Wrong number of parameters", |
| 654 | + PgWrongType(_) => "Unexpected type", |
| 655 | + PgInvalidColumn => "Invalid column", |
| 656 | + PgWasNull => "The value was NULL", |
| 657 | + PgWrongTransaction => { |
| 658 | + "An attempt was made to use an object other than the active transaction" |
| 659 | + } |
| 660 | + PgBadResponse => "The server returned an unexpected response", |
| 661 | + PgBadData => "The server provided data that the client could not parse", |
| 662 | + } |
| 663 | + } |
| 664 | + |
| 665 | + fn detail(&self) -> Option<String> { |
| 666 | + match *self { |
| 667 | + PgWrongParamCount { expected, actual } => { |
| 668 | + Some(format!("expected: {}, actual: {}", expected, actual)) |
| 669 | + } |
| 670 | + PgWrongType(ref ty) => Some(format!("saw type {}", ty)), |
| 671 | + _ => None |
| 672 | + } |
| 673 | + } |
| 674 | + |
| 675 | + fn cause(&self) -> Option<&error::Error> { |
| 676 | + match *self { |
| 677 | + PgDbError(ref err) => Some(err as &error::Error), |
| 678 | + PgStreamError(ref err) => Some(err as &error::Error), |
| 679 | + _ => None |
| 680 | + } |
| 681 | + } |
| 682 | +} |
0 commit comments