Expand description
§error-location
A lightweight utility for capturing and displaying error locations in Rust.
This crate provides a simple wrapper around std::panic::Location to make it easier
to track where errors originate in your code. It’s particularly useful when building
custom error types with crates like thiserror.
§Usage
ⓘ
use error_location::ErrorLocation;
use std::panic::Location;
#[track_caller]
fn might_fail() -> Result<(), String> {
let location = ErrorLocation::from(Location::caller());
Err(format!("Something went wrong at {}", location))
}
fn main() {
match might_fail() {
Ok(_) => println!("Success!"),
Err(e) => eprintln!("Error: {}", e),
}
}§Example with thiserror
ⓘ
use error_location::ErrorLocation;
use std::panic::Location;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum MyError {
#[error("Database error at {location}: {message}")]
Database {
message: String,
location: ErrorLocation,
},
}
#[track_caller]
fn query_database() -> Result<(), MyError> {
Err(MyError::Database {
message: "Connection failed".to_string(),
location: ErrorLocation::from(Location::caller()),
})
}Structs§
- Error
Location - A lightweight wrapper around
std::panic::Locationfor tracking error origins.