I know I could pcall(error) but for that I need to know how many callers the function has.
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Getting caller function
Started by Konlab, 07 November 2015 - 11:03 AMPosted 07 November 2015 - 12:03 PM
How can you get the place where your function was called? (Like in error messages)
I know I could pcall(error) but for that I need to know how many callers the function has.
I know I could pcall(error) but for that I need to know how many callers the function has.
Posted 07 November 2015 - 12:29 PM
error(msg,2) This allows you to blame the function that called the function that errored.
Posted 07 November 2015 - 12:34 PM
Thanks for replying, but I said in the original post that pcall("test",2) is just the caller. But I need the caller's caller, the caller's caller's caller etc.error(msg,2) This allows you to blame the function that called the function that errored.
Posted 07 November 2015 - 01:16 PM
You can blame whatever layer you want.
I assume the same applies to the pcall thing. I wouldn't know.
error( "hi", 2 ) --#blames the function that called this one
error( "hi", 3 ) --#blames the function that called the function that called this one
error( "hi", 4 ) --#blames the function that called the function that called the function that called this one
error( "hi", 5 ) --#...
I assume the same applies to the pcall thing. I wouldn't know.
Posted 07 November 2015 - 01:21 PM
You can blame whatever layer you want.error( "hi", 2 ) --#blames the function that called this one error( "hi", 3 ) --#blames the function that called the function that called this one error( "hi", 4 ) --#blames the function that called the function that called the function that called this one error( "hi", 5 ) --#...
I assume the same applies to the pcall thing. I wouldn't know.
Wouldn't the code be aborted before the other errors are called?
Posted 07 November 2015 - 01:35 PM
You can blame whatever layer you want.error( "hi", 2 ) --#blames the function that called this one error( "hi", 3 ) --#blames the function that called the function that called this one error( "hi", 4 ) --#blames the function that called the function that called the function that called this one error( "hi", 5 ) --#...
I assume the same applies to the pcall thing. I wouldn't know.
Wouldn't the code be aborted before the other errors are called?
That's why you have to use pcall.
Posted 07 November 2015 - 01:37 PM
–snip–
That's why you have to use pcall.
This makes sense! It gives you already two levels.
Posted 07 November 2015 - 02:42 PM
So you can do any number?
(Thanks, but what happens if I try to pcall error with a very big level, like 500 or math.huge? Or negative?)
I will try it
Thanks for helping!
(Thanks, but what happens if I try to pcall error with a very big level, like 500 or math.huge? Or negative?)
I will try it
Thanks for helping!
Edited on 07 November 2015 - 01:43 PM
Posted 07 November 2015 - 03:02 PM
If you get above the number of layers, I believe it acts like 0 does - erring without a line number or file name.
Posted 07 November 2015 - 10:05 PM
… which means you still need to know how many callers there were.
If you want every error to be able to produce a full trace then you'll need to pcall every single function, have those functions pcall every function they want to call, etc.
Lua has a debug library that (among other things) would deal with this for you, but that's not available within distributable copies of ComputerCraft. Still, it's entirely possible to set up your own Lua environment with it enabled.
If you want every error to be able to produce a full trace then you'll need to pcall every single function, have those functions pcall every function they want to call, etc.
Lua has a debug library that (among other things) would deal with this for you, but that's not available within distributable copies of ComputerCraft. Still, it's entirely possible to set up your own Lua environment with it enabled.
Posted 07 November 2015 - 10:11 PM
You can use CCTweaks for the debug library.
Posted 07 November 2015 - 10:11 PM
There is a program that does error tracing. http://www.computercraft.info/forums2/index.php?/topic/24618-stack-trace-for-cc/page__fromsearch__1