gircore / gir.core

A C# binding generator for GObject based libraries providing a C# friendly API surface
https://gircore.github.io/
MIT License
315 stars 28 forks source link

Improve Exception integration #873

Open badcel opened 1 year ago

badcel commented 1 year ago

Currently GLib has GException which is thrown if an error occurs in C:

https://github.com/gircore/gir.core/blob/712ab1740ca138a9a6dcd36975e2302b0a58f8d0/src/Libs/GLib-2.0/Public/Exception.cs#L6

GLib errors contain a domain and error code in addition to its message.

Derive from GException and create GException<T> with a property Code of type T.

The libraries define their error domains as enums which map to error domains. If an error is received we can get the domain code, map it to the corresponding enum and convert the error code into an enum value.

A user catching GException<ConvertError> can get the specific error from the Code property.

Compare the ConvertError domain: https://docs.gtk.org/glib/error.ConvertError.html

See generated ConvertError enum: https://gircore.github.io/api/GLib.ConvertError.html

Using generics could potentially be hard if the domain is unknown during compile time.

Lehonti commented 5 days ago

I am contributing to a project that uses gir.core and would benefit if exceptions exposed error codes:

https://github.com/PintaProject/Pinta/pull/1136

@cameronwhite might also appreciate getting updates on this