Skip to content

Commit

Permalink
Sending error message to STDERR. Improving error messages to be more …
Browse files Browse the repository at this point in the history
…helpful.
  • Loading branch information
Murray Christopherson committed Jan 21, 2016
1 parent 2d3ca16 commit 2048eb7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
30 changes: 29 additions & 1 deletion ID3SQL/ID3SQL/CommandLineOptions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Text;
using System.Collections.Generic;

using CommandLine;
using CommandLine.Text;
Expand Down Expand Up @@ -43,7 +45,33 @@ public class CommandLineOptions
[HelpOption]
public string GetUsage()
{
return HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
StringBuilder sb = new StringBuilder();
sb.AppendLine(HelpText.AutoBuild(this, (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current)));

sb.AppendLine();
sb.AppendLine("Available Getters");
sb.AppendLine();

IEnumerable<string> getterPropertyNames = TagFunctionManager.AllGetFunctionPropertyNames();
foreach(string getterPropertyName in getterPropertyNames)
{
sb.AppendLine(getterPropertyName);
}

sb.AppendLine();
sb.AppendLine("Available Setters");
sb.AppendLine();

IEnumerable<string> setterPropertyNames = TagFunctionManager.AllSetFunctionPropertyNames();
foreach(string setterPropertyName in setterPropertyNames)
{
sb.AppendLine(setterPropertyName);
}

sb.AppendLine();
sb.AppendLine("Getter and Setter Property names are case-sensitive");

return sb.ToString();
}
}
}
15 changes: 10 additions & 5 deletions ID3SQL/ID3SQL/ExecutionPlan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ private static void PrintParseTree(ParseTreeNode parseTreeNode, int level)
{
for(int i = 0; i < level; i++)
{
Console.Write('\t');
Console.Error.Write('\t');
}
Console.WriteLine(parseTreeNode);

Console.Error.WriteLine(parseTreeNode);

foreach(ParseTreeNode childNode in parseTreeNode.ChildNodes)
{
Expand Down Expand Up @@ -232,6 +233,10 @@ private static Func<File, string, ExecutionPlanOptions, object> ToExpressionFunc
{
string propertyName = node.Token.Text;
Func<File, string, object> getFn = TagFunctionManager.GetFunction(propertyName);
if(getFn == null)
{
throw new ID3SQLException(string.Format("Unknown property name in expression: {0}", propertyName));
}
expressionFn = (file, filePath, executionPlanOptions) =>
{
return getFn(file, filePath);
Expand Down Expand Up @@ -694,7 +699,7 @@ private static Action<File, string, ExecutionPlanOptions> ToSelectAction(ParseTr
}
else
{
throw new ID3SQLException("Unknown property name in select list");
throw new ID3SQLException(string.Format("Unknown property name in select list: {0}", propertyName));
}
}
action = (file, filePath, executionPlanOptions) =>
Expand Down Expand Up @@ -789,11 +794,11 @@ private static Action<File, string, ExecutionPlanOptions> ToUpdateAction(ParseTr
}
catch(Exception ex)
{
Console.WriteLine(string.Format("Failed to write {0}", filePath));
Console.Error.WriteLine(string.Format("Failed to write {0}", filePath));

if(executionPlanOptions.Verbose)
{
Console.WriteLine(ex);
Console.Error.WriteLine(ex);
}
}

Expand Down
12 changes: 6 additions & 6 deletions ID3SQL/ID3SQL/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static void Main(string[] args)
}
catch(Exception ex)
{
throw new ID3SQLException(string.Format("Error building file list from startDirectory '{0}'", startDirectory), ex);
throw new ID3SQLException(string.Format("Error building file list from start directory '{0}'", startDirectory), ex);
}

ExecutionPlanOptions executionPlanOptions = new ExecutionPlanOptions()
Expand All @@ -75,23 +75,23 @@ public static void Main(string[] args)
ColumnSeparator = options.ColumnSeparator
};

executionPlan.Invoke(tagFilePaths, executionPlanOptions);
executionPlan(tagFilePaths, executionPlanOptions);
}
}
catch(ID3SQLException ex)
{
Console.WriteLine(ex.Message);
Console.Error.WriteLine(ex.Message);
if(options.Verbose)
{
Console.Write(ex);
Console.Error.Write(ex);
}
}
catch(Exception ex)
{
Console.WriteLine("Unknown error has occured");
Console.Error.WriteLine("Unknown error has occured");
if(options.Verbose)
{
Console.WriteLine(ex);
Console.Error.WriteLine(ex);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions ID3SQL/ID3SQL/TagFunctionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ public static Action<object, File, ExecutionPlanOptions> SetFunction(string prop
}
}

public static IEnumerable<string> AllSetFunctionPropertyNames(Comparison<string> comparison = null)
{
if (comparison != null)
{
List<string> keys = _SetFunctions.Keys.ToList();
keys.Sort(comparison);

return keys;
}
else
{
return _SetFunctions.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Key);
}
}

private static readonly IDictionary<string, Action<object, File, ExecutionPlanOptions>> _SetFunctions = new Dictionary<string, Action<object, File, ExecutionPlanOptions>>()
{
{
Expand Down

0 comments on commit 2048eb7

Please sign in to comment.