4
4
using System . IO ;
5
5
using System . Linq ;
6
6
using System . Runtime . InteropServices ;
7
+ using System . Text ;
7
8
using System . Threading ;
8
9
using LibGit2Sharp . Core . Handles ;
9
10
using LibGit2Sharp . Handlers ;
@@ -23,7 +24,7 @@ public static void giterr_set_str(GitErrorCategory error_class, Exception except
23
24
}
24
25
else
25
26
{
26
- NativeMethods . giterr_set_str ( error_class , exception . Message ) ;
27
+ NativeMethods . giterr_set_str ( error_class , ErrorMessageFromException ( exception ) ) ;
27
28
}
28
29
}
29
30
@@ -32,6 +33,63 @@ public static void giterr_set_str(GitErrorCategory error_class, String errorStri
32
33
NativeMethods . giterr_set_str ( error_class , errorString ) ;
33
34
}
34
35
36
+ /// <summary>
37
+ /// This method will take an exception and try to generate an error message
38
+ /// that captures the important messages of the error.
39
+ /// The formatting is a bit subjective.
40
+ /// </summary>
41
+ /// <param name="ex"></param>
42
+ /// <returns></returns>
43
+ public static string ErrorMessageFromException ( Exception ex )
44
+ {
45
+ StringBuilder sb = new StringBuilder ( ) ;
46
+ BuildErrorMessageFromException ( sb , 0 , ex ) ;
47
+ return sb . ToString ( ) ;
48
+ }
49
+
50
+ private static void BuildErrorMessageFromException ( StringBuilder sb , int level , Exception ex )
51
+ {
52
+ string indent = new string ( ' ' , level * 4 ) ;
53
+ sb . AppendFormat ( "{0}{1}" , indent , ex . Message ) ;
54
+
55
+ if ( ex is AggregateException )
56
+ {
57
+ AggregateException aggregateException = ( ( AggregateException ) ex ) . Flatten ( ) ;
58
+
59
+ if ( aggregateException . InnerExceptions . Count == 1 )
60
+ {
61
+ sb . AppendLine ( ) ;
62
+ sb . AppendLine ( ) ;
63
+
64
+ sb . AppendFormat ( "{0}Contained exception:{1}" , indent , Environment . NewLine ) ;
65
+ BuildErrorMessageFromException ( sb , level + 1 , aggregateException . InnerException ) ;
66
+ }
67
+ else
68
+ {
69
+ sb . AppendLine ( ) ;
70
+ sb . AppendLine ( ) ;
71
+
72
+ sb . AppendFormat ( "{0}Contained exceptions:{1}" , indent , Environment . NewLine ) ;
73
+ for ( int i = 0 ; i < aggregateException . InnerExceptions . Count ; i ++ )
74
+ {
75
+ if ( i != 0 )
76
+ {
77
+ sb . AppendLine ( ) ;
78
+ }
79
+
80
+ BuildErrorMessageFromException ( sb , level + 1 , aggregateException . InnerExceptions [ i ] ) ;
81
+ }
82
+ }
83
+ }
84
+ else if ( ex . InnerException != null )
85
+ {
86
+ sb . AppendLine ( ) ;
87
+ sb . AppendLine ( ) ;
88
+ sb . AppendFormat ( "{0}Inner exception:{1}" , indent , Environment . NewLine ) ;
89
+ BuildErrorMessageFromException ( sb , level + 1 , ex . InnerException ) ;
90
+ }
91
+ }
92
+
35
93
#endregion
36
94
37
95
#region git_blame_
0 commit comments