private static void HandleError(int result) { string errorMessage; GitError error = null; var errHandle = NativeMethods.giterr_last(); if (errHandle != null && !errHandle.IsInvalid) { error = errHandle.MarshalAsGitError(); } if (error == null) { error = new GitError { Category = GitErrorCategory.Unknown, Message = IntPtr.Zero }; errorMessage = "No error message has been provided by the native library"; } else { errorMessage = LaxUtf8Marshaler.FromNative(error.Message); } Func <string, GitErrorCode, GitErrorCategory, LibGit2SharpException> exceptionBuilder; if (!GitErrorsToLibGit2SharpExceptions.TryGetValue((GitErrorCode)result, out exceptionBuilder)) { exceptionBuilder = (m, r, c) => new LibGit2SharpException(m, r, c); } throw exceptionBuilder(errorMessage, (GitErrorCode)result, error.Category); }
private static void HandleError(int result) { string errorMessage; GitError error = NativeMethods.giterr_last().MarshalAsGitError(); if (error == null) { error = new GitError { Category = GitErrorCategory.Unknown, Message = IntPtr.Zero }; errorMessage = "No error message has been provided by the native library"; } else { errorMessage = Utf8Marshaler.FromNative(error.Message); } switch (result) { case (int)GitErrorCode.User: throw new UserCancelledException(errorMessage, (GitErrorCode)result, error.Category); case (int)GitErrorCode.BareRepo: throw new BareRepositoryException(errorMessage, (GitErrorCode)result, error.Category); case (int)GitErrorCode.Exists: throw new NameConflictException(errorMessage, (GitErrorCode)result, error.Category); case (int)GitErrorCode.InvalidSpecification: throw new InvalidSpecificationException(errorMessage, (GitErrorCode)result, error.Category); case (int)GitErrorCode.UnmergedEntries: throw new UnmergedIndexEntriesException(errorMessage, (GitErrorCode)result, error.Category); case (int)GitErrorCode.NonFastForward: throw new NonFastForwardException(errorMessage, (GitErrorCode)result, error.Category); case (int)GitErrorCode.MergeConflict: throw new MergeConflictException(errorMessage, (GitErrorCode)result, error.Category); default: throw new LibGit2SharpException(errorMessage, (GitErrorCode)result, error.Category); } }
/// <summary> /// Check that the result of a C call was successful /// <para> /// This usually means that the method is expected to return 0. /// In some rare cases, some methods may return negative values for errors and /// positive values carrying information. Those positive values should be interpreted /// as successful calls as well. /// </para> /// </summary> /// <param name = "result">The result to examine.</param> /// <param name = "allowPositiveResult">False to only allow success when comparing against 0, /// True when positive values are allowed as well.</param> public static void Success(int result, bool allowPositiveResult = false) { if (result == (int)GitErrorCode.GIT_OK) { return; } if (allowPositiveResult && result > (int)GitErrorCode.GIT_OK) { return; } GitError error = NativeMethods.giterr_last().MarshalAsGitError(); var errorMessage = (string)marshaler.MarshalNativeToManaged(error.Message); throw new LibGit2Exception( String.Format(CultureInfo.InvariantCulture, "An error was raised by libgit2. Class = {0} ({1}).{2}{3}", Enum.GetName(typeof(GitErrorType), error.Klass), result, Environment.NewLine, errorMessage)); }