NLua icon indicating copy to clipboard operation
NLua copied to clipboard

Inconsistent exception messages

Open CryoViking opened this issue 2 years ago • 0 comments

Preface

I while ago I opened an issue about not getting an expected error message back from the lua instance and closed it because it happened to work properly for some reason when I went to try again. I've now ran into a problem where most of my error messages are matching up to what I would expect, however a particular type of error seems to be breaking the normal expected output.

Working Example:

Lua m_lua = new Lua();
string lua_script = @"
    local function add(a, b)
      return a + b
    end

    local function mult(a, b)
      return a * b
    end

    local result = add(5, "something")
    print(result)
    ";
try {
  m_lua.DoString(script);
}
catch(Exception ex) {
  Console.WriteLine(ex.Message);
}

Expected (lua interpreter)

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> function add(a,b)
>> return a+b
>> end
> result = add(10, "something")
stdin:2: attempt to add a 'number' with a 'string'
stack traceback:
        [C]: in metamethod 'add'
        stdin:2: in function 'add'
        stdin:1: in main chunk
        [C]: in ?

Result (from c#)

[string "chunk"]:2: attempt to add a 'number' with a 'string'
stack traceback:
	[C]: in metamethod 'add'
	[string "chunk"]:2: in local 'add'
	[string "chunk"]:5: in main chunk

As you can see, it lines up fairly well.

Failing Example:

Now the case that I have found that does not work as expected, is when I do the following:

Lua m_lua = new Lua();
string lua_script = @"
    some_function_that_does_not_exist()
    ";
try {
  m_lua.DoString(script);
}
catch(Exception ex) {
  Console.WriteLine(ex.Message);
}

Expected (lua interpreter)

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
> some_function_that_does_not_exist()
stdin:1: attempt to call a nil value (global 'some_function_that_does_not_exist')
stack traceback:
        stdin:1: in main chunk
        [C]: in ?

Result (from c#)

[string "chunk"]:12: C stack overflow
stack traceback:
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        ...     (skipping 177 levels)
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:12: in metamethod 'index'
        [string "chunk"]:2: in main chunk

I'm wondering if there is something I am missing in this very simple example. Or if this is just one of those edge cases that I've stumbled into.

Let me know if I need to provide more information in regards to this problem. It's not a major issue at the moment and a workaround I might implement at some point if I don't get a resolution here will be to regex for the pattern and override the message.

CryoViking avatar Feb 14 '24 05:02 CryoViking