Handle syntax errors in LuaInterface - c#

I am trying to integrate a Lua Scripting interface into my C# project by using LuaInterface.
It works as expected if I execute syntactically correct code, but as soon as a syntax error (or any other error as it seems) is introduced to the script an SEHException gets thrown without any information regarding the error.
A simple example to trigger the behavior: new LuaInterface.Lua().DoString("die");
That of course completely nullifies Lua's error handling mechanisms and is a show-stopper for me.
Apparently this is a known bug which is open since 2011.
Are there any workarounds, a version of LuaInterface without this bug or is there an alternative lua wrapper which correctly handles errors?

Lua errors (syntax or runtime errors) encountered by DoString should result in a LuaException being thrown. Its message will contain the error string generated by Lua. For instance, given your example:
try
{
new LuaInterface.Lua().DoString("die");
}
catch (LuaException ex)
{
Console.WriteLine(ex.Message);
}
You should get the following error:
[string "chunk"]:1: '=' expected near '<eof>'
You don't show any context for your one liner. Is it wrapped in a try-catch block?
If so, perhaps it's a bug?

Related

NLua handling C# Exceptions

I'm looking at NLua for scripting of an existing C# application, where C# functions called from lua may throw exceptions.
So far, I found this:
If the lua code does not handle an exception in a called C# function, NLua wraps and re-throws it so it can be handled at C# level.
If lua code does "catch" an exception via xpcall, I have found no way to access the exception's details (e.g -.Message)
The former allows me to get a debug.traceback(), a lua-level stackdump, but I don't get details of the exception.
The latter provides the exception, but no way to get a Lua stackdump (I get lua file and line in .Source, but that isn't enough).
The question is:
Can I get details of a C# exception in a NLua "xpcall" error handler (? At least the .Message field, even better the actual exception type.
Alternatively, can I handle a C# exception in NLua (with the intention of creating a lua stackdump) and re-throw it?
Or can I get a lua stackdump somehow from the "lua state" when e.g.luaState.DoFile("myScript.lua") causes an exception?
Any help would be appreciated. NLua seems near-perfect for what I have in mind, if I could only sort out the exception handling.
You need to use pcall from your script. The NLua will wrap the exception and return on second value returned by pcall.
error, exception = pcall (someFunction)
if (not error) then
print(exception.Message)
end
Example on GitHub.

SSIS: Refections invocation error--Is there any way to get more information?

Techies--
I have a 2012 SSIS package that executes successfully in my local environment. Unfortunately, it does not execute successfully on a new environment. I see an incomplete looking message: "[package name]: Error: Exception has been thrown by the target of invocation." The last time I saw such a generic error of this nature, one of the DLLs was missing for a C# task. Once the library was provided, the failure disappeared. Something similar may be happening, but this message is so broad, I can't tell. Is there a way to trap more of this message?
For anyone following this thread--
In this case, the service invocation was due to a missing 4.5 assembly, specifically system.net.http. I changed the catch block to include the inner messaging--writing the message to an error table.

Why people use ProjectData

Recently I looked at some coding at the web. I found some people use Microsoft.VisualBasic.CompilerServices.ProjectData.ProjectData class in catch block.
catch (Exception exception1)
{
//ProjectData.SetProjectError(exception1);
Console.WriteLine(exception1.ToString());
Console.WriteLine();
Console.WriteLine(sSQL);
//ProjectData.ClearProjectError();
}
I searched it on msdn that mentioned that this API supports the .NET Framework infrastructure and is not intended to be used directly from your code.
I am curious what reason people use it. Would you explain it to me?
My experience has been that this type of code use is found in c#/VB.NET projects that have been converted from VB6. When developing new c#/VB.NET solutions/projects, this practice should not be used.
Note: This technique can safely be replaced with proper exception handling that you would be used to seeing in other .NET solutions/projects.
This code is either emitted by a code conversion tool that converted VB code to C#, or resulted from decompiling an assembly that was originally created using VB.
I'm porting a VB project to Mono, and found out that the VB compiler injects these calls ProjectData.SetProjectError(exception) and ProjectData.ClearProjectError() in any catch block, and try to find a way to prevent the compiler from doing so because Mono doesn't implement the ProjectData module. And found your question while doing my research!
so this is a result of old legacy vb6 for those interested. when vb6 started out there was an err object which is still around but has moved to the projectdata object in vb. if anybody like me remembers vb6 ( this is back when dinosaurs roamed the earth), there was a handy little call on error resume next. this is if you did not like those pesky little exceptions. most vb6 programs used it copiously and voila you had no exceptions because you ignored any. so here comes the explanation.
catch (Exception exception1) // catch any exceptions that just happened
{
ProjectData.SetProjectError(exception1); // set the information
//in the err object if anyone actually wants to check
ProjectData.ClearProjectError(); //clear the err object
}
as you can see this is completely ignoring any exceptions and in true vb6 fashion, your code just explodes without any explanation. needless to say if anyone writes code like this or uses vb in this way, I will find you and figure out a way to get you incarcerated.

How to access VBScript's Err object in C# code

I see there is a problem with the error handling in VBScript, So I would like to fix this using c# code (COM). Here, I need to access Err object of VBScript in c#.net code. I did add the reference namespace VBScript_Global to the project, but VBScript_Global.ErrObj is an abstract class, so could not access it. Is there a way that c# code can watch the Err.Number.
Let me put it more clear,
In VB6, we have On Error GoTo in which if there is a runtime error it automatically drops into error handling code and we can report the error there and do appropriate action.
The same is true with .net and Java (it may be try/catch there).
Where as in VBScript, we have On Error Resume Next (it does not support GoTo ).
On Error Resume Next just moves the controller to the next statement and continue with the execution. Where, we need to explicitly check whether Err.number <> 0 after each statement in the code, which is not feasible to implement.
So I want a generic approach in VBScript, if there is a runtime error, then I should be able to report it somehow.
I thought of writing a dll in c#.net which always checks for the Err.Number of VBScript and report when there is an error occurred. Is this possible? Or my approach is wrong?
Any other alternative suggestions/ideas to handle this situation is appreciated. Thanks.
If the dll cannot do what am looking for, is there a way I can always check whether Err.number of VBScript is NOT 0 out side the VBScript?
VBScript executes in a separate environment. You cannot access an intrinsic VBScript object outside of VBScript, or more appropriately, Windows Script Host.

Handling fatal exceptions in C#?

How can I execute some code when my program experiences a fatal error and crashes? For example, something goes wrong and the box pops up that say "TestApp.exe has encountered an error and needs to close." and then I want to write to a file with an error code and say a report of the last few things that were entered into the program. How would I do this in C#??
Depending on the type of application you are writing there are different ways. You may take a look at the following article to see how this could be achieved in a WinForms application.
Generally it's a bad idea to write handlers for Exception, FatalException and two more types, you should be more specific in try.. catch clause. Try explore the Application class, especially its SetUnhandledExceptionMode. You can find a nice example on how to use it here.
It depends on what you are asking here.
Regular exceptions you can get through try/catch.
However if you are running any external dll that may for example get Corrupted state exceptions (CSEs) you need to enable legacyCorruptedStateExceptionsPolicy in your app.config (but dont forget that there is a reason for Microsoft to remove this as default as it was before. You should try to fix this kind of errors not only catch them and forget them)
http://dotnetslackers.com/articles/net/All-about-Corrupted-State-Exceptions-in-NET4.aspx
For logging errors to the file you may use log4net
Any way it should look like:
try
{
// Something potentially dangerous
}
catch (Exception Ex)
{
Logger.Append(Ex.Message);
}

Categories

Resources