Skip to content

Commit 75527be

Browse files
committed
Add handling of DB-Errors in Notification processing
Handle the error that is generated when the DB connection goes down instead of panicing with an unreachable code error. This allows the notification client to retry establishing the connection instead of crashing the complete program.
1 parent 64286d0 commit 75527be

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

postgres/src/notification.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
use fallible_iterator::{FallibleIterator, IntoFallibleIterator};
44
use std::fmt;
55
use std::time::Duration;
6-
use postgres_protocol::message::backend;
6+
use postgres_protocol::message::backend::{self, ErrorFields};
7+
use error::DbError;
78

89
#[doc(inline)]
10+
use postgres_shared;
911
pub use postgres_shared::Notification;
1012

1113
use {desynchronized, Result, Connection};
@@ -110,6 +112,7 @@ impl<'a> FallibleIterator for Iter<'a> {
110112
payload: body.message()?.to_owned(),
111113
}))
112114
}
115+
Ok(Some(backend::Message::ErrorResponse(body))) => Err(err(&mut body.fields())),
113116
Ok(None) => Ok(None),
114117
Err(err) => Err(err.into()),
115118
_ => unreachable!(),
@@ -149,6 +152,7 @@ impl<'a> FallibleIterator for BlockingIter<'a> {
149152
payload: body.message()?.to_owned(),
150153
}))
151154
}
155+
Ok(backend::Message::ErrorResponse(body)) => Err(err(&mut body.fields())),
152156
Err(err) => Err(err.into()),
153157
_ => unreachable!(),
154158
}
@@ -185,6 +189,7 @@ impl<'a> FallibleIterator for TimeoutIter<'a> {
185189
payload: body.message()?.to_owned(),
186190
}))
187191
}
192+
Ok(Some(backend::Message::ErrorResponse(body))) => Err(err(&mut body.fields())),
188193
Ok(None) => Ok(None),
189194
Err(err) => Err(err.into()),
190195
_ => unreachable!(),
@@ -195,3 +200,10 @@ impl<'a> FallibleIterator for TimeoutIter<'a> {
195200
(self.conn.0.borrow().notifications.len(), None)
196201
}
197202
}
203+
204+
fn err(fields: &mut ErrorFields) -> Error {
205+
match DbError::new(fields) {
206+
Ok(err) => postgres_shared::error::db(err),
207+
Err(err) => err.into(),
208+
}
209+
}

0 commit comments

Comments
 (0)