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>
impl<'a> ErrorBoundary<'a>
Sourcepub fn new(console: &'a FastMcpConsole) -> Self
pub fn new(console: &'a FastMcpConsole) -> Self
Sourcepub fn with_exit_on_error(self, exit: bool) -> Self
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());Sourcepub fn wrap<T, E>(&self, result: Result<T, E>) -> Option<T>
pub fn wrap<T, E>(&self, result: Result<T, E>) -> Option<T>
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 typeE- The error type, which must be convertible toMcpError
§Example
let boundary = ErrorBoundary::new(console());
if let Some(config) = boundary.wrap(load_config()) {
// Use config...
}Sourcepub fn wrap_with_context<T, E>(
&self,
result: Result<T, E>,
context: &str,
) -> Option<T>
pub fn wrap_with_context<T, E>( &self, result: Result<T, E>, context: &str, ) -> Option<T>
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"
);Sourcepub fn wrap_result<T, E>(&self, result: Result<T, E>) -> Result<T, McpError>
pub fn wrap_result<T, E>(&self, result: Result<T, E>) -> Result<T, 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 */ }
}Sourcepub fn wrap_result_with_context<T, E>(
&self,
result: Result<T, E>,
context: &str,
) -> Result<T, McpError>
pub fn wrap_result_with_context<T, E>( &self, result: Result<T, E>, context: &str, ) -> Result<T, 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.
Sourcepub fn display_error(&self, error: &McpError)
pub fn display_error(&self, error: &McpError)
Sourcepub fn error_count(&self) -> usize
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.
Sourcepub fn has_errors(&self) -> bool
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.
Sourcepub fn reset_count(&self)
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.