33
44using System ;
55using System . Configuration ;
6+ using System . Text ;
67
78namespace Microsoft . Azure . WebJobs . Script
89{
9- internal static class Utility
10+ public static class Utility
1011 {
1112 public static string GetFunctionShortName ( string functionName )
1213 {
@@ -21,14 +22,47 @@ public static string GetFunctionShortName(string functionName)
2122
2223 public static string FlattenException ( Exception ex )
2324 {
24- string formattedError = ex . Message ;
25+ StringBuilder flattenedErrorsBuilder = new StringBuilder ( ) ;
26+ string lastError = null ;
2527
26- while ( ( ex = ex . InnerException ) != null )
28+ if ( ex is AggregateException )
2729 {
28- formattedError += ". " + ex . Message ;
30+ ex = ex . InnerException ;
2931 }
3032
31- return formattedError ;
33+ do
34+ {
35+ StringBuilder currentErrorBuilder = new StringBuilder ( ) ;
36+ if ( ! string . IsNullOrEmpty ( ex . Source ) )
37+ {
38+ currentErrorBuilder . AppendFormat ( "{0}: " , ex . Source ) ;
39+ }
40+
41+ currentErrorBuilder . Append ( ex . Message ) ;
42+
43+ if ( ! ex . Message . EndsWith ( "." ) )
44+ {
45+ currentErrorBuilder . Append ( "." ) ;
46+ }
47+
48+ // sometimes inner exceptions are exactly the same
49+ // so first check before duplicating
50+ string currentError = currentErrorBuilder . ToString ( ) ;
51+ if ( lastError == null ||
52+ string . Compare ( lastError . Trim ( ) , currentError . Trim ( ) ) != 0 )
53+ {
54+ if ( flattenedErrorsBuilder . Length > 0 )
55+ {
56+ flattenedErrorsBuilder . Append ( " " ) ;
57+ }
58+ flattenedErrorsBuilder . Append ( currentError ) ;
59+ }
60+
61+ lastError = currentError ;
62+ }
63+ while ( ( ex = ex . InnerException ) != null ) ;
64+
65+ return flattenedErrorsBuilder . ToString ( ) ;
3266 }
3367
3468 public static string GetAppSettingOrEnvironmentValue ( string name )
0 commit comments