Skip to content

Commit

Permalink
Recursively add context to Exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
benhall-7 committed Jan 29, 2019
1 parent a7af3e0 commit 241a340
Showing 1 changed file with 31 additions and 15 deletions.
46 changes: 31 additions & 15 deletions ParamXML/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static void Main(string[] args)
}
if (mode == BuildMode.Disassemble)
{
if (output == null)
if (string.IsNullOrEmpty(output))
output = Path.GetFileNameWithoutExtension(input) + ".xml";
hashToStringLabels = new Dictionary<ulong, string>();
if (!string.IsNullOrEmpty(labelName))
Expand All @@ -86,7 +86,7 @@ static void Main(string[] args)
}
else
{
if (output == null)
if (string.IsNullOrEmpty(output))
output = Path.GetFileNameWithoutExtension(input) + ".prc";
stringToHashLabels = new Dictionary<string, ulong>();
if (!string.IsNullOrEmpty(labelName))
Expand All @@ -106,9 +106,10 @@ static void Main(string[] args)
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
if (!helpPrinted)
Console.WriteLine(HelpText);
if (e.InnerException == null)
Console.WriteLine(e.StackTrace);
else
Console.WriteLine(e.InnerException.StackTrace);
}
}

Expand Down Expand Up @@ -169,17 +170,32 @@ static XmlNode ParamValue2Node(ParamValue value)

static IParam Node2Param(XmlNode node)
{
if (!Enum.IsDefined(typeof(ParamType), node.Name))
throw new FormatException(node.Name + " is not a valid param type");
ParamType type = (ParamType)Enum.Parse(typeof(ParamType), node.Name);
switch (type)
try
{
case ParamType.@struct:
return Node2ParamStruct(node);
case ParamType.array:
return Node2ParamArray(node);
default:
return Node2ParamValue(node, type);
if (!Enum.IsDefined(typeof(ParamType), node.Name))
throw new FormatException($"\"{node.Name}\" is not a valid param type");
ParamType type = (ParamType)Enum.Parse(typeof(ParamType), node.Name);
switch (type)
{
case ParamType.@struct:
return Node2ParamStruct(node);
case ParamType.array:
return Node2ParamArray(node);
default:
return Node2ParamValue(node, type);
}
}
catch (Exception e)
{
//recursively add param node context to exceptions until we exit
string trace = "Trace: " + node.Name;
foreach (XmlAttribute attr in node.Attributes)
trace += $" ({attr.Name}=\"{attr.Value}\")";
string message = trace + Environment.NewLine + e.Message;
if (e.InnerException == null)
throw new Exception(message, e);
else
throw new Exception(message, e.InnerException);
}
}

Expand Down

0 comments on commit 241a340

Please sign in to comment.