pub struct WithSource<E>(pub E);Expand description
Wrapper that formats the full error chain for tool responses.
By default, tool errors only show the top-level message from [Display]. When using
thiserror with #[source], nested error causes are attached but not displayed. This wrapper
traverses the Error::source chain to produce a complete message
like "google API error: HTTP request failed: connection refused".
This pattern follows the design from
The elements of Rust error handling:
keep Display impls minimal and let callers decide when to show the full chain.
§When to Use
Use WithSource when your error type has nested causes that would help the LLM understand
what went wrong. This is especially useful for errors from external services, I/O operations,
or any chain where the root cause provides actionable information.
§Example
use mercutio::WithSource;
async fn handle_tool(tool: MyTools) -> Result<String, WithSource<MyError>> {
let data = fetch_api().map_err(WithSource)?;
Ok(format!("Got: {data}"))
}Without WithSource, an API failure might show: "API request failed"
With WithSource, it shows: "API request failed: HTTP error: 403 Forbidden"
Tuple Fields§
§0: ETrait Implementations§
Source§impl<E: Debug> Debug for WithSource<E>
impl<E: Debug> Debug for WithSource<E>
Source§impl<E: Error> Display for WithSource<E>
impl<E: Error> Display for WithSource<E>
Source§impl<E: Error> Error for WithSource<E>
impl<E: Error> Error for WithSource<E>
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()