Skip to content

Commit

Permalink
Add __tostring to error object
Browse files Browse the repository at this point in the history
  • Loading branch information
Srlion committed Dec 14, 2024
1 parent 36dec64 commit f0549a9
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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::<sqlx::Error>() {
Some(sqlx_e) => handle_sqlx_error_internal(l, sqlx_e),
Expand All @@ -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<i32> {
// 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();
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit f0549a9

Please sign in to comment.