Exceptions.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using Lua.Internal;
  2. using Lua.Runtime;
  3. namespace Lua;
  4. public class LuaException : Exception
  5. {
  6. protected LuaException(Exception innerException) : base(innerException.Message, innerException)
  7. {
  8. }
  9. public LuaException(string message) : base(message)
  10. {
  11. }
  12. protected LuaException()
  13. {
  14. }
  15. }
  16. public class LuaParseException(string message) : LuaException(message);
  17. public class LuaRuntimeException : LuaException
  18. {
  19. public LuaRuntimeException(Traceback traceback, Exception innerException) : base(innerException)
  20. {
  21. LuaTraceback = traceback;
  22. }
  23. public LuaRuntimeException(Traceback traceback, LuaValue errorObject) : base(CreateMessage(traceback, errorObject))
  24. {
  25. LuaTraceback = traceback;
  26. ErrorObject = errorObject;
  27. }
  28. public Traceback LuaTraceback { get; }
  29. public LuaValue ErrorObject { get; }
  30. public static void AttemptInvalidOperation(Traceback traceback, string op, LuaValue a, LuaValue b)
  31. {
  32. throw new LuaRuntimeException(traceback, $"attempt to {op} a '{a.Type}' with a '{b.Type}'");
  33. }
  34. public static void AttemptInvalidOperation(Traceback traceback, string op, LuaValue a)
  35. {
  36. throw new LuaRuntimeException(traceback, $"attempt to {op} a '{a.Type}' value");
  37. }
  38. public static void BadArgument(Traceback traceback, int argumentId, string functionName)
  39. {
  40. throw new LuaRuntimeException(traceback, $"bad argument #{argumentId} to '{functionName}' (value expected)");
  41. }
  42. public static void BadArgument(Traceback traceback, int argumentId, string functionName, LuaValueType[] expected)
  43. {
  44. throw new LuaRuntimeException(traceback, $"bad argument #{argumentId} to '{functionName}' ({string.Join(" or ", expected)} expected)");
  45. }
  46. public static void BadArgument(Traceback traceback, int argumentId, string functionName, string expected, string actual)
  47. {
  48. throw new LuaRuntimeException(traceback, $"bad argument #{argumentId} to '{functionName}' ({expected} expected, got {actual})");
  49. }
  50. public static void BadArgument(Traceback traceback, int argumentId, string functionName, string message)
  51. {
  52. throw new LuaRuntimeException(traceback, $"bad argument #{argumentId} to '{functionName}' ({message})");
  53. }
  54. public static void BadArgumentNumberIsNotInteger(Traceback traceback, int argumentId, string functionName)
  55. {
  56. throw new LuaRuntimeException(traceback, $"bad argument #{argumentId} to '{functionName}' (number has no integer representation)");
  57. }
  58. public static void ThrowBadArgumentIfNumberIsNotInteger(LuaThread thread, string functionName, int argumentId, double value)
  59. {
  60. if (!MathEx.IsInteger(value))
  61. {
  62. BadArgumentNumberIsNotInteger(thread.GetTraceback(), argumentId, functionName);
  63. }
  64. }
  65. static string CreateMessage(Traceback traceback, LuaValue errorObject)
  66. {
  67. var pooledList = new PooledList<char>(64);
  68. pooledList.Clear();
  69. try
  70. {
  71. pooledList.AddRange("Lua-CSharp: ");
  72. traceback.WriteLastLuaTrace(ref pooledList);
  73. pooledList.AddRange(": ");
  74. pooledList.AddRange($"{errorObject}");
  75. return pooledList.AsSpan().ToString();
  76. }
  77. finally
  78. {
  79. pooledList.Dispose();
  80. }
  81. }
  82. public override string ToString()
  83. {
  84. var pooledList = new PooledList<char>(64);
  85. pooledList.Clear();
  86. try
  87. {
  88. pooledList.AddRange(base.Message);
  89. pooledList.Add('\n');
  90. pooledList.AddRange(LuaTraceback.ToString());
  91. pooledList.Add('\n');
  92. pooledList.AddRange(StackTrace);
  93. return pooledList.AsSpan().ToString();
  94. }
  95. finally
  96. {
  97. pooledList.Dispose();
  98. }
  99. //return $"{Message} {StackTrace}";
  100. }
  101. }
  102. public class LuaAssertionException(Traceback traceback, string message) : LuaRuntimeException(traceback, message)
  103. {
  104. // public override string ToString()
  105. // {
  106. // return $"{Message}\n{StackTrace}";
  107. // }
  108. }
  109. public class LuaModuleNotFoundException(string moduleName) : LuaException($"module '{moduleName}' not found");