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 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("<unnamed>")
)
.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<T, E: Display>(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(())
}