+
Skip to content

Improve exception handling #906

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

Merged
merged 4 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ private static string RenderCallStatement(string signatureName, GirModel.Callbac
private static string RenderThrowOnError(GirModel.Callback callback, IEnumerable<Public.ParameterToNativeData> parameters)
{
return callback.Throws || parameters.Any(x => x.IsGLibErrorParameter)
? "GLib.Error.ThrowOnError(new GLib.Internal.ErrorUnownedHandle(error));"
? @"if(error != IntPtr.Zero)
throw new GLib.GException(new GLib.Internal.ErrorUnownedHandle(error));"
: string.Empty;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Generation/Generator/Renderer/Public/Error.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public static string RenderParameter(GirModel.Callable callable)
public static string RenderThrowOnError(GirModel.Callable callable)
{
return callable.Throws
? "GLib.Error.ThrowOnError(error);" + Environment.NewLine
? @"if(!error.IsInvalid)
throw new GLib.GException(error);"
: string.Empty;
}
}
27 changes: 21 additions & 6 deletions src/Libs/GLib-2.0/Internal/PlatformStringHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ protected NullablePlatformStringHandle(bool ownsHandle) : base(IntPtr.Zero, owns
return null;

var fileName = FilenameToUtf8(handle, -1, out _, out _, out var error);
GLib.Error.ThrowOnError(error);

if (!error.IsInvalid)
throw new GException(error);

return fileName.ConvertToString();
}
}
Expand Down Expand Up @@ -55,7 +58,9 @@ public static NullablePlatformStringUnownedHandle Create(string? s)
return handle;

var fileName = FilenameFromUtf8(NonNullableUtf8StringOwnedHandle.Create(s), -1, out _, out _, out var error);
GLib.Error.ThrowOnError(error);

if (!error.IsInvalid)
throw new GException(error);

handle.SetHandle(fileName);
return handle;
Expand Down Expand Up @@ -90,7 +95,9 @@ public static NullablePlatformStringOwnedHandle Create(string? s)
return handle;

var fileName = FilenameFromUtf8(NonNullableUtf8StringOwnedHandle.Create(s), -1, out _, out _, out var error);
GLib.Error.ThrowOnError(error);

if (!error.IsInvalid)
throw new GException(error);

handle.SetHandle(fileName);
return handle;
Expand Down Expand Up @@ -124,7 +131,10 @@ public string ConvertToString()
throw new NullHandleException($"{nameof(NonNullablePlatformStringHandle)} should not have a null handle");

var fileName = Functions.FilenameToUtf8(this, -1, out _, out _, out var error);
GLib.Error.ThrowOnError(error);

if (!error.IsInvalid)
throw new GException(error);

return fileName.ConvertToString();
}
}
Expand All @@ -148,7 +158,9 @@ private NonNullablePlatformStringUnownedHandle() : base(false)
public static NonNullablePlatformStringUnownedHandle Create(string s)
{
var fileName = FilenameFromUtf8(NonNullableUtf8StringOwnedHandle.Create(s), -1, out _, out _, out var error);
GLib.Error.ThrowOnError(error);

if (!error.IsInvalid)
throw new GException(error);

var handle = new NonNullablePlatformStringUnownedHandle();
handle.SetHandle(fileName);
Expand Down Expand Up @@ -181,7 +193,10 @@ private NonNullablePlatformStringOwnedHandle() : base(true)
public static NonNullablePlatformStringOwnedHandle Create(string s)
{
var handle = Functions.FilenameFromUtf8(NonNullableUtf8StringOwnedHandle.Create(s), -1, out _, out _, out var error);
GLib.Error.ThrowOnError(error);

if (!error.IsInvalid)
throw new GException(error);

return handle;
}

Expand Down
4 changes: 3 additions & 1 deletion src/Libs/GLib-2.0/Public/Dir.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ public partial class Dir : IDisposable
public static Dir Open(string path, uint flags)
{
var handle = Internal.Dir.Open(Internal.NonNullableUtf8StringOwnedHandle.Create(path), flags, out var error);
Error.ThrowOnError(error);

if (!error.IsInvalid)
throw new GException(error);

return new Dir(handle);
}
Expand Down
10 changes: 0 additions & 10 deletions src/Libs/GLib-2.0/Public/Error.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,19 @@

namespace GLib;

public sealed partial class GException : Exception, IDisposable
public sealed class GException : Exception, IDisposable
{
#region Fields

private readonly Internal.ErrorHandle _errorHandle;

#endregion

#region Constructors

internal GException(Internal.ErrorHandle errorHandle)
public GException(Internal.ErrorHandle errorHandle)
: base(Marshal.PtrToStructure<Internal.ErrorData>(errorHandle.DangerousGetHandle()).Message)
{

_errorHandle = errorHandle;
}

#endregion

#region Methods

public void Dispose()
{
_errorHandle.Dispose();
}

#endregion
}
8 changes: 6 additions & 2 deletions src/Libs/GdkPixbuf-2.0/Public/PixbufLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ public static Pixbuf FromBytes(byte[] data)
using var bytes = Bytes.From(data);

Internal.PixbufLoader.WriteBytes(handle, bytes.Handle, out var error);
Error.ThrowOnError(error);

if (!error.IsInvalid)
throw new GException(error);

Internal.PixbufLoader.Close(handle, out error);
Error.ThrowOnError(error);

if (!error.IsInvalid)
throw new GException(error);

return new Pixbuf(Internal.PixbufLoader.GetPixbuf(handle), false);
}
Expand Down
36 changes: 18 additions & 18 deletions src/Libs/Gio-2.0/Public/DBusConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,41 @@ namespace Gio;

