mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 13:00:37 +00:00
Clean up LuneError type for consumers
This commit is contained in:
parent
d87d3f6cee
commit
74a41c1bf1
2 changed files with 57 additions and 20 deletions
|
@ -12,35 +12,72 @@ use crate::lune::lua::stdio::formatting::pretty_format_luau_error;
|
||||||
*/
|
*/
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct LuneError {
|
pub struct LuneError {
|
||||||
message: String,
|
error: LuaError,
|
||||||
incomplete_input: bool,
|
disable_colors: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LuneError {
|
impl LuneError {
|
||||||
pub(crate) fn from_lua_error(error: LuaError, disable_colors: bool) -> Self {
|
/**
|
||||||
Self {
|
Enables colorization of the error message when formatted using the [`Display`] trait.
|
||||||
message: pretty_format_luau_error(&error, !disable_colors),
|
|
||||||
incomplete_input: matches!(
|
Colorization is enabled by default.
|
||||||
error,
|
*/
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub fn enable_colors(mut self) -> Self {
|
||||||
|
self.disable_colors = false;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Disables colorization of the error message when formatted using the [`Display`] trait.
|
||||||
|
|
||||||
|
Colorization is enabled by default.
|
||||||
|
*/
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub fn disable_colors(mut self) -> Self {
|
||||||
|
self.disable_colors = true;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Returns `true` if the error can likely be fixed by appending more input to the source code.
|
||||||
|
|
||||||
|
See [`mlua::Error::SyntaxError`] for more information.
|
||||||
|
*/
|
||||||
|
pub fn is_incomplete_input(&self) -> bool {
|
||||||
|
matches!(
|
||||||
|
self.error,
|
||||||
LuaError::SyntaxError {
|
LuaError::SyntaxError {
|
||||||
incomplete_input: true,
|
incomplete_input: true,
|
||||||
..
|
..
|
||||||
}
|
}
|
||||||
),
|
)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LuneError {
|
impl From<LuaError> for LuneError {
|
||||||
pub fn is_incomplete_input(&self) -> bool {
|
fn from(value: LuaError) -> Self {
|
||||||
self.incomplete_input
|
Self {
|
||||||
|
error: value,
|
||||||
|
disable_colors: false,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for LuneError {
|
impl Display for LuneError {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
|
||||||
write!(f, "{}", self.message)
|
write!(
|
||||||
|
f,
|
||||||
|
"{}",
|
||||||
|
pretty_format_luau_error(&self.error, !self.disable_colors)
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Error for LuneError {}
|
impl Error for LuneError {
|
||||||
|
// TODO: Comment this out when we are ready to also re-export
|
||||||
|
// `mlua` as part of our public library interface in Lune
|
||||||
|
// fn cause(&self) -> Option<&dyn Error> {
|
||||||
|
// Some(&self.error)
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
|
@ -56,7 +56,7 @@ impl Lune {
|
||||||
) -> Result<ExitCode, LuneError> {
|
) -> Result<ExitCode, LuneError> {
|
||||||
self.run_inner(script_name, script_contents)
|
self.run_inner(script_name, script_contents)
|
||||||
.await
|
.await
|
||||||
.map_err(|err| LuneError::from_lua_error(err, false))
|
.map_err(LuneError::from)
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn run_inner(
|
async fn run_inner(
|
||||||
|
@ -87,7 +87,7 @@ impl Lune {
|
||||||
loop {
|
loop {
|
||||||
let result = sched.resume_queue().await;
|
let result = sched.resume_queue().await;
|
||||||
if let Some(err) = result.get_lua_error() {
|
if let Some(err) = result.get_lua_error() {
|
||||||
eprintln!("{}", LuneError::from_lua_error(err, false));
|
eprintln!("{}", LuneError::from(err));
|
||||||
got_error = true;
|
got_error = true;
|
||||||
}
|
}
|
||||||
if result.is_done() {
|
if result.is_done() {
|
||||||
|
|
Loading…
Reference in a new issue