Skip to content

Commit

Permalink
Merge pull request #23 from UiPath/fix/exception_error_code
Browse files Browse the repository at this point in the history
fix confusion between exception ctor(format, params args) and ctor(message, code, category)
  • Loading branch information
andrei-balint authored Oct 4, 2023
2 parents 9e91883 + d40e9a5 commit 12d44cb
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 18 deletions.
18 changes: 18 additions & 0 deletions LibGit2Sharp.Tests/ExceptionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace LibGit2Sharp.Tests;
public class ExceptionTests
{
[Fact]
public void When_CreatingException_Given_ItHasErrorCode_Then_ErrorCodeIsSet()
{
var instance = new LibGit2SharpException("message").WithErrorCode(Core.GitErrorCode.Certificate, Core.GitErrorCategory.Ssh);
Assert.Equal(Core.GitErrorCategory.Ssh, instance.GitErrorCategory);
Assert.Equal(Core.GitErrorCode.Certificate, instance.GitErrorCode);
}
}
5 changes: 3 additions & 2 deletions LibGit2Sharp/Core/Ensure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ private static unsafe void HandleError(int result)
}

Func<string, GitErrorCategory, LibGit2SharpException> exceptionBuilder;
if (!GitErrorsToLibGit2SharpExceptions.TryGetValue((GitErrorCode)result, out exceptionBuilder))
var errorCode = (GitErrorCode)result;
if (!GitErrorsToLibGit2SharpExceptions.TryGetValue(errorCode, out exceptionBuilder))
{
exceptionBuilder = (m, c) => new LibGit2SharpException(m, c);
exceptionBuilder = (m, c) => new LibGit2SharpException(m).WithErrorCode(errorCode, errorCategory);
}

throw exceptionBuilder(errorMessage, errorCategory);
Expand Down
3 changes: 2 additions & 1 deletion LibGit2Sharp/Core/GitErrorCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public enum GitErrorCategory
Worktree,
Sha1,
Http,
Internal
Internal,
GraphTS
}
}
4 changes: 1 addition & 3 deletions LibGit2Sharp/Core/Proxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1536,9 +1536,7 @@ public static IntPtr git_odb_backend_malloc(IntPtr backend, UIntPtr len)
if (IntPtr.Zero == toReturn)
{
throw new LibGit2SharpException("Unable to allocate {0} bytes; out of memory",
len,
GitErrorCode.Error,
GitErrorCategory.NoMemory);
len).WithErrorCode( GitErrorCode.Error, GitErrorCategory.NoMemory);
}

return toReturn;
Expand Down
2 changes: 1 addition & 1 deletion LibGit2Sharp/GlobalSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ public static FilterRegistration RegisterFilter(Filter filter, int priority)
// if the filter has already been registered
if (registeredFilters.ContainsKey(filter))
{
throw new EntryExistsException("The filter has already been registered.", GitErrorCode.Exists, GitErrorCategory.Filter);
throw new EntryExistsException("The filter has already been registered.").WithErrorCode(GitErrorCode.Exists, GitErrorCategory.Filter);
}

// allocate the registration object
Expand Down
24 changes: 13 additions & 11 deletions LibGit2Sharp/LibGit2SharpException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,25 @@ protected LibGit2SharpException(SerializationInfo info, StreamingContext context
: base(info, context)
{ }

internal LibGit2SharpException(string message, GitErrorCode code, GitErrorCategory category) : this(message)
{
Data.Add("libgit2.code", (int)code);
Data.Add("libgit2.category", (int)category);

GitErrorCode = code;
GitErrorCategory = category;
}

/// <summary>
/// The error code returned by libgit2
/// </summary>
public GitErrorCode GitErrorCode { get; }
public GitErrorCode GitErrorCode { get; private set; }

/// <summary>
/// The error category
/// </summary>
public GitErrorCategory GitErrorCategory { get; }
public GitErrorCategory GitErrorCategory { get; private set; }

public LibGit2SharpException WithErrorCode(GitErrorCode code, GitErrorCategory category)
{
Data.Add("libgit2.code", (int)code);
Data.Add("libgit2.category", (int)category);

GitErrorCode = code;
GitErrorCategory = category;

return this;
}
}
}

0 comments on commit 12d44cb

Please sign in to comment.