Saturday, 10 August 2013

How can I translate exceptions from library to client side, while using pimpl idiom?

How can I translate exceptions from library to client side, while using
pimpl idiom?

I am having a project which is divided between a client side API and a set
of private library which implements the functionality. The client API
hides the detail of the library using pimpl idiom and decouples any
physical dependency from the library which implements it. i.e. the client
API header does not include any of the library header and vice
versa.(client implementation includes header from library obviously).
The implementation is trivial, such as
class resource
{
struct impl_t;
std::unique_ptr<impl_t> impl_; ...
};
However, the library throws some exceptions, and they propagates through
library boundary and reaches client call. This means either client catches
everything as std::exception assuming every library has a root hierarchy
starting from it. Otherwise, I have to expose all of the library headers,
which declares the exceptions.
Is there a way to automatically translate the library exceptions to a
corresponding exception defined in the client API (I can provide a mapping
between the client API and library exception hierarchy as an mpl::map) so
that the client and library headers are decoupled, and client can catch an
appropriate translated exception rather than std::exception?

No comments:

Post a Comment