Skip to content

Commit 6f6f0ae

Browse files
committed
Try harder to format error messages
Also marshals text as UTF8, as expected by libgit2.
1 parent 16aaf6c commit 6f6f0ae

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<Compile Include="RefSpecFixture.cs" />
6464
<Compile Include="EqualityFixture.cs" />
6565
<Compile Include="RevertFixture.cs" />
66+
<Compile Include="SetErrorFixture.cs" />
6667
<Compile Include="SignatureFixture.cs" />
6768
<Compile Include="FilterBranchFixture.cs" />
6869
<Compile Include="RemoveFixture.cs" />

LibGit2Sharp/Core/NativeMethods.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ static NativeMethods()
8585
[DllImport(libgit2)]
8686
internal static extern void giterr_set_str(
8787
GitErrorCategory error_class,
88-
string errorString);
88+
[MarshalAs(UnmanagedType.CustomMarshaler, MarshalCookie = UniqueId.UniqueIdentifier, MarshalTypeRef = typeof(StrictUtf8Marshaler))] string errorString);
8989

9090
[DllImport(libgit2)]
9191
internal static extern void giterr_set_oom();

LibGit2Sharp/Core/Proxy.cs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Linq;
66
using System.Runtime.InteropServices;
7+
using System.Text;
78
using System.Threading;
89
using LibGit2Sharp.Core.Handles;
910
using LibGit2Sharp.Handlers;
@@ -23,7 +24,7 @@ public static void giterr_set_str(GitErrorCategory error_class, Exception except
2324
}
2425
else
2526
{
26-
NativeMethods.giterr_set_str(error_class, exception.Message);
27+
NativeMethods.giterr_set_str(error_class, ErrorMessageFromException(exception));
2728
}
2829
}
2930

@@ -32,6 +33,63 @@ public static void giterr_set_str(GitErrorCategory error_class, String errorStri
3233
NativeMethods.giterr_set_str(error_class, errorString);
3334
}
3435

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+
3593
#endregion
3694

3795
#region git_blame_

0 commit comments

Comments
 (0)