Struct pgx::PgTryBuilder
source · pub struct PgTryBuilder<'a, R, F>where
F: FnOnce() -> R + UnwindSafe,{ /* private fields */ }
Expand description
PgTryBuilder
is a mechanism to mimic Postgres C macros PG_TRY
and PG_CATCH
.
A primary difference is that the PgTryBuilder::finally()
block runs even if a catch handler
rethrows (or throws a new) error.
Implementations§
source§impl<'a, R, F> PgTryBuilder<'a, R, F>where
F: FnOnce() -> R + UnwindSafe,
impl<'a, R, F> PgTryBuilder<'a, R, F>where F: FnOnce() -> R + UnwindSafe,
sourcepub fn new(func: F) -> PgTryBuilder<'a, R, F>
pub fn new(func: F) -> PgTryBuilder<'a, R, F>
Create a new [PgTryBuilder]
. The func
argument specifies the closure that is to run.
If it fails with either a Rust panic or a Postgres error, a registered catch handler for that specific error is run. Whether one exists or not, the finally block also runs at the end, for any necessary cleanup.
Example
let i = 41;
let mut finished = false;
let result = PgTryBuilder::new(|| {
if i < 42 {
ereport!(ERROR, PgSqlErrorCode::ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE, "number too small");
}
i
})
.catch_when(PgSqlErrorCode::ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE, |cause| cause.rethrow())
.finally(|| finished = true)
.execute();
assert_eq!(finished, true);
assert_eq!(result, 42);
sourcepub fn catch_when(
self,
error: PgSqlErrorCode,
f: impl FnMut(CaughtError) -> R + UnwindSafe + RefUnwindSafe + 'a
) -> PgTryBuilder<'a, R, F>
pub fn catch_when( self, error: PgSqlErrorCode, f: impl FnMut(CaughtError) -> R + UnwindSafe + RefUnwindSafe + 'a ) -> PgTryBuilder<'a, R, F>
Add a catch handler to run should a specific error occur during execution.
The argument to the catch handler closure is a CaughtError
which can be
rethrown via CaughtError::rethrow()
The return value must be of the same type as the main execution block, or the supplied error cause must be rethrown.
Safety
While this function isn’t itself unsafe, catching and then ignoring an important internal Postgres error may very well leave your database in an undesirable state. This is your responsibility.
sourcepub fn catch_others(
self,
f: impl FnMut(CaughtError) -> R + UnwindSafe + RefUnwindSafe + 'a
) -> PgTryBuilder<'a, R, F>
pub fn catch_others( self, f: impl FnMut(CaughtError) -> R + UnwindSafe + RefUnwindSafe + 'a ) -> PgTryBuilder<'a, R, F>
Add a catch-all handler to catch a raised error that wasn’t explicitly caught via PgTryBuilder::catch_when.
The argument to the catch handler closure is a CaughtError
which can be
rethrown via CaughtError::rethrow()
.
The return value must be of the same type as the main execution block, or the supplied error cause must be rethrown.
Safety
While this function isn’t itself unsafe, catching and then ignoring an important internal Postgres error may very well leave your database in an undesirable state. This is your responsibility.
sourcepub fn catch_rust_panic(
self,
f: impl FnMut(CaughtError) -> R + UnwindSafe + RefUnwindSafe + 'a
) -> PgTryBuilder<'a, R, F>
pub fn catch_rust_panic( self, f: impl FnMut(CaughtError) -> R + UnwindSafe + RefUnwindSafe + 'a ) -> PgTryBuilder<'a, R, F>
Add a handler to specifically respond to a Rust panic.
The catch handler’s closure argument is a [CaughtError::PostgresError {ereport, payload}
]
that you can inspect in whatever way makes sense.
The return value must be of the same type as the main execution block, or the supplied error cause must be rethrown.
sourcepub fn finally(self, f: impl FnMut() + 'a) -> PgTryBuilder<'a, R, F>
pub fn finally(self, f: impl FnMut() + 'a) -> PgTryBuilder<'a, R, F>
The finally block, of which there can be only one. Successive calls to this function will replace the prior finally block.
The finally block closure is called after successful return from the main execution handler or a catch handler. The finally block does not return a value.
Auto Trait Implementations§
impl<'a, R, F> !RefUnwindSafe for PgTryBuilder<'a, R, F>
impl<'a, R, F> !Send for PgTryBuilder<'a, R, F>
impl<'a, R, F> !Sync for PgTryBuilder<'a, R, F>
impl<'a, R, F> Unpin for PgTryBuilder<'a, R, F>where F: Unpin,
impl<'a, R, F> !UnwindSafe for PgTryBuilder<'a, R, F>
Blanket Implementations§
§impl<T> Conv for T
impl<T> Conv for T
§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where Self: Display,
self
to use its Display
implementation when
Debug
-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where &'a Self: for<'a> IntoIterator,
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere R: 'a,
self
and passes that borrow into the pipe function. Read more§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere
Self: Borrow<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> Rwhere Self: Borrow<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R
) -> Rwhere
Self: BorrowMut<B>,
B: 'a + ?Sized,
R: 'a,
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> Rwhere Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere
Self: AsRef<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> Rwhere Self: AsRef<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_ref()
into the pipe function.§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere
Self: AsMut<U>,
U: 'a + ?Sized,
R: 'a,
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> Rwhere Self: AsMut<U>, U: 'a + ?Sized, R: 'a,
self
, then passes self.as_mut()
into the pipe
function.§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
Borrow<B>
of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
BorrowMut<B>
of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
AsRef<R>
view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
AsMut<R>
view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere
Self: Deref<Target = T>,
T: ?Sized,
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Selfwhere Self: Deref<Target = T>, T: ?Sized,
Deref::Target
of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere
Self: DerefMut<Target = T> + Deref,
T: ?Sized,
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Selfwhere Self: DerefMut<Target = T> + Deref, T: ?Sized,
Deref::Target
of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere
Self: Borrow<B>,
B: ?Sized,
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Selfwhere Self: Borrow<B>, B: ?Sized,
.tap_borrow()
only in debug builds, and is erased in release
builds.§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere
Self: BorrowMut<B>,
B: ?Sized,
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Selfwhere Self: BorrowMut<B>, B: ?Sized,
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere
Self: AsRef<R>,
R: ?Sized,
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Selfwhere Self: AsRef<R>, R: ?Sized,
.tap_ref()
only in debug builds, and is erased in release
builds.§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere
Self: AsMut<R>,
R: ?Sized,
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Selfwhere Self: AsMut<R>, R: ?Sized,
.tap_ref_mut()
only in debug builds, and is erased in release
builds.