Hey there!
So, I got one of those questions about coding.
I have the following problem:
Say, you have two JS functions, where one JS function calls (not mutually of course) the other function and a SugarCube macro that calls one of these JS functions.
So you get a call stack of:
// Use case 1
<<mySC2Macro>> -> func1() -> func2()
now, both func1() and func2() use the this.error() to output exceptions when an error occurs. This happens because sometimes func2() is called in some of my macros as is and not through func1(). So, we get the following scenario:
// Use case 2
<<myOtherSC2Macro>> -> func2()
Now, the problem is that, if we go through use case 1 (or use case 2, it doesn't matter, since the macro context is not passed) and an error occurs in func2 and we have not passed the macro's context (this) over to func1() and then over to func2(), we get an error message that says "this.error()" is undefined. Which does not help at all to find out what went wrong, even if you try to print the error prior to return this.error() that console.error() for some reason doesn't get executed (yeah, sounds like an anomally, I haven't got the slightest clue why that happens. It maybe has something to do with how the broswer executes JS and finds the error (missing error() function in 'this') before executing the actual script.).
My question is, how do you throw exceptions properly in SugarCube 2? Do you carry the macro context (this) over all your functions or do you use something fancy like "throw new Error()"? Or maybe something completely different?
Also, does anyone know a good lexer or any kind of context aware code editor for JS? I'm sometimes driven crazy by the type failures that I do while editing my code and tend to pass the wrong variables to my arguments, causing all kinds of errors that takes me hours to debug using console.error() and console.log().
So, I got one of those questions about coding.
I have the following problem:
Say, you have two JS functions, where one JS function calls (not mutually of course) the other function and a SugarCube macro that calls one of these JS functions.
So you get a call stack of:
// Use case 1
<<mySC2Macro>> -> func1() -> func2()
now, both func1() and func2() use the this.error() to output exceptions when an error occurs. This happens because sometimes func2() is called in some of my macros as is and not through func1(). So, we get the following scenario:
// Use case 2
<<myOtherSC2Macro>> -> func2()
Now, the problem is that, if we go through use case 1 (or use case 2, it doesn't matter, since the macro context is not passed) and an error occurs in func2 and we have not passed the macro's context (this) over to func1() and then over to func2(), we get an error message that says "this.error()" is undefined. Which does not help at all to find out what went wrong, even if you try to print the error prior to return this.error() that console.error() for some reason doesn't get executed (yeah, sounds like an anomally, I haven't got the slightest clue why that happens. It maybe has something to do with how the broswer executes JS and finds the error (missing error() function in 'this') before executing the actual script.).
My question is, how do you throw exceptions properly in SugarCube 2? Do you carry the macro context (this) over all your functions or do you use something fancy like "throw new Error()"? Or maybe something completely different?
Also, does anyone know a good lexer or any kind of context aware code editor for JS? I'm sometimes driven crazy by the type failures that I do while editing my code and tend to pass the wrong variables to my arguments, causing all kinds of errors that takes me hours to debug using console.error() and console.log().