Make it easier to emit formatted lua errors

This commit is contained in:
Filip Tibell 2023-08-19 21:59:05 -05:00
parent c484ae73d6
commit a91e24eb01
4 changed files with 24 additions and 10 deletions

View file

@ -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 hyper_tungstenite::{is_upgrade_request as is_ws_upgrade_request, upgrade as ws_upgrade};
use tokio::{sync::oneshot, task}; use tokio::{sync::oneshot, task};
use crate::{ use crate::lune::{
lune::{scheduler::Scheduler, util::TableBuilder}, scheduler::Scheduler,
LuneError, util::{traits::LuaEmitErrorExt, TableBuilder},
}; };
use super::{NetServeResponse, NetWebSocket}; use super::{NetServeResponse, NetWebSocket};
@ -116,15 +116,14 @@ impl Service<Request<Body>> for NetServiceInner {
.with_value("body", lua.create_string(&bytes)?)? .with_value("body", lua.create_string(&bytes)?)?
.build_readonly()?; .build_readonly()?;
let response: LuaResult<NetServeResponse> = handler.call(request); let response: LuaResult<NetServeResponse> = handler.call(request);
// Send below errors to task scheduler so that they can emit properly // Return successful response, or emit any error using pretty formatting
let lua_error = match response { lua.emit_error(match response {
Ok(r) => match r.into_response() { Ok(r) => match r.into_response() {
Ok(res) => return Ok(res), Ok(res) => return Ok(res),
Err(err) => err, Err(err) => err,
}, },
Err(err) => err, Err(err) => err,
}; });
eprintln!("{}", LuneError::from(lua_error));
Ok(Response::builder() Ok(Response::builder()
.status(500) .status(500)
.body(Body::from("Internal Server Error")) .body(Body::from("Internal Server Error"))

View file

@ -5,7 +5,7 @@ use mlua::prelude::*;
use tokio::task::LocalSet; use tokio::task::LocalSet;
use crate::LuneError; use crate::lune::util::traits::LuaEmitErrorExt;
use super::Scheduler; use super::Scheduler;
@ -53,8 +53,7 @@ where
// a non-zero exit code, and print it out to stderr // a non-zero exit code, and print it out to stderr
if let Err(err) = &res { if let Err(err) = &res {
self.state.increment_error_count(); self.state.increment_error_count();
// NOTE: LuneError will pretty-format this error self.lua.emit_error(err.clone());
eprintln!("{}", LuneError::from(err));
} }
// Send results of resuming this thread to any listeners // Send results of resuming this thread to any listeners

View file

@ -1,5 +1,6 @@
mod table_builder; mod table_builder;
pub mod formatting; pub mod formatting;
pub mod traits;
pub use table_builder::TableBuilder; pub use table_builder::TableBuilder;

15
src/lune/util/traits.rs Normal file
View file

@ -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));
}
}