From f0549a94d7b73cea467121fed484f7e9b8fe9ca5 Mon Sep 17 00:00:00 2001 From: Srlion Date: Sat, 14 Dec 2024 11:47:53 +0200 Subject: [PATCH] Add __tostring to error object --- src/error.rs | 38 ++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 2 files changed, 39 insertions(+) diff --git a/src/error.rs b/src/error.rs index 4d75be5..0a588d0 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,6 +1,11 @@ +use anyhow::Result; use gmod::*; use sqlx::mysql::MySqlDatabaseError; +use crate::cstr_from_args; + +const META_NAME: LuaCStr = cstr_from_args!(crate::GLOBAL_TABLE_NAME, "_error"); + // call this function after creating a table fn handle_database_error(l: lua::State, db_e: &MySqlDatabaseError) -> String { if let Some(sqlstate) = db_e.code() { @@ -32,6 +37,8 @@ fn handle_sqlx_error_internal(l: lua::State, e: &sqlx::Error) -> String { pub fn handle_error(l: lua::State, e: anyhow::Error) -> String { l.create_table(0, 3); + l.get_metatable_name(META_NAME); + unsafe { l.set_metatable(-2) }; let msg = match e.downcast_ref::() { Some(sqlx_e) => handle_sqlx_error_internal(l, sqlx_e), @@ -46,6 +53,37 @@ pub fn handle_error(l: lua::State, e: anyhow::Error) -> String { pub fn handle_sqlx_error(l: lua::State, e: sqlx::Error) -> String { l.create_table(0, 3); + l.get_metatable_name(META_NAME); + unsafe { l.set_metatable(-2) }; handle_sqlx_error_internal(l, &e) } + +#[lua_function] +fn __tostring(l: lua::State) -> Result { + // retrieve the code field and the message field + l.get_field(-1, c"code"); + let code = l.check_number(-1); + l.pop(); + + l.get_field(-1, c"message"); + let message = l.check_string(-1); // copy the string + + let message = message.or_else(|_| anyhow::Ok("unknown error".into()))?; + if let Ok(code) = code { + l.push_string(&format!("({}) {}", code, message)); + } else { + l.push_string(&message); + } + + Ok(1) +} + +pub fn init(l: lua::State) { + l.new_metatable(META_NAME); + { + l.push_function(__tostring); + l.set_field(-2, c"__tostring"); + } + l.pop(); +} diff --git a/src/lib.rs b/src/lib.rs index f4b99b5..be45803 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -37,6 +37,7 @@ fn gmod13_open(l: lua::State) -> i32 { runtime::load(get_max_worker_threads(l)); conn::on_gmod_open::init(l); + error::init(l); 0 }