pub struct ToolError { /* private fields */ }Expand description
The error a tool body returns, much like anyhow::Error.
Any std::error::Error converts into it with ?, and so does the InterruptError that
ToolContext::interrupt returns, also after it passed
through a BoxError. The model sees a failure as an error result with the text
Error: <message>; an interrupt ends the tool as ToolOutcome::Interrupt instead, so a tool
cannot turn an interrupt into a failure by accident.
ToolError does not implement std::error::Error itself, which is what lets every error
type convert into it.
use strands::ToolError;
fn parse(input: &str) -> Result<u32, ToolError> {
Ok(input.parse::<u32>()?)
}
assert_eq!(parse("x").unwrap_err().to_string(), "invalid digit found in string");
assert_eq!(ToolError::msg("city not found").to_string(), "city not found");Implementations§
Source§impl ToolError
impl ToolError
Sourcepub fn new(error: impl StdError + Send + Sync + 'static) -> Self
pub fn new(error: impl StdError + Send + Sync + 'static) -> Self
Wraps an error; an InterruptError stays an interrupt.
Sourcepub fn from_boxed(error: BoxError) -> Self
pub fn from_boxed(error: BoxError) -> Self
Wraps a boxed error: a BoxError, or what an error type without a
std::error::Error impl (such as anyhow::Error) converts into. BoxError cannot
convert with ? itself, because it does not implement std::error::Error.
use strands::{BoxError, ToolError};
fn lookup() -> Result<u32, BoxError> {
Err("not found".into())
}
fn tool_body() -> Result<u32, ToolError> {
lookup().map_err(ToolError::from_boxed)
}
assert_eq!(tool_body().unwrap_err().to_string(), "not found");An InterruptError inside stays an interrupt.
Sourcepub fn is_interrupt(&self) -> bool
pub fn is_interrupt(&self) -> bool
Whether this is an interrupt rather than a failure.
Sourcepub fn as_interrupt(&self) -> Option<&InterruptError>
pub fn as_interrupt(&self) -> Option<&InterruptError>
The interrupts, when this is one.
Sourcepub fn as_error(&self) -> Option<&(dyn StdError + Send + Sync + 'static)>
pub fn as_error(&self) -> Option<&(dyn StdError + Send + Sync + 'static)>
The underlying error, unless this is an interrupt.
Sourcepub fn into_outcome(self, tool_use_id: impl Into<String>) -> ToolOutcome
pub fn into_outcome(self, tool_use_id: impl Into<String>) -> ToolOutcome
The outcome of a tool that failed with this error: an error result with the text
Error: <message> that keeps the error, or the interrupt.
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for ToolError
impl !UnwindSafe for ToolError
impl Freeze for ToolError
impl Send for ToolError
impl Sync for ToolError
impl Unpin for ToolError
impl UnsafeUnpin for ToolError
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more