Print nicer error messages

This commit is contained in:
lumi 2025-06-25 21:45:29 +10:00
parent c249549b3c
commit 91302db725
Signed by: luaneko
GPG Key ID: 406809B8763FF07A

View File

@ -1,7 +1,9 @@
use clap::Parser; use clap::Parser;
use mimalloc::MiMalloc; use mimalloc::MiMalloc;
use owo_colors::OwoColorize; 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; use sysexits::ExitCode;
#[global_allocator] #[global_allocator]
@ -20,21 +22,23 @@ fn panic_cb(panic: &panic::PanicHookInfo) {
}; };
eprint!( eprint!(
"{}:\n{trace}", "{}\n{trace}",
format_args!( format_args!(
"thread '{}' panicked at {location}: {msg}", "thread '{}' panicked at {location}: {msg}",
thread::current().name().unwrap_or("<unnamed>") thread::current().name().unwrap_or("<unnamed>")
) )
.red() .red()
.bold()
); );
eprintln!( eprintln!(
"{}", "{}",
format_args!( 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") env!("CARGO_PKG_REPOSITORY")
) )
.yellow() .yellow()
.bold()
); );
} }
@ -110,17 +114,17 @@ impl Args {
fn exit_err<T, E: Display>(code: ExitCode) -> impl FnOnce(E) -> T { fn exit_err<T, E: Display>(code: ExitCode) -> impl FnOnce(E) -> T {
move |err| { move |err| {
eprintln!("{}", err.red()); eprintln!("{}", err.red().bold());
code.exit() code.exit()
} }
} }
fn main() -> Result<(), ExitCode> { fn main() {
panic::set_hook(Box::new(panic_cb)); panic::set_hook(Box::new(panic_cb));
let args = Args::parse(); let args = Args::parse();
if args.version { if args.version {
return Ok(print_version()); return print_version();
} }
init_logger(&args); 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 { for ref path in args.path {
let mut s = state.guard(); let mut s = state.guard();
let chunk = match std::fs::read(path) { let chunk = match std::fs::read(path) {
Ok(chunk) => chunk, Ok(chunk) => chunk,
Err(err) => { Err(err) => {
eprintln!("{}", format_args!("{path}: {err}").red()); eprintln!("{}", format_args!("{path}: {err}").red().bold());
ExitCode::NoInput.exit(); 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 { if let Err(err) = s.call_async(0, 0).await {
match err.trace() { match err.trace() {
Some(trace) => eprintln!("{}\n{trace}", err.red()), // runtime error Some(trace) => eprintln!("{}\n{trace}", err.red().bold()),
None => eprintln!("{}", err.red()), None => eprintln!("{}", err.red().bold()),
} }
ExitCode::DataErr.exit(); process::exit(1);
} }
} }
Ok(())
} }