-
Notifications
You must be signed in to change notification settings - Fork 899
Cleanup error marshaling and generating code #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
dfc5de4
5f61f26
08374d7
55ee014
5e28e01
a820e0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
namespace LibGit2Sharp.Core | ||
{ | ||
internal enum GitErrorType | ||
public enum GitErrorCategory | ||
{ | ||
Unknown = -1, | ||
NoMemory, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
namespace LibGit2Sharp.Core | ||
{ | ||
internal enum GitErrorCode | ||
public enum GitErrorCode | ||
{ | ||
Ok = 0, | ||
Error = -1, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace LibGit2Sharp.Core | ||
{ | ||
internal class GitErrorMarshaler : ICustomMarshaler | ||
{ | ||
static readonly GitErrorMarshaler staticInstance = new GitErrorMarshaler(); | ||
|
||
public void CleanUpManagedData(object managedObj) | ||
{ | ||
} | ||
|
||
public void CleanUpNativeData(IntPtr pNativeData) | ||
{ | ||
if (pNativeData != IntPtr.Zero) | ||
{ | ||
Marshal.FreeHGlobal(pNativeData); | ||
} | ||
} | ||
|
||
public int GetNativeDataSize() | ||
{ | ||
return -1; | ||
} | ||
|
||
public IntPtr MarshalManagedToNative(object managedObj) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public object MarshalNativeToManaged(IntPtr pNativeData) | ||
{ | ||
return NativeToGitError(pNativeData); | ||
} | ||
|
||
protected GitError NativeToGitError(IntPtr pNativeData) | ||
{ | ||
return (GitError)Marshal.PtrToStructure(pNativeData, typeof(GitError)); | ||
} | ||
|
||
public static ICustomMarshaler GetInstance(string cookie) | ||
{ | ||
return staticInstance; | ||
} | ||
} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
using System; | ||
using System.Runtime.Serialization; | ||
using System.Globalization; | ||
using LibGit2Sharp.Core; | ||
|
||
namespace LibGit2Sharp | ||
{ | ||
|
@@ -39,16 +40,29 @@ public LibGit2Exception(string message, Exception innerException) | |
/// <summary> | ||
/// The exception that is thrown when an error occurs during application execution. | ||
/// </summary> | ||
[Serializable] | ||
public class LibGit2SharpException : Exception | ||
{ | ||
readonly GitErrorCode code; | ||
readonly GitErrorCategory category; | ||
readonly bool isLibraryError; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref = "LibGit2SharpException" /> class. | ||
/// </summary> | ||
public LibGit2SharpException() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref = "LibGit2SharpException" /> class. | ||
/// </summary> | ||
public LibGit2SharpException(GitErrorCode code, GitErrorCategory category, string message) : base(message) | ||
{ | ||
Data["libgit2.code"] = this.code = code; | ||
Data["libgit2.class"] = this.category = category; | ||
isLibraryError = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why did you stop exposing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I'm not sure to follow you. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I think in our branch we exposed it as a property. Why would you make this internal and not give me anything else I can use to know exactly what the error is? Parsing exception messages is piss poor and likely to change. Reaching into the Data property is ugly too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The advantage of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dahlbyk Sure. As a consumer of the API, I sort of don't care what the backing storage of the properties are. As it stands, I have my own extension methods that do this, but it seems like a no-brainer to have it in the class as part of the API. Another option is to implement the serialization constructors for the exception type per FX Design Guidelines: http://msdn.microsoft.com/en-us/library/ms229064.aspx There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implementing deserialization isn't hard, it's just more effort than using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, I really don't care how it's done. :) But while we're on the topic, it does seem odd to me because All for a little less effort? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It's not about avoiding implementing the deserialization. Although that's a nice side benefit :) You can peek at the following comments for a better context There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @nulltoken ah, thanks for the context! I like the idea of exposing specific exception classes. But I didn't see such classes yet. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref = "LibGit2SharpException" /> class with a specified error message. | ||
/// </summary> | ||
|
@@ -69,13 +83,30 @@ public LibGit2SharpException(string message, Exception innerException) | |
} | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref = "LibGit2SharpException" /> class with a serialized data. | ||
/// The specific libgit2 error code. | ||
/// </summary> | ||
/// <param name = "info">The <see cref="SerializationInfo "/> that holds the serialized object data about the exception being thrown.</param> | ||
/// <param name = "context">The <see cref="StreamingContext"/> that contains contextual information about the source or destination.</param> | ||
protected LibGit2SharpException(SerializationInfo info, StreamingContext context) | ||
: base(info, context) | ||
public GitErrorCode Code | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tclem @haacked As previously discussed, I'm really not willing to make the Therefore, for now, I'd prefer to only expose In order to reduce the pain, we'll indeed have to add proper exceptions to LibGit2Sharp. Which one would you see fit? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
{ | ||
get { return Data.Contains("libgit2.code") ? (GitErrorCode)Data["libgit2.code"] : GitErrorCode.Error; } | ||
} | ||
|
||
/// <summary> | ||
/// The specific libgit2 error class. | ||
/// </summary> | ||
public GitErrorCategory Category | ||
{ | ||
get { return Data.Contains("libgit2.class") ? (GitErrorCategory)Data["libgit2.class"] : GitErrorCategory.Unknown; } | ||
} | ||
|
||
public override string ToString() | ||
{ | ||
return isLibraryError | ||
? String.Format(CultureInfo.InvariantCulture, "An error was raised by libgit2. Class = {0} ({1}).{2}{3}", | ||
category, | ||
code, | ||
Environment.NewLine, | ||
Message) | ||
: base.ToString(); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Classy!