You could now, for example, define a
GError domain for text parser errors which includes context information about a parsing failure, such as the current line and character position. Or attach the filename of a file which was being read, to the
GError informing of a read failure. Define an extended error domain using
G_DEFINE_EXTENDED_ERROR(). The extended information is stored in a ‘private’ struct provided by you, similarly to how it’s implemented for
There are code examples on how to use the new APIs in the GLib documentation, so I won’t reproduce them here.
An important limitation to note is that existing
GError domains which have ever been part of a stable public API cannot be extended retroactively unless you are breaking ABI. That’s because extending a
GError domain increases the size of the allocated
GError instances for that domain, and it’s possible that users of your API will have stack-allocated
GErrors in the past.
Please don’t stack-allocate
GErrors, as it makes future extensions of the API impossible, and doesn’t buy you notable extra performance, as
GErrors should not be used on fast paths. By their very nature, they’re for failure reporting.