Skip to content

Commit

Permalink
Merge pull request diesel-rs#2860 from hi-rustin/rustin-patch-error
Browse files Browse the repository at this point in the history
 Add ClosedConnection error kind
  • Loading branch information
weiznich authored Aug 18, 2021
2 parents 3640bc6 + 07a24a4 commit 6eae69f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
9 changes: 8 additions & 1 deletion diesel/src/pg/connection/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ use super::raw::RawResult;
use super::row::PgRow;
use crate::result::{DatabaseErrorInformation, DatabaseErrorKind, Error, QueryResult};

// Message after a database connection has been unexpectedly closed.
const CLOSED_CONNECTION_MSG: &str = "server closed the connection unexpectedly\n\t\
This probably means the server terminated abnormally\n\tbefore or while processing the request.\n";

pub struct PgResult {
internal_result: RawResult,
column_count: usize,
Expand Down Expand Up @@ -38,7 +42,7 @@ impl PgResult {
))
}
_ => {
let error_kind =
let mut error_kind =
match get_result_field(internal_result.as_ptr(), ResultField::SqlState) {
Some(error_codes::UNIQUE_VIOLATION) => DatabaseErrorKind::UniqueViolation,
Some(error_codes::FOREIGN_KEY_VIOLATION) => {
Expand All @@ -57,6 +61,9 @@ impl PgResult {
_ => DatabaseErrorKind::Unknown,
};
let error_information = Box::new(PgErrorInformation(internal_result));
if error_information.message() == CLOSED_CONNECTION_MSG {
error_kind = DatabaseErrorKind::ClosedConnection;
}
Err(Error::DatabaseError(error_kind, error_information))
}
}
Expand Down
6 changes: 6 additions & 0 deletions diesel/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ pub enum DatabaseErrorKind {
/// A check constraint was violated.
CheckViolation,

/// The connection to the server was unexpectedly closed.
///
/// This error is only detected for PostgreSQL and is emitted on a best-effort basis
/// and may be missed.
ClosedConnection,

#[doc(hidden)]
Unknown, // Match against _ instead, more variants may be added in the future
}
Expand Down

0 comments on commit 6eae69f

Please sign in to comment.