public partial class DBusConnection
{
#region Static methods

public static DBusConnection Get(BusType busType)
{
var handle = Internal.Functions.BusGetSync(busType, IntPtr.Zero, out var error);
Error.ThrowOnError(error);

if (!error.IsInvalid)
throw new GException(error);

return GObject.Internal.ObjectWrapper.WrapHandle<DBusConnection>(handle, true);
}

#endregion

#region Methods

public Task<Variant> CallAsync(string busName, string objectPath, string interfaceName, string methodName,
Variant? parameters = null)
{
var tcs = new TaskCompletionSource<Variant>();

void Callback(GObject.Object sourceObject, AsyncResult res, IntPtr data)
var callbackHandler = new Internal.AsyncReadyCallbackAsyncHandler((sourceObject, res, data) =>
{
// TODO: Make sure this is correct (can we assume res is a GObject?)
var ret = Internal.DBusConnection.CallFinish(sourceObject.Handle, (res as GObject.Object).Handle, out var error);
Error.ThrowOnError(error);
if (sourceObject is null)
{
tcs.SetException(new Exception("Missing source object"));
return;
}

tcs.SetResult(new Variant(ret));
}
var ret = Internal.DBusConnection.CallFinish(sourceObject.Handle, res.Handle, out var error);

var callAsyncCallbackHandler = new Internal.AsyncReadyCallbackAsyncHandler(Callback);
if (!error.IsInvalid)
tcs.SetException(new GException(error));
else
tcs.SetResult(new Variant(ret));
});

Internal.DBusConnection.Call(Handle,
GLib.Internal.NullableUtf8StringOwnedHandle.Create(busName), GLib.Internal.NonNullableUtf8StringOwnedHandle.Create(objectPath),
GLib.Internal.NonNullableUtf8StringOwnedHandle.Create(interfaceName), GLib.Internal.NonNullableUtf8StringOwnedHandle.Create(methodName),
parameters.GetSafeHandle(), GLib.Internal.VariantTypeNullHandle.Instance, DBusCallFlags.None, -1, IntPtr.Zero, callAsyncCallbackHandler.NativeCallback, IntPtr.Zero);
parameters.GetSafeHandle(), GLib.Internal.VariantTypeNullHandle.Instance, DBusCallFlags.None, -1, IntPtr.Zero, callbackHandler.NativeCallback, IntPtr.Zero);

return tcs.Task;
}
Expand All @@ -52,10 +53,9 @@ public Variant Call(string busName, string objectPath, string interfaceName, str
GLib.Internal.NonNullableUtf8StringOwnedHandle.Create(interfaceName), GLib.Internal.NonNullableUtf8StringOwnedHandle.Create(methodName),
parameterHandle, GLib.Internal.VariantTypeNullHandle.Instance, DBusCallFlags.None, 9999, IntPtr.Zero, out var error);

Error.ThrowOnError(error);
if (!error.IsInvalid)
throw new GException(error);

return new Variant(ret);
}

#endregion
}
78 changes: 42 additions & 36 deletions src/Libs/Gtk-4.0/Public/FileDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,29 @@ public partial class FileDialog
{
var tcs = new TaskCompletionSource<Gio.File?>();

void Callback(IntPtr sourceObject, IntPtr res, IntPtr userData)
var callbackHandler = new Gio.Internal.AsyncReadyCallbackAsyncHandler((sourceObject, res, data) =>
{
var fileValue = Internal.FileDialog.OpenFinish(sourceObject, res, out var error);
GLib.Error.ThrowOnError(error);

if (fileValue == IntPtr.Zero)
if (sourceObject is null)
{
tcs.SetResult(null);
tcs.SetException(new Exception("Missing source object"));
return;
}

var fileValue = Internal.FileDialog.OpenFinish(sourceObject.Handle, res.Handle, out var error);

if (!error.IsInvalid)
tcs.SetException(new GLib.GException(error));
else if (fileValue == IntPtr.Zero)
tcs.SetResult(null);
else
{
var value = new Gio.FileHelper(fileValue, true);
tcs.SetResult(value);
}
}
tcs.SetResult(new Gio.FileHelper(fileValue, true));
});

Internal.FileDialog.Open(
self: Handle,
parent: parent.Handle,
cancellable: IntPtr.Zero,
callback: Callback,
callback: callbackHandler.NativeCallback,
userData: IntPtr.Zero
);

Expand All @@ -42,27 +44,29 @@ void Callback(IntPtr sourceObject, IntPtr res, IntPtr userData)
{
var tcs = new TaskCompletionSource<Gio.File?>();

void Callback(IntPtr sourceObject, IntPtr res, IntPtr userData)
var callbackHandler = new Gio.Internal.AsyncReadyCallbackAsyncHandler((sourceObject, res, data) =>
{
var fileValue = Internal.FileDialog.SaveFinish(sourceObject, res, out var error);
GLib.Error.ThrowOnError(error);

if (fileValue == IntPtr.Zero)
if (sourceObject is null)
{
tcs.SetResult(null);
tcs.SetException(new Exception("Missing source object"));
return;
}

var fileValue = Internal.FileDialog.SaveFinish(sourceObject.Handle, res.Handle, out var error);

if (!error.IsInvalid)
tcs.SetException(new GLib.GException(error));
else if (fileValue == IntPtr.Zero)
tcs.SetResult(null);
else
{
var value = new Gio.FileHelper(fileValue, true);
tcs.SetResult(value);
}
}
tcs.SetResult(new Gio.FileHelper(fileValue, true));
});

Internal.FileDialog.Save(
self: Handle,
parent: parent.Handle,
cancellable: IntPtr.Zero,
callback: Callback,
callback: callbackHandler.NativeCallback,
userData: IntPtr.Zero
);

Expand All @@ -75,27 +79,29 @@ void Callback(IntPtr sourceObject, IntPtr res, IntPtr userData)
{
var tcs = new TaskCompletionSource<Gio.File?>();

void Callback(IntPtr sourceObject, IntPtr res, IntPtr userData)
var callbackHandler = new Gio.Internal.AsyncReadyCallbackAsyncHandler((sourceObject, res, data) =>
{
var fileValue = Internal.FileDialog.SelectFolderFinish(sourceObject, res, out var error);
GLib.Error.ThrowOnError(error);

if (fileValue == IntPtr.Zero)
if (sourceObject is null)
{
tcs.SetResult(null);
tcs.SetException(new Exception("Missing source object"));
return;
}

var fileValue = Internal.FileDialog.SelectFolderFinish(sourceObject.Handle, res.Handle, out var error);

if (!error.IsInvalid)
tcs.SetException(new GLib.GException(error));
else if (fileValue == IntPtr.Zero)
tcs.SetResult(null);
else
{
var value = new Gio.FileHelper(fileValue, true);
tcs.SetResult(value);
}
}
tcs.SetResult(new Gio.FileHelper(fileValue, true));
});

Internal.FileDialog.SelectFolder(
self: Handle,
parent: parent.Handle,
cancellable: IntPtr.Zero,
callback: Callback,
callback: callbackHandler.NativeCallback,
userData: IntPtr.Zero
);

Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载