From 91302db72515fb754a41fa543497ab047218e775 Mon Sep 17 00:00:00 2001 From: luaneko Date: Wed, 25 Jun 2025 21:45:29 +1000 Subject: [PATCH] Print nicer error messages --- src/main.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index baf074d..7767b69 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,9 @@ use clap::Parser; use mimalloc::MiMalloc; use owo_colors::OwoColorize; -use std::{backtrace::Backtrace, fmt::Display, net::SocketAddr, num::NonZero, panic, thread}; +use std::{ + backtrace::Backtrace, fmt::Display, net::SocketAddr, num::NonZero, panic, process, thread, +}; use sysexits::ExitCode; #[global_allocator] @@ -20,21 +22,23 @@ fn panic_cb(panic: &panic::PanicHookInfo) { }; eprint!( - "{}:\n{trace}", + "{}\n{trace}", format_args!( "thread '{}' panicked at {location}: {msg}", thread::current().name().unwrap_or("") ) .red() + .bold() ); eprintln!( "{}", format_args!( - "This is a bug in luby. Please kindly report this at {}.", + "luby should never panic. Please kindly report this bug at {}.", env!("CARGO_PKG_REPOSITORY") ) .yellow() + .bold() ); } @@ -110,17 +114,17 @@ impl Args { fn exit_err(code: ExitCode) -> impl FnOnce(E) -> T { move |err| { - eprintln!("{}", err.red()); + eprintln!("{}", err.red().bold()); code.exit() } } -fn main() -> Result<(), ExitCode> { +fn main() { panic::set_hook(Box::new(panic_cb)); let args = Args::parse(); if args.version { - return Ok(print_version()); + return print_version(); } init_logger(&args); @@ -232,13 +236,13 @@ fn parse_jitlib_cmd(s: &str) -> Option<(&str, &str)> { } } -async fn main_async(args: Args, state: &mut luajit::State) -> Result<(), ExitCode> { +async fn main_async(args: Args, state: &mut luajit::State) { for ref path in args.path { let mut s = state.guard(); let chunk = match std::fs::read(path) { Ok(chunk) => chunk, Err(err) => { - eprintln!("{}", format_args!("{path}: {err}").red()); + eprintln!("{}", format_args!("{path}: {err}").red().bold()); ExitCode::NoInput.exit(); } }; @@ -248,13 +252,11 @@ async fn main_async(args: Args, state: &mut luajit::State) -> Result<(), ExitCod if let Err(err) = s.call_async(0, 0).await { match err.trace() { - Some(trace) => eprintln!("{}\n{trace}", err.red()), // runtime error - None => eprintln!("{}", err.red()), + Some(trace) => eprintln!("{}\n{trace}", err.red().bold()), + None => eprintln!("{}", err.red().bold()), } - ExitCode::DataErr.exit(); + process::exit(1); } } - - Ok(()) }