use {
anstream::println,
owo_colors::*,
problemo::{backtrace::*, common::*, *},
std::panic::*,
};
#[track_caller]
fn read_file(path: &str) -> Result<String, Problem> {
std::fs::read_to_string(path)
.via(LowLevelError)
.with_location()
.with_backtrace()
}
fn main() {
if let Err(problem) = read_file("non-existing.txt") {
if let Some(location) = problem.attachment_of_type::<Location>() {
println!("location:");
println!(
"{} @ {}:{}",
location.file().yellow(),
location.line().red(),
location.column().red()
);
}
if let Some(backtrace) = problem.attachment_of_type::<Backtrace>() {
println!("backtrace:");
for frame in backtrace.frames() {
for symbol in frame.symbols() {
if let Some(name) = symbol.name() {
println!("• {}", name.blue());
}
if let Some(filename) = symbol.filename() {
println!(" {}{}", filename.display().yellow(), location(symbol));
}
}
}
}
}
}
fn location(symbol: &BacktraceSymbol) -> String {
if let Some(lineno) = symbol.lineno()
&& let Some(colno) = symbol.colno()
{
format!(" @ {}:{}", lineno.red(), colno.red())
} else {
Default::default()
}
}