Browse Source

fix: enhance BadArgument error handling with expected types

Akeit0 6 months ago
parent
commit
3f96f9bf71

+ 12 - 4
src/Lua/Exceptions.cs

@@ -228,21 +228,29 @@ public class LuaRuntimeException : Exception, ILuaTracebackBuildable
         throw new LuaRuntimeException(thread, $"bad argument #{argumentId} to '{GetCurrentFunctionName(thread)}' (value expected)");
     }
 
-    public static void BadArgument(LuaThread thread, int argumentId, LuaValueType[] expected)
+
+    public static void BadArgument(LuaThread thread, int argumentId, LuaValueType expected, LuaValueType actual)
+    {
+        throw new LuaRuntimeException(thread, $"bad argument #{argumentId} to '{GetCurrentFunctionName(thread)}' ({LuaValue.ToString(expected)} expected, got {LuaValue.ToString(actual)})");
+    }
+
+    public static void BadArgument(LuaThread thread, int argumentId, LuaValueType[] expected, LuaValueType actual)
     {
-        throw new LuaRuntimeException(thread, $"bad argument #{argumentId} to '{GetCurrentFunctionName(thread)}' ({string.Join(" or ", expected)} expected)");
+        throw new LuaRuntimeException(thread, $"bad argument #{argumentId} to '{GetCurrentFunctionName(thread)}' ({string.Join(" or ", expected.Select(LuaValue.ToString))} expected, got {LuaValue.ToString(actual)})");
     }
 
+
     public static void BadArgument(LuaThread thread, int argumentId, string expected, string actual)
     {
         throw new LuaRuntimeException(thread, $"bad argument #{argumentId} to '{GetCurrentFunctionName(thread)}' ({expected} expected, got {actual})");
     }
 
-    public static void BadArgument(LuaThread thread, int argumentId, LuaValueType expected, LuaValueType actual)
+    public static void BadArgument(LuaThread thread, int argumentId, string[] expected, string actual)
     {
-        throw new LuaRuntimeException(thread, $"bad argument #{argumentId} to '{GetCurrentFunctionName(thread)}' ({LuaValue.ToString(expected)} expected, got {LuaValue.ToString(actual)})");
+        throw new LuaRuntimeException(thread, $"bad argument #{argumentId} to '{GetCurrentFunctionName(thread)}' ({string.Join(" or ", expected)} expected, got {actual})");
     }
 
+
     public static void BadArgument(LuaThread thread, int argumentId, string message)
     {
         throw new LuaRuntimeException(thread, $"bad argument #{argumentId} to '{GetCurrentFunctionName(thread)}' ({message})");

+ 1 - 1
src/Lua/LuaFunctionExecutionContext.cs

@@ -192,7 +192,7 @@ public readonly record struct LuaFunctionExecutionContext
     {
         if (ArgumentCount <= index)
         {
-            LuaRuntimeException.BadArgument(Thread, index + 1, Thread.GetCurrentFrame().Function.Name);
+            LuaRuntimeException.BadArgument(Thread, index + 1);
         }
     }
 }

+ 3 - 3
src/Lua/Standard/BasicLibrary.cs

@@ -198,7 +198,7 @@ public sealed class BasicLibrary
             }
             else
             {
-                LuaRuntimeException.BadArgument(context.Thread, 1, "load");
+                LuaRuntimeException.BadArgument(context.Thread, 1,[LuaValueType.String,LuaValueType.Function],arg0.Type);
                 return default; // dummy
             }
         }
@@ -313,7 +313,7 @@ public sealed class BasicLibrary
         }
         else
         {
-            LuaRuntimeException.BadArgument(context.Thread, 2, [LuaValueType.String, LuaValueType.Table]);
+            LuaRuntimeException.BadArgument(context.Thread, 2, [LuaValueType.String, LuaValueType.Table], arg0.Type);
             return default;
         }
     }
@@ -363,7 +363,7 @@ public sealed class BasicLibrary
 
         if (arg1.Type is not (LuaValueType.Nil or LuaValueType.Table))
         {
-            LuaRuntimeException.BadArgument(context.Thread, 2, [LuaValueType.Nil, LuaValueType.Table]);
+            LuaRuntimeException.BadArgument(context.Thread, 2, [LuaValueType.Nil, LuaValueType.Table], arg1.Type);
         }
 
         if (arg0.Metatable != null && arg0.Metatable.TryGetValue(Metamethods.Metatable, out _))

+ 1 - 1
src/Lua/Standard/DebugLibrary.cs

@@ -278,7 +278,7 @@ public class DebugLibrary
 
         if (arg1.Type is not (LuaValueType.Nil or LuaValueType.Table))
         {
-            LuaRuntimeException.BadArgument(context.Thread, 2, [LuaValueType.Nil, LuaValueType.Table]);
+            LuaRuntimeException.BadArgument(context.Thread, 2, [LuaValueType.Nil, LuaValueType.Table], arg1.Type);
         }
 
         context.State.SetMetatable(arg0, arg1.UnsafeRead<LuaTable>());

+ 1 - 1
src/Lua/Standard/Internal/IOHelper.cs

@@ -140,7 +140,7 @@ internal static class IOHelper
                 }
                 else
                 {
-                    LuaRuntimeException.BadArgument(thread, i + 1, name);
+                    LuaRuntimeException.BadArgument(thread, i + 1,  ["string", "integer"] , format.TypeToString());
                 }
             }