pub struct ToolError {
pub message: String,
/* private fields */
}Expand description
An error returned from a tool execution. The error message is sent back to the model as the tool’s error response. Structured metadata can be attached for hooks and logging — it is not sent to the model.
Implements From<String> and From<&str> for ergonomic construction.
§Example
use llm_tool::ToolError;
use serde::Serialize;
let err: ToolError = "something went wrong".into();
assert_eq!(err.to_string(), "something went wrong");
let err = ToolError::new(format!("failed to read {}", "file.txt"));
assert!(err.to_string().contains("file.txt"));
// Structured metadata from a typed struct (preferred)
#[derive(Serialize)]
struct HttpErrorMeta {
status_code: u16,
url: String,
}
let err = ToolError::new("HTTP request failed")
.with_metadata(&HttpErrorMeta {
status_code: 503,
url: "https://example.com".into(),
})
.unwrap();
assert_eq!(err.metadata()["status_code"], 503);
// Single ad-hoc entry
let err = ToolError::new("timeout").with_meta("retry_after_secs", serde_json::json!(30));
assert_eq!(err.metadata()["retry_after_secs"], 30);Fields§
§message: StringHuman-readable error message sent to the model.
Implementations§
Source§impl ToolError
impl ToolError
Sourcepub fn with_meta(self, key: impl Into<String>, value: Value) -> Self
pub fn with_meta(self, key: impl Into<String>, value: Value) -> Self
Attach a single metadata key-value pair. Chainable.
For attaching multiple fields at once, prefer
with_metadata with a typed struct.
Sourcepub fn with_metadata<T: Serialize>(self, value: &T) -> Result<Self, Self>
pub fn with_metadata<T: Serialize>(self, value: &T) -> Result<Self, Self>
Attach structured metadata from a serializable value.
The value is serialized to a JSON object and its fields are merged
into the metadata map. See ToolOutput::with_metadata for details.
§Errors
Returns Err(self) if value doesn’t serialize to a JSON object.
Trait Implementations§
impl Eq for ToolError
Source§impl Error for ToolError
impl Error for ToolError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0:
use the Display impl or to_string()
Source§impl From<Infallible> for ToolError
impl From<Infallible> for ToolError
Source§fn from(never: Infallible) -> Self
fn from(never: Infallible) -> Self
Converts to this type from the input type.
impl StructuralPartialEq for ToolError
Auto Trait Implementations§
impl Freeze for ToolError
impl RefUnwindSafe for ToolError
impl Send for ToolError
impl Sync for ToolError
impl Unpin for ToolError
impl UnsafeUnpin for ToolError
impl UnwindSafe 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
Mutably borrows from an owned value. Read more