From a91e24eb0198f341e9ad35b33a7d1f6ef6e71515 Mon Sep 17 00:00:00 2001 From: Filip Tibell Date: Sat, 19 Aug 2023 21:59:05 -0500 Subject: [PATCH] Make it easier to emit formatted lua errors --- src/lune/builtins/net/server.rs | 13 ++++++------- src/lune/scheduler/impl_runner.rs | 5 ++--- src/lune/util/mod.rs | 1 + src/lune/util/traits.rs | 15 +++++++++++++++ 4 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 src/lune/util/traits.rs diff --git a/src/lune/builtins/net/server.rs b/src/lune/builtins/net/server.rs index 8fb515b..5f9c364 100644 --- a/src/lune/builtins/net/server.rs +++ b/src/lune/builtins/net/server.rs @@ -12,9 +12,9 @@ use hyper::{Body, Request, Response}; use hyper_tungstenite::{is_upgrade_request as is_ws_upgrade_request, upgrade as ws_upgrade}; use tokio::{sync::oneshot, task}; -use crate::{ - lune::{scheduler::Scheduler, util::TableBuilder}, - LuneError, +use crate::lune::{ + scheduler::Scheduler, + util::{traits::LuaEmitErrorExt, TableBuilder}, }; use super::{NetServeResponse, NetWebSocket}; @@ -116,15 +116,14 @@ impl Service> for NetServiceInner { .with_value("body", lua.create_string(&bytes)?)? .build_readonly()?; let response: LuaResult = handler.call(request); - // Send below errors to task scheduler so that they can emit properly - let lua_error = match response { + // Return successful response, or emit any error using pretty formatting + lua.emit_error(match response { Ok(r) => match r.into_response() { Ok(res) => return Ok(res), Err(err) => err, }, Err(err) => err, - }; - eprintln!("{}", LuneError::from(lua_error)); + }); Ok(Response::builder() .status(500) .body(Body::from("Internal Server Error")) diff --git a/src/lune/scheduler/impl_runner.rs b/src/lune/scheduler/impl_runner.rs index f013c9a..635c4f3 100644 --- a/src/lune/scheduler/impl_runner.rs +++ b/src/lune/scheduler/impl_runner.rs @@ -5,7 +5,7 @@ use mlua::prelude::*; use tokio::task::LocalSet; -use crate::LuneError; +use crate::lune::util::traits::LuaEmitErrorExt; use super::Scheduler; @@ -53,8 +53,7 @@ where // a non-zero exit code, and print it out to stderr if let Err(err) = &res { self.state.increment_error_count(); - // NOTE: LuneError will pretty-format this error - eprintln!("{}", LuneError::from(err)); + self.lua.emit_error(err.clone()); } // Send results of resuming this thread to any listeners diff --git a/src/lune/util/mod.rs b/src/lune/util/mod.rs index 1352d14..38984af 100644 --- a/src/lune/util/mod.rs +++ b/src/lune/util/mod.rs @@ -1,5 +1,6 @@ mod table_builder; pub mod formatting; +pub mod traits; pub use table_builder::TableBuilder; diff --git a/src/lune/util/traits.rs b/src/lune/util/traits.rs new file mode 100644 index 0000000..32c514d --- /dev/null +++ b/src/lune/util/traits.rs @@ -0,0 +1,15 @@ +use mlua::prelude::*; + +use super::formatting::format_label; +use crate::LuneError; + +pub trait LuaEmitErrorExt { + fn emit_error(&self, err: LuaError); +} + +impl LuaEmitErrorExt for Lua { + fn emit_error(&self, err: LuaError) { + // NOTE: LuneError will pretty-format this error + eprintln!("{}\n{}", format_label("error"), LuneError::from(err)); + } +}