Skip to main content

ErrorBoundary

Struct ErrorBoundary 

Source
pub struct ErrorBoundary<'a> { /* private fields */ }
Expand description

Wraps operations and displays errors beautifully on failure.

ErrorBoundary provides a consistent way to handle and display errors throughout a FastMCP application. Instead of manually calling error rendering at every error site, wrap operations with an ErrorBoundary and it will automatically handle display on failure.

§Thread Safety

ErrorBoundary is thread-safe and can be shared across threads. The error count is tracked using atomic operations.

§Exit on Error

For CLI applications, you can configure the boundary to exit the process on error using with_exit_on_error.

Implementations§

Source§

impl<'a> ErrorBoundary<'a>

Source

pub fn new(console: &'a FastMcpConsole) -> Self

Creates a new ErrorBoundary with the given console.

The boundary will use the console’s theme and context for rendering errors.

§Example
use fastmcp_console::{console, error::ErrorBoundary};

let boundary = ErrorBoundary::new(console());
Source

pub fn with_exit_on_error(self, exit: bool) -> Self

Configures the boundary to exit the process on error.

When exit is true, any error will cause the process to exit with code 1 after displaying the error. This is useful for CLI applications where errors should terminate the program.

§Example
let boundary = ErrorBoundary::new(console())
    .with_exit_on_error(true);

// This will exit the process if load_config() fails
boundary.wrap(load_config());
Source

pub fn wrap<T, E>(&self, result: Result<T, E>) -> Option<T>
where E: Into<McpError>,

Wraps a Result, displaying error if Err.

Returns Some(value) on success, or None on error. The error is displayed using the configured console and renderer.

§Type Parameters
  • T - The success type
  • E - The error type, which must be convertible to McpError
§Example
let boundary = ErrorBoundary::new(console());

if let Some(config) = boundary.wrap(load_config()) {
    // Use config...
}
Source

pub fn wrap_with_context<T, E>( &self, result: Result<T, E>, context: &str, ) -> Option<T>
where E: Into<McpError>,

Wraps a Result with a custom context message.

Like wrap, but displays an additional context message before the error to help identify where the error occurred.

§Example
let boundary = ErrorBoundary::new(console());

let config = boundary.wrap_with_context(
    load_config(),
    "Loading server configuration"
);
Source

pub fn wrap_result<T, E>(&self, result: Result<T, E>) -> Result<T, McpError>
where E: Into<McpError>,

Wraps a Result, returning the error if present.

Unlike wrap, this returns Result<T, McpError> instead of Option<T>. The error is still displayed, but you can also handle it programmatically.

§Example
let boundary = ErrorBoundary::new(console());

match boundary.wrap_result(load_config()) {
    Ok(config) => { /* use config */ }
    Err(e) => { /* error was displayed, but we can also log it */ }
}
Source

pub fn wrap_result_with_context<T, E>( &self, result: Result<T, E>, context: &str, ) -> Result<T, McpError>
where E: Into<McpError>,

Wraps a Result with context, returning the error if present.

Combines wrap_with_context and wrap_result - displays context and error, then returns the error for further handling.

Source

pub fn display_error(&self, error: &McpError)

Displays an error directly without wrapping a Result.

This is useful when you already have an McpError that you want to display.

§Example
let boundary = ErrorBoundary::new(console());
let error = McpError::internal_error("Something went wrong");
boundary.display_error(&error);
Source

pub fn error_count(&self) -> usize

Gets the total number of errors that have occurred.

This count is incremented each time an error is handled through this boundary.

Source

pub fn has_errors(&self) -> bool

Checks if any errors have occurred.

Returns true if at least one error has been handled through this boundary.

Source

pub fn reset_count(&self)

Resets the error count to zero.

This can be useful when reusing a boundary for multiple operations where you want to track errors separately.

Auto Trait Implementations§

§

impl<'a> !Freeze for ErrorBoundary<'a>

§

impl<'a> RefUnwindSafe for ErrorBoundary<'a>

§

impl<'a> Send for ErrorBoundary<'a>

§

impl<'a> Sync for ErrorBoundary<'a>

§

impl<'a> Unpin for ErrorBoundary<'a>

§

impl<'a> UnwindSafe for ErrorBoundary<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more