Skip to content

Commit 42cf855

Browse files
committed
Include the Rust type name in WrongType
1 parent 54e52bd commit 42cf855

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

tokio-postgres/src/row.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ impl Row {
164164

165165
let ty = self.columns()[idx].type_();
166166
if !T::accepts(ty) {
167-
return Err(Error::from_sql(Box::new(WrongType::new(ty.clone())), idx));
167+
return Err(Error::from_sql(
168+
Box::new(WrongType::new::<T>(ty.clone())),
169+
idx,
170+
));
168171
}
169172

170173
let buf = self.ranges[idx].clone().map(|r| &self.body.buffer()[r]);

tokio-postgres/src/types/mod.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ where
6868
T: ToSql,
6969
{
7070
if !T::accepts(ty) {
71-
return Err(Box::new(WrongType(ty.clone())));
71+
return Err(Box::new(WrongType::new::<T>(ty.clone())));
7272
}
7373
v.to_sql(ty, out)
7474
}
@@ -91,6 +91,7 @@ mod type_gen;
9191

9292
#[cfg(feature = "with-serde_json-1")]
9393
pub use crate::types::serde_json_1::Json;
94+
use std::any::type_name;
9495

9596
/// A Postgres type.
9697
#[derive(PartialEq, Eq, Clone, Debug)]
@@ -206,23 +207,29 @@ impl Error for WasNull {}
206207
/// An error indicating that a conversion was attempted between incompatible
207208
/// Rust and Postgres types.
208209
#[derive(Debug)]
209-
pub struct WrongType(Type);
210+
pub struct WrongType {
211+
postgres: Type,
212+
rust: &'static str,
213+
}
210214

211215
impl fmt::Display for WrongType {
212216
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
213217
write!(
214218
fmt,
215-
"cannot convert to or from a Postgres value of type `{}`",
216-
self.0
219+
"cannot convert between the Rust type `{}` and the Postgres type `{}`",
220+
self.rust, self.postgres,
217221
)
218222
}
219223
}
220224

221225
impl Error for WrongType {}
222226

223227
impl WrongType {
224-
pub(crate) fn new(ty: Type) -> WrongType {
225-
WrongType(ty)
228+
pub(crate) fn new<T>(ty: Type) -> WrongType {
229+
WrongType {
230+
postgres: ty,
231+
rust: type_name::<T>(),
232+
}
226233
}
227234
}
228235

0 commit comments

Comments
 (0)