Re: [PATCH v2 5/7] lib: return NOTMUCH_STATUS_OPERATION_INVALIDATED where appropriate

Subject: Re: [PATCH v2 5/7] lib: return NOTMUCH_STATUS_OPERATION_INVALIDATED where appropriate

Date: Tue, 5 Aug 2025 13:55:17 +0100

To: David Bremner

Cc: notmuch@notmuchmail.org, xapian-discuss@lists.xapian.org

From: Olly Betts


On Tue, Aug 05, 2025 at 08:39:34AM -0300, David Bremner wrote:
> Olly Betts <olly@survex.com> writes:
> >
> > In C++ the natural approach is to use multiple catch clauses to handle
> > some error subclasses differently, i.e. something like:
> >
> >     try {
> >         do_something_with_xapian();
> >     } catch (const Xapian::DatabaseModifiedError&) {
> >         do_something_special();
> >     } catch (const Xapian::Error& e) {
> >         report_error(e);
> >     }
> 
> I think Anton's intent (and mine where I've tried similar tricks) is to
> avoid duplicating boilerplate catch blocks. I guess it's mainly a matter
> of taste. If we were writing the code from scratch, perhaps the
> try/catch blocks could be re-arranged to reduce code duplication.
> 
> As an alternative to the current get_type based function, one could do
> something like
> 
> static inline notmuch_private_status_t
> _notmuch_xapian_error_private (const Xapian::Error &error)
> {
>   try {
>     throw error;
>   } catch (const Xapian::DatabaseModifiedError& _e) {
>     return  NOTMUCH_PRIVATE_STATUS_OPERATION_INVALIDATED;
>   } catch (const Xapian::Error& _e) {
>     return NOTMUCH_PRIVATE_STATUS_XAPIAN_EXCEPTION;
>   }
> }
> 
> This is at least statically typed (mistyping a class name will be
> caught, while mistyping a string constant will not). On the other hand,
> I concede that using throw just to pattern match is also unfortunate.

Presumably this gets called from a catch block on the exception just
caught in which case you can rethrow the current exception with just
`throw;` (we do this in xapian-bindings to avoid a lot of duplicate
code to translate a C++ exception to something suitable in the target
language).  I don't know if that's actually better than explicitly
throwing the exception object again like you showed.

Cheers,
    Olly
_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: