Skip to content

Commit ffe518c

Browse files
committed
Exceptions: refactor native exceptions to include code
Include the error code as a first-class citizen of exceptions that correspond to libgit2 errors. Previously, we would store the error code in exception data when creating the exception from a libgit2 native error. However, we may also want to _generate_ these exceptions and have them converted to a libgit2 error. (For example, a callback may want to return GIT_EUSER, and so throw a `UserCancelledException`.) Therefore, provide exceptions with knowledge of their corresponding libgit2 error code so that we can round-trip that data.
1 parent a32e0cf commit ffe518c

14 files changed

+178
-52
lines changed

LibGit2Sharp/AmbiguousSpecificationException.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using LibGit2Sharp.Core;
12
using System;
23
using System.Runtime.Serialization;
34

@@ -7,7 +8,7 @@ namespace LibGit2Sharp
78
/// The exception that is thrown when the provided specification cannot uniquely identify a reference, an object or a path.
89
/// </summary>
910
[Serializable]
10-
public class AmbiguousSpecificationException : LibGit2SharpException
11+
public class AmbiguousSpecificationException : NativeException
1112
{
1213
/// <summary>
1314
/// Initializes a new instance of the <see cref="AmbiguousSpecificationException"/> class.
@@ -50,5 +51,13 @@ public AmbiguousSpecificationException(string message, Exception innerException)
5051
protected AmbiguousSpecificationException(SerializationInfo info, StreamingContext context)
5152
: base(info, context)
5253
{ }
54+
55+
internal override GitErrorCode ErrorCode
56+
{
57+
get
58+
{
59+
return GitErrorCode.Ambiguous;
60+
}
61+
}
5362
}
5463
}

LibGit2Sharp/BareRepositoryException.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace LibGit2Sharp
99
/// working directory is performed against a bare repository.
1010
/// </summary>
1111
[Serializable]
12-
public class BareRepositoryException : LibGit2SharpException
12+
public class BareRepositoryException : NativeException
1313
{
1414
/// <summary>
1515
/// Initializes a new instance of the <see cref="LibGit2Sharp.BareRepositoryException"/> class.
@@ -52,8 +52,16 @@ protected BareRepositoryException(SerializationInfo info, StreamingContext conte
5252
: base(info, context)
5353
{ }
5454

55-
internal BareRepositoryException(string message, GitErrorCode code, GitErrorCategory category)
56-
: base(message, code, category)
55+
internal BareRepositoryException(string message, GitErrorCategory category)
56+
: base(message, category)
5757
{ }
58+
59+
internal override GitErrorCode ErrorCode
60+
{
61+
get
62+
{
63+
return GitErrorCode.BareRepo;
64+
}
65+
}
5866
}
5967
}

LibGit2Sharp/CheckoutConflictException.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace LibGit2Sharp
1010
/// in the working directory.
1111
/// </summary>
1212
[Serializable]
13-
public class CheckoutConflictException : LibGit2SharpException
13+
public class CheckoutConflictException : NativeException
1414
{
1515
/// <summary>
1616
/// Initializes a new instance of the <see cref="LibGit2Sharp.CheckoutConflictException"/> class.
@@ -53,8 +53,16 @@ protected CheckoutConflictException(SerializationInfo info, StreamingContext con
5353
: base(info, context)
5454
{ }
5555

56-
internal CheckoutConflictException(string message, GitErrorCode code, GitErrorCategory category)
57-
: base(message, code, category)
56+
internal CheckoutConflictException(string message, GitErrorCategory category)
57+
: base(message, category)
5858
{ }
59+
60+
internal override GitErrorCode ErrorCode
61+
{
62+
get
63+
{
64+
return GitErrorCode.Conflict;
65+
}
66+
}
5967
}
6068
}

LibGit2Sharp/Core/Ensure.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,20 @@ public static void ArgumentIsExpectedIntPtr(IntPtr argumentValue, IntPtr expecte
114114
}
115115
}
116116

117-
private static readonly Dictionary<GitErrorCode, Func<string, GitErrorCode, GitErrorCategory, LibGit2SharpException>>
117+
private static readonly Dictionary<GitErrorCode, Func<string, GitErrorCategory, LibGit2SharpException>>
118118
GitErrorsToLibGit2SharpExceptions =
119-
new Dictionary<GitErrorCode, Func<string, GitErrorCode, GitErrorCategory, LibGit2SharpException>>
119+
new Dictionary<GitErrorCode, Func<string, GitErrorCategory, LibGit2SharpException>>
120120
{
121-
{ GitErrorCode.User, (m, r, c) => new UserCancelledException(m, r, c) },
122-
{ GitErrorCode.BareRepo, (m, r, c) => new BareRepositoryException(m, r, c) },
123-
{ GitErrorCode.Exists, (m, r, c) => new NameConflictException(m, r, c) },
124-
{ GitErrorCode.InvalidSpecification, (m, r, c) => new InvalidSpecificationException(m, r, c) },
125-
{ GitErrorCode.UnmergedEntries, (m, r, c) => new UnmergedIndexEntriesException(m, r, c) },
126-
{ GitErrorCode.NonFastForward, (m, r, c) => new NonFastForwardException(m, r, c) },
127-
{ GitErrorCode.Conflict, (m, r, c) => new CheckoutConflictException(m, r, c) },
128-
{ GitErrorCode.LockedFile, (m, r, c) => new LockedFileException(m, r, c) },
129-
{ GitErrorCode.NotFound, (m, r, c) => new NotFoundException(m, r, c) },
130-
{ GitErrorCode.Peel, (m, r, c) => new PeelException(m, r, c) },
121+
{ GitErrorCode.User, (m, c) => new UserCancelledException(m, c) },
122+
{ GitErrorCode.BareRepo, (m, c) => new BareRepositoryException(m, c) },
123+
{ GitErrorCode.Exists, (m, c) => new NameConflictException(m, c) },
124+
{ GitErrorCode.InvalidSpecification, (m, c) => new InvalidSpecificationException(m, c) },
125+
{ GitErrorCode.UnmergedEntries, (m, c) => new UnmergedIndexEntriesException(m, c) },
126+
{ GitErrorCode.NonFastForward, (m, c) => new NonFastForwardException(m, c) },
127+
{ GitErrorCode.Conflict, (m, c) => new CheckoutConflictException(m, c) },
128+
{ GitErrorCode.LockedFile, (m, c) => new LockedFileException(m, c) },
129+
{ GitErrorCode.NotFound, (m, c) => new NotFoundException(m, c) },
130+
{ GitErrorCode.Peel, (m, c) => new PeelException(m, c) },
131131
};
132132

133133
private static unsafe void HandleError(int result)
@@ -145,13 +145,13 @@ private static unsafe void HandleError(int result)
145145
errorMessage = LaxUtf8Marshaler.FromNative(error->Message);
146146
}
147147

148-
Func<string, GitErrorCode, GitErrorCategory, LibGit2SharpException> exceptionBuilder;
148+
Func<string, GitErrorCategory, LibGit2SharpException> exceptionBuilder;
149149
if (!GitErrorsToLibGit2SharpExceptions.TryGetValue((GitErrorCode)result, out exceptionBuilder))
150150
{
151-
exceptionBuilder = (m, r, c) => new LibGit2SharpException(m, r, c);
151+
exceptionBuilder = (m, c) => new LibGit2SharpException(m, c);
152152
}
153153

154-
throw exceptionBuilder(errorMessage, (GitErrorCode)result, errorCategory);
154+
throw exceptionBuilder(errorMessage, errorCategory);
155155
}
156156

157157
/// <summary>

LibGit2Sharp/InvalidSpecificationException.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace LibGit2Sharp
1111
/// create a branch from a blob, or peeling a blob to a commit).
1212
/// </summary>
1313
[Serializable]
14-
public class InvalidSpecificationException : LibGit2SharpException
14+
public class InvalidSpecificationException : NativeException
1515
{
1616
/// <summary>
1717
/// Initializes a new instance of the <see cref="InvalidSpecificationException"/> class.
@@ -54,8 +54,16 @@ protected InvalidSpecificationException(SerializationInfo info, StreamingContext
5454
: base(info, context)
5555
{ }
5656

57-
internal InvalidSpecificationException(string message, GitErrorCode code, GitErrorCategory category)
58-
: base(message, code, category)
57+
internal InvalidSpecificationException(string message, GitErrorCategory category)
58+
: base(message, category)
5959
{ }
60+
61+
internal override GitErrorCode ErrorCode
62+
{
63+
get
64+
{
65+
return GitErrorCode.InvalidSpecification;
66+
}
67+
}
6068
}
6169
}

LibGit2Sharp/LibGit2SharpException.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,5 @@ public LibGit2SharpException(string format, params object[] args)
5252
protected LibGit2SharpException(SerializationInfo info, StreamingContext context)
5353
: base(info, context)
5454
{ }
55-
56-
internal LibGit2SharpException(string message, GitErrorCode code, GitErrorCategory category) : this(message)
57-
{
58-
Data.Add("libgit2.code", (int)code);
59-
Data.Add("libgit2.category", (int)category);
60-
}
6155
}
6256
}

LibGit2Sharp/LockedFileException.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace LibGit2Sharp
88
/// The exception that is thrown attempting to open a locked file.
99
/// </summary>
1010
[Serializable]
11-
public class LockedFileException : LibGit2SharpException
11+
public class LockedFileException : NativeException
1212
{
1313
/// <summary>
1414
/// Initializes a new instance of the <see cref="LibGit2Sharp.LockedFileException"/> class.
@@ -51,8 +51,16 @@ protected LockedFileException(SerializationInfo info, StreamingContext context)
5151
: base(info, context)
5252
{ }
5353

54-
internal LockedFileException(string message, GitErrorCode code, GitErrorCategory category)
55-
: base(message, code, category)
54+
internal LockedFileException(string message, GitErrorCategory category)
55+
: base(message, category)
5656
{ }
57+
58+
internal override GitErrorCode ErrorCode
59+
{
60+
get
61+
{
62+
return GitErrorCode.LockedFile;
63+
}
64+
}
5765
}
5866
}

LibGit2Sharp/NameConflictException.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace LibGit2Sharp
88
/// The exception that is thrown when a reference, a remote, a submodule... with the same name already exists in the repository
99
/// </summary>
1010
[Serializable]
11-
public class NameConflictException : LibGit2SharpException
11+
public class NameConflictException : NativeException
1212
{
1313
/// <summary>
1414
/// Initializes a new instance of the <see cref="NameConflictException"/> class.
@@ -51,8 +51,16 @@ protected NameConflictException(SerializationInfo info, StreamingContext context
5151
: base(info, context)
5252
{ }
5353

54-
internal NameConflictException(string message, GitErrorCode code, GitErrorCategory category)
55-
: base(message, code, category)
54+
internal NameConflictException(string message, GitErrorCategory category)
55+
: base(message, category)
5656
{ }
57+
58+
internal override GitErrorCode ErrorCode
59+
{
60+
get
61+
{
62+
return GitErrorCode.Exists;
63+
}
64+
}
5765
}
5866
}

LibGit2Sharp/NativeException.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using LibGit2Sharp.Core;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Globalization;
5+
using System.Runtime.Serialization;
6+
using System.Text;
7+
8+
namespace LibGit2Sharp
9+
{
10+
/// <summary>
11+
/// An exception thrown that corresponds to a libgit2 (native library) error.
12+
/// </summary>
13+
[Serializable]
14+
public abstract class NativeException : LibGit2SharpException
15+
{
16+
internal NativeException()
17+
{ }
18+
19+
internal NativeException(string message)
20+
: base(message)
21+
{ }
22+
23+
internal NativeException(string message, Exception innerException)
24+
: base(message, innerException)
25+
{ }
26+
27+
internal NativeException(string format, params object[] args)
28+
: base(format, args)
29+
{
30+
}
31+
32+
internal NativeException(SerializationInfo info, StreamingContext context)
33+
: base(info, context)
34+
{ }
35+
36+
internal NativeException(string message, GitErrorCategory category) : this(message)
37+
{
38+
Data.Add("libgit2.category", (int)category);
39+
}
40+
41+
internal abstract GitErrorCode ErrorCode { get; }
42+
}
43+
}

LibGit2Sharp/NonFastForwardException.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace LibGit2Sharp
99
/// against the remote without losing commits.
1010
/// </summary>
1111
[Serializable]
12-
public class NonFastForwardException : LibGit2SharpException
12+
public class NonFastForwardException : NativeException
1313
{
1414
/// <summary>
1515
/// Initializes a new instance of the <see cref="LibGit2Sharp.NonFastForwardException"/> class.
@@ -52,8 +52,16 @@ protected NonFastForwardException(SerializationInfo info, StreamingContext conte
5252
: base(info, context)
5353
{ }
5454

55-
internal NonFastForwardException(string message, GitErrorCode code, GitErrorCategory category)
56-
: base(message, code, category)
55+
internal NonFastForwardException(string message, GitErrorCategory category)
56+
: base(message, category)
5757
{ }
58+
59+
internal override GitErrorCode ErrorCode
60+
{
61+
get
62+
{
63+
return GitErrorCode.NonFastForward;
64+
}
65+
}
5866
}
5967
}

LibGit2Sharp/NotFoundException.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace LibGit2Sharp
88
/// The exception that is thrown attempting to reference a resource that does not exist.
99
/// </summary>
1010
[Serializable]
11-
public class NotFoundException : LibGit2SharpException
11+
public class NotFoundException : NativeException
1212
{
1313
/// <summary>
1414
/// Initializes a new instance of the <see cref="LibGit2Sharp.NotFoundException"/> class.
@@ -51,8 +51,16 @@ protected NotFoundException(SerializationInfo info, StreamingContext context)
5151
: base(info, context)
5252
{ }
5353

54-
internal NotFoundException(string message, GitErrorCode code, GitErrorCategory category)
55-
: base(message, code, category)
54+
internal NotFoundException(string message, GitErrorCategory category)
55+
: base(message, category)
5656
{ }
57+
58+
internal override GitErrorCode ErrorCode
59+
{
60+
get
61+
{
62+
return GitErrorCode.NotFound;
63+
}
64+
}
5765
}
5866
}

LibGit2Sharp/PeelException.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace LibGit2Sharp
99
/// target type due to the object model.
1010
/// </summary>
1111
[Serializable]
12-
public class PeelException : LibGit2SharpException
12+
public class PeelException : NativeException
1313
{
1414
/// <summary>
1515
/// Initializes a new instance of the <see cref="PeelException"/> class.
@@ -52,8 +52,16 @@ protected PeelException(SerializationInfo info, StreamingContext context)
5252
: base(info, context)
5353
{ }
5454

55-
internal PeelException(string message, GitErrorCode code, GitErrorCategory category)
56-
: base(message, code, category)
55+
internal PeelException(string message, GitErrorCategory category)
56+
: base(message, category)
5757
{ }
58+
59+
internal override GitErrorCode ErrorCode
60+
{
61+
get
62+
{
63+
return GitErrorCode.Peel;
64+
}
65+
}
5866
}
5967
}

LibGit2Sharp/UnmergedIndexEntriesException.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace LibGit2Sharp
99
/// is performed against an index with unmerged entries
1010
/// </summary>
1111
[Serializable]
12-
public class UnmergedIndexEntriesException : LibGit2SharpException
12+
public class UnmergedIndexEntriesException : NativeException
1313
{
1414
/// <summary>
1515
/// Initializes a new instance of the <see cref="UnmergedIndexEntriesException"/> class.
@@ -52,8 +52,16 @@ protected UnmergedIndexEntriesException(SerializationInfo info, StreamingContext
5252
: base(info, context)
5353
{ }
5454

55-
internal UnmergedIndexEntriesException(string message, GitErrorCode code, GitErrorCategory category)
56-
: base(message, code, category)
55+
internal UnmergedIndexEntriesException(string message, GitErrorCategory category)
56+
: base(message, category)
5757
{ }
58+
59+
internal override GitErrorCode ErrorCode
60+
{
61+
get
62+
{
63+
return GitErrorCode.UnmergedEntries;
64+
}
65+
}
5866
}
5967
}

0 commit comments

Comments
 (0)