Skip to content

Commit 6d724b8

Browse files
committed
added ExceptionHelpers (#61)
1 parent b7cf7d6 commit 6d724b8

6 files changed

Lines changed: 96 additions & 5 deletions

File tree

Synapse.Common/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@
3838
// by using the '*' as shown below:
3939
// [assembly: AssemblyVersion("1.0.*")]
4040
[assembly: AssemblyVersion( "0.1.0.0" )]
41-
[assembly: AssemblyFileVersion( "0.1.18021.0" )]
41+
[assembly: AssemblyFileVersion( "0.1.18023.0" )]

Synapse.Core/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,4 @@
3737
// by using the '*' as shown below:
3838
// [assembly: AssemblyVersion("1.0.*")]
3939
[assembly: AssemblyVersion( "0.1.0.0" )]
40-
[assembly: AssemblyFileVersion( "0.1.18021.0" )]
40+
[assembly: AssemblyFileVersion( "0.1.18023.0" )]

Synapse.Core/Synapse.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<Compile Include="Runtime\ActionItem.cs" />
104104
<Compile Include="Runtime\PlanSignature.cs" />
105105
<Compile Include="Utilities\AssemblyLoader.cs" />
106+
<Compile Include="Utilities\ExceptionHelpers.cs" />
106107
<Compile Include="Utilities\JsonHelpers.cs" />
107108
<Compile Include="Utilities\XmlHelpers.cs" />
108109
<Compile Include="Dal\ActionInstance.cs" />
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using YamlDotNet.Core;
5+
6+
namespace Synapse.Core.Utilities
7+
{
8+
public class ExceptionHelpers
9+
{
10+
public static string UnwindException(Exception ex)
11+
{
12+
return UnwindException( null, ex );
13+
}
14+
public static string UnwindException(string context, Exception ex, bool asSingleLine = false)
15+
{
16+
string lineEnd = asSingleLine ? "|" : @"\r\n";
17+
18+
StringBuilder msg = new StringBuilder();
19+
if( !string.IsNullOrWhiteSpace( context ) )
20+
msg.Append( $"An error occurred in: {context}{lineEnd}" );
21+
22+
msg.Append( $"{ex.Message}{lineEnd}" );
23+
24+
if( ex.InnerException != null )
25+
{
26+
if( ex.InnerException is AggregateException )
27+
{
28+
AggregateException ae = ex.InnerException as AggregateException;
29+
foreach( Exception wcx in ae.InnerExceptions )
30+
{
31+
Stack<Exception> exceptions = new Stack<Exception>();
32+
exceptions.Push( wcx );
33+
34+
while( exceptions.Count > 0 )
35+
{
36+
Exception e = exceptions.Pop();
37+
38+
if( e.InnerException != null )
39+
exceptions.Push( e.InnerException );
40+
41+
msg.Append( $"{e.Message}{lineEnd}" );
42+
}
43+
}
44+
}
45+
else
46+
{
47+
Stack<Exception> exceptions = new Stack<Exception>();
48+
exceptions.Push( ex.InnerException );
49+
50+
while( exceptions.Count > 0 )
51+
{
52+
Exception e = exceptions.Pop();
53+
54+
if( e.InnerException != null )
55+
exceptions.Push( e.InnerException );
56+
57+
msg.Append( $"{e.Message}{lineEnd}" );
58+
}
59+
}
60+
}
61+
62+
return asSingleLine ? msg.ToString().TrimEnd( '|' ) : msg.ToString();
63+
}
64+
}
65+
66+
67+
68+
69+
public static class ExceptionExtensions
70+
{
71+
public static Exception ToFriendlyYamlException(this Exception ex, string message = null, string yaml = null)
72+
{
73+
if( ex is YamlException yx )
74+
{
75+
string err = null;
76+
if( !string.IsNullOrWhiteSpace( yaml ) )
77+
err = $"\r\nText of range is: {yaml.Substring( yx.Start.Index, yx.End.Index - yx.Start.Index )}";
78+
79+
string msg = $"Error encountered with YAML between line {yx.Start.Line}/ch {yx.Start.Column} and line {yx.End.Line}/ch {yx.End.Column}, and between document indexes {yx.Start.Index} and {yx.End.Index}.{err}\r\n{message}";
80+
81+
return new Exception( message: msg, innerException: yx );
82+
}
83+
else
84+
return ex;
85+
}
86+
}
87+
}

Synapse.cli/Program.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ class Program
1616

1717
static void Main(string[] args)
1818
{
19+
Arguments a = null;
1920
try
2021
{
2122
int exitCode = 11;
2223

2324
EnsureDatabaseExists();
2425

25-
Arguments a = new Arguments( args );
26+
a = new Arguments( args );
27+
2628
if( !a.IsParsed )
2729
WriteHelpAndExit( a.Message );
2830

@@ -118,7 +120,8 @@ static void Main(string[] args)
118120
}
119121
catch( Exception ex )
120122
{
121-
WriteHelpAndExit( ex.Message );
123+
ex = ex.ToFriendlyYamlException( yaml: a.Plan );
124+
WriteHelpAndExit( ExceptionHelpers.UnwindException( ex ) );
122125
}
123126
}
124127

Synapse.cli/Properties/AssemblyInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
3535
[assembly: AssemblyVersion( "0.1.0.0" )]
36-
[assembly: AssemblyFileVersion( "0.1.18021.0" )]
36+
[assembly: AssemblyFileVersion( "0.1.18023.0" )]

0 commit comments

Comments
 (0)