pub struct ErrorMapper { /* private fields */ }Expand description
Maps errors of one type (anywhere in an AppError’s source chain) to a
specific AppError. Contributed with
ModuleBuilderHttpExt::map_error.
Implementations§
Source§impl ErrorMapper
impl ErrorMapper
Sourcepub fn new<E: StdError + 'static>(f: fn(&E) -> Option<AppError>) -> Self
pub fn new<E: StdError + 'static>(f: fn(&E) -> Option<AppError>) -> Self
A mapper for errors of type E; returning None keeps the 500.
Sourcepub fn apply(mappers: &[ErrorMapper], error: &AppError) -> Option<AppError>
pub fn apply(mappers: &[ErrorMapper], error: &AppError) -> Option<AppError>
Applies the first matching mapper to error’s source chain; None
when no mapper matches. The HTTP pipeline does this for every 500
with a source; other protocols (navree-grpc) and code that reports
errors outside a response (MCP tool results, for example) collect the
app’s mappers with ctx.contributions::<ErrorMapper>() and call it
too.
use http::StatusCode;
use navree_http::{AppError, ErrorMapper};
#[derive(Debug)]
struct Missing;
impl std::fmt::Display for Missing {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("missing")
}
}
impl std::error::Error for Missing {}
let mappers = [ErrorMapper::new(|_: &Missing| Some(AppError::not_found()))];
let mapped = ErrorMapper::apply(&mappers, &AppError::internal(Missing)).unwrap();
assert_eq!(mapped.status, StatusCode::NOT_FOUND);
assert!(ErrorMapper::apply(&mappers, &AppError::internal("other")).is_none());Trait Implementations§
Source§impl Clone for ErrorMapper
impl Clone for ErrorMapper
Auto Trait Implementations§
impl !RefUnwindSafe for ErrorMapper
impl !UnwindSafe for ErrorMapper
impl Forget for ErrorMapper
impl Freeze for ErrorMapper
impl Move for ErrorMapper
impl Send for ErrorMapper
impl Sync for ErrorMapper
impl Unpin for ErrorMapper
impl UnsafeUnpin for ErrorMapper
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more