Skip to content

Commit

Permalink
Added Try/Catch/Finally/End Try Commands saucepleez#138
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepleez committed Jul 31, 2019
1 parent 6679cc8 commit f6dce5e
Show file tree
Hide file tree
Showing 14 changed files with 423 additions and 8 deletions.
41 changes: 41 additions & 0 deletions taskt/Core/Automation/Commands/CatchExceptionCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using taskt.UI.CustomControls;
using taskt.UI.Forms;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("Error Handling Commands")]
[Attributes.ClassAttributes.Description("This command allows you to define actions that should occur after encountering an error.")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to define how your script should behave when an error is encountered.")]
[Attributes.ClassAttributes.ImplementationDescription("")]
public class CatchExceptionCommand : ScriptCommand
{
public string ErrorMessage { get; set; }
public string StackTrace { get; set; }

public CatchExceptionCommand()
{
this.CommandName = "CatchExceptionCommand";
this.SelectionName = "Catch Exception";
this.CommandEnabled = true;
this.CustomRendering = true;
}
public override void RunCommand(object sender)
{
//no execution required, used as a marker by the Automation Engine
}
public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_Comment", this, editor));
return RenderedControls;
}
public override string GetDisplayValue()
{
return base.GetDisplayValue();
}
}
}
39 changes: 39 additions & 0 deletions taskt/Core/Automation/Commands/EndTryCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using taskt.UI.CustomControls;
using taskt.UI.Forms;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("Error Handling Commands")]
[Attributes.ClassAttributes.Description("This command specifies the end of a try/catch block.")]
[Attributes.ClassAttributes.UsesDescription("Use this command to enclose your try/catch block.")]
[Attributes.ClassAttributes.ImplementationDescription("")]
public class EndTryCommand : ScriptCommand
{
public EndTryCommand()
{
this.CommandName = "EndTryCommand";
this.SelectionName = "End Try";
this.CommandEnabled = true;
this.CustomRendering = true;
}
public override void RunCommand(object sender)
{
//no execution required, used as a marker by the Automation Engine
}
public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_Comment", this, editor));
return RenderedControls;
}
public override string GetDisplayValue()
{
return base.GetDisplayValue();
}
}
}
40 changes: 40 additions & 0 deletions taskt/Core/Automation/Commands/FinallyCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using taskt.Core.Automation.Engine;
using taskt.UI.CustomControls;
using taskt.UI.Forms;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("Error Handling Commands")]
[Attributes.ClassAttributes.Description("This command specifies execution that should occur whether or not an error occured")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to always execute a specific command before leaving the try/catch block")]
[Attributes.ClassAttributes.ImplementationDescription("")]
public class FinallyCommand : ScriptCommand
{
public FinallyCommand()
{
this.CommandName = "FinallyCommand";
this.SelectionName = "Finally";
this.CommandEnabled = true;
this.CustomRendering = true;
}
public override void RunCommand(object sender, Script.ScriptAction parentCommand)
{
//no execution required, used as a marker by the Automation Engine
}
public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_Comment", this, editor));
return RenderedControls;
}
public override string GetDisplayValue()
{
return base.GetDisplayValue();
}
}
}
4 changes: 4 additions & 0 deletions taskt/Core/Automation/Commands/ScriptCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ namespace taskt.Core.Automation.Commands
[XmlInclude(typeof(DatabaseDefineConnectionCommand))]
[XmlInclude(typeof(DatabaseExecuteQueryCommand))]
[XmlInclude(typeof(ParseDatasetRowCommand))]
[XmlInclude(typeof(TryCommand))]
[XmlInclude(typeof(CatchExceptionCommand))]
[XmlInclude(typeof(FinallyCommand))]
[XmlInclude(typeof(EndTryCommand))]
public abstract class ScriptCommand
{
[XmlAttribute]
Expand Down
90 changes: 90 additions & 0 deletions taskt/Core/Automation/Commands/TryCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Xml.Serialization;
using taskt.Core.Automation.Engine;
using taskt.UI.CustomControls;
using taskt.UI.Forms;

namespace taskt.Core.Automation.Commands
{
[Serializable]
[Attributes.ClassAttributes.Group("Error Handling Commands")]
[Attributes.ClassAttributes.Description("This command allows embedding commands and will automatically move to the 'catch' handler")]
[Attributes.ClassAttributes.UsesDescription("Use this command when you want to handle potential errors that could occur.")]
[Attributes.ClassAttributes.ImplementationDescription("")]
public class TryCommand : ScriptCommand
{
public TryCommand()
{
this.CommandName = "TryCommand";
this.SelectionName = "Try";
this.CommandEnabled = true;
this.CustomRendering = true;
}

public override void RunCommand(object sender, Script.ScriptAction parentCommand)
{
//get engine
var engine = (Core.Automation.Engine.AutomationEngineInstance)sender;

//get indexes of commands
var startIndex = 0;
var catchIndex = parentCommand.AdditionalScriptCommands.FindIndex(a => a.ScriptCommand is CatchExceptionCommand);
var finallyIndex = parentCommand.AdditionalScriptCommands.FindIndex(a => a.ScriptCommand is FinallyCommand);
var endTryIndex = parentCommand.AdditionalScriptCommands.FindIndex(a => a.ScriptCommand is EndTryCommand);

var lineNumber = 0;
for (int i = startIndex; i < catchIndex; i++)
{
if ((engine.IsCancellationPending) || (engine.CurrentLoopCancelled))
return;
try
{
Script.ScriptAction cmd = parentCommand.AdditionalScriptCommands[i];
lineNumber = cmd.ScriptCommand.LineNumber;
engine.ExecuteCommand(cmd);
}
catch (Exception ex)
{
//error occured so start processing from catch index onwards
var catchCommandItem = parentCommand.AdditionalScriptCommands[catchIndex];
var catchCommand = (CatchExceptionCommand)catchCommandItem.ScriptCommand;

catchCommand.StackTrace = ex.ToString();
catchCommand.ErrorMessage = ex.Message;
engine.AddVariable("Catch:StackTrace", catchCommand.StackTrace);
engine.AddVariable("Catch:ErrorMessage", catchCommand.ErrorMessage);

//assify = (input >= 0) ? "nonnegative" : "negative";
var endCatch = (finallyIndex != -1) ? finallyIndex : endTryIndex;

for (int j = catchIndex; j < endCatch; j++)
{
engine.ExecuteCommand(parentCommand.AdditionalScriptCommands[j]);
}

}

}
//handle finally block if exists
if (finallyIndex != -1)
{
for (int k = finallyIndex; k < endTryIndex; k++)
{
engine.ExecuteCommand(parentCommand.AdditionalScriptCommands[k]);
}
}
}
public override List<Control> Render(frmCommandEditor editor)
{
base.Render(editor);
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_Comment", this, editor));
return RenderedControls;
}
public override string GetDisplayValue()
{
return base.GetDisplayValue();
}
}
}
6 changes: 3 additions & 3 deletions taskt/Core/Automation/Engine/AutomationEngineInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public void ExecuteCommand(Core.Script.ScriptAction command)
try
{
//determine type of command
if ((parentCommand is Core.Automation.Commands.BeginNumberOfTimesLoopCommand) || (parentCommand is Core.Automation.Commands.BeginContinousLoopCommand) || (parentCommand is Core.Automation.Commands.BeginListLoopCommand) || (parentCommand is Core.Automation.Commands.BeginIfCommand) || parentCommand is Core.Automation.Commands.BeginExcelDatasetLoopCommand)
if ((parentCommand is Core.Automation.Commands.BeginNumberOfTimesLoopCommand) || (parentCommand is Core.Automation.Commands.BeginContinousLoopCommand) || (parentCommand is Core.Automation.Commands.BeginListLoopCommand) || (parentCommand is Core.Automation.Commands.BeginIfCommand) || (parentCommand is Core.Automation.Commands.BeginExcelDatasetLoopCommand) || (parentCommand is Commands.TryCommand))
{
//run the command and pass bgw/command as this command will recursively call this method for sub commands
parentCommand.RunCommand(this, command);
Expand Down Expand Up @@ -319,13 +319,13 @@ public void ExecuteCommand(Core.Script.ScriptAction command)

default:

throw new Exception(ex.ToString());
throw ex;
}
}
else
{

throw new Exception(ex.ToString());
throw ex;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions taskt/Core/Script.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static Script SerializeScript(ListView.ListViewItemCollection scriptComma
var command = (Core.Automation.Commands.ScriptCommand)commandItem.Tag;
command.LineNumber = lineNumber;

if ((command is Core.Automation.Commands.BeginNumberOfTimesLoopCommand) || (command is Core.Automation.Commands.BeginContinousLoopCommand) || (command is Core.Automation.Commands.BeginListLoopCommand) || (command is Core.Automation.Commands.BeginIfCommand) || (command is Core.Automation.Commands.BeginExcelDatasetLoopCommand))
if ((command is Core.Automation.Commands.BeginNumberOfTimesLoopCommand) || (command is Core.Automation.Commands.BeginContinousLoopCommand) || (command is Core.Automation.Commands.BeginListLoopCommand) || (command is Core.Automation.Commands.BeginIfCommand) || (command is Core.Automation.Commands.BeginExcelDatasetLoopCommand) || (command is Core.Automation.Commands.TryCommand))
{
if (subCommands.Count == 0) //if this is the first loop
{
Expand All @@ -93,7 +93,7 @@ public static Script SerializeScript(ListView.ListViewItemCollection scriptComma
subCommands.Add(nextNodeParent);
}
}
else if ((command is Core.Automation.Commands.EndLoopCommand) || (command is Core.Automation.Commands.EndIfCommand)) //if current loop scenario is ending
else if ((command is Core.Automation.Commands.EndLoopCommand) || (command is Core.Automation.Commands.EndIfCommand) || (command is Core.Automation.Commands.EndTryCommand)) //if current loop scenario is ending
{
//get reference to previous node
var parentCommand = subCommands[subCommands.Count - 1];
Expand Down
10 changes: 10 additions & 0 deletions taskt/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions taskt/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,7 @@
<data name="command_database" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\command-database.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="command_try" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\command-try.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
</root>
Binary file added taskt/Resources/command-try.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit f6dce5e

Please sign in to comment.