forked from launchbadge/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
breaking: fix name collision in
FromRow
, return `Error::ColumnDecod…
…e` for `TryFrom` errors (launchbadge#3356) * chore: create regression test for launchbadge#3344 * fix(derives): use a parameter name that's less likely to collide * breaking(derives): emit `Error::ColumnDecode` when a `TryFrom` conversion fails in `FromRow` Breaking because `#[sqlx(default)]` on an individual field or the struct itself would have previously suppressed the error. This doesn't seem like good behavior as it could result in some potentially very difficult bugs. Instead of using `TryFrom` for these fields, just implement `From` and apply the default explicitly. * fix: run `cargo fmt` * fix: use correct field in `ColumnDecode`
- Loading branch information
Showing
4 changed files
with
181 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use std::any::Any; | ||
use std::error::Error; | ||
use std::fmt::{Debug, Display}; | ||
|
||
// Autoderef specialization similar to `clap::value_parser!()`. | ||
pub struct SpecErrorWrapper<E>(pub E); | ||
|
||
pub trait SpecError<E>: Sized { | ||
fn __sqlx_spec_error( | ||
&self, | ||
) -> fn(SpecErrorWrapper<E>) -> Box<dyn Error + Send + Sync + 'static>; | ||
} | ||
|
||
impl<E> SpecError<E> for &&&&SpecErrorWrapper<E> | ||
where | ||
E: Error + Send + Sync + 'static, | ||
{ | ||
fn __sqlx_spec_error( | ||
&self, | ||
) -> fn(SpecErrorWrapper<E>) -> Box<dyn Error + Send + Sync + 'static> { | ||
|e| Box::new(e.0) | ||
} | ||
} | ||
|
||
impl<E> SpecError<E> for &&&SpecErrorWrapper<E> | ||
where | ||
E: Display, | ||
{ | ||
fn __sqlx_spec_error( | ||
&self, | ||
) -> fn(SpecErrorWrapper<E>) -> Box<dyn Error + Send + Sync + 'static> { | ||
|e| e.0.to_string().into() | ||
} | ||
} | ||
|
||
impl<E> SpecError<E> for &&SpecErrorWrapper<E> | ||
where | ||
E: Debug, | ||
{ | ||
fn __sqlx_spec_error( | ||
&self, | ||
) -> fn(SpecErrorWrapper<E>) -> Box<dyn Error + Send + Sync + 'static> { | ||
|e| format!("{:?}", e.0).into() | ||
} | ||
} | ||
|
||
impl<E> SpecError<E> for &SpecErrorWrapper<E> | ||
where | ||
E: Any, | ||
{ | ||
fn __sqlx_spec_error( | ||
&self, | ||
) -> fn(SpecErrorWrapper<E>) -> Box<dyn Error + Send + Sync + 'static> { | ||
|_e| format!("unprintable error: {}", std::any::type_name::<E>()).into() | ||
} | ||
} | ||
|
||
impl<E> SpecError<E> for SpecErrorWrapper<E> { | ||
fn __sqlx_spec_error( | ||
&self, | ||
) -> fn(SpecErrorWrapper<E>) -> Box<dyn Error + Send + Sync + 'static> { | ||
|_e| "unprintable error: (unprintable type)".into() | ||
} | ||
} | ||
|
||
#[doc(hidden)] | ||
#[macro_export] | ||
macro_rules! __spec_error { | ||
($e:expr) => {{ | ||
use $crate::spec_error::{SpecError, SpecErrorWrapper}; | ||
|
||
let wrapper = SpecErrorWrapper($e); | ||
let wrap_err = wrapper.__sqlx_spec_error(); | ||
wrap_err(wrapper) | ||
}}; | ||
} | ||
|
||
#[test] | ||
fn test_spec_error() { | ||
#[derive(Debug)] | ||
struct DebugError; | ||
|
||
struct AnyError; | ||
|
||
let _e: Box<dyn Error + Send + Sync + 'static> = | ||
__spec_error!(std::io::Error::from(std::io::ErrorKind::Unsupported)); | ||
|
||
let _e: Box<dyn Error + Send + Sync + 'static> = __spec_error!("displayable error"); | ||
|
||
let _e: Box<dyn Error + Send + Sync + 'static> = __spec_error!(DebugError); | ||
|
||
let _e: Box<dyn Error + Send + Sync + 'static> = __spec_error!(AnyError); | ||
|
||
let _e: Box<dyn Error + Send + Sync + 'static> = __spec_error!(&1i32); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters