You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@lucy.apache.org by goran kent <go...@gmail.com> on 2011/09/15 15:28:13 UTC

[lucy-user] Return value on error (add_index() and commit())

Hi,

Please forgive the rapid-fire posts - I'm trying things out and the
following was unexpected:

$indexer = Lucy::Index::Indexer->new(...)
$index->add_index(...)
$index->commit

...do not return true/false based on success/failure, so you cannot:

$indexer = Lucy::Index::Indexer->new(...) or die "ERROR...";
$index->add_index(...) or die "ERROR...";
$index->commit or die "ERROR...";

...which would really be ideal.

I'm so used to coding with these kinds of checks in mind that not
being able to do so is weird :)

In fact, doing

$index->add_index($idxpath) or die;

... will always die, even on success.

Same with ->commit().

Anyway, not a train-smash, just mentioning it.  The only way to check
(sought of) for failure is with eval{}, which is not exactly elegant.

Cheers!
gk

Re: [lucy-user] Return value on error (add_index() and commit())

Posted by Marvin Humphrey <ma...@rectangular.com>.
On Thu, Sep 15, 2011 at 03:28:13PM +0200, goran kent wrote:
> $indexer = Lucy::Index::Indexer->new(...) or die "ERROR...";
> $index->add_index(...) or die "ERROR...";
> $index->commit or die "ERROR...";
> 
> ...which would really be ideal.
> 
> I'm so used to coding with these kinds of checks in mind that not
> being able to do so is weird :)

If Indexer#commit returns without throwing an exception, Lucy believes that it
has succeeded.

> In fact, doing
> 
> $index->add_index($idxpath) or die;
> 
> ... will always die, even on success.
> 
> Same with ->commit().
> 
> Anyway, not a train-smash, just mentioning it.  The only way to check
> (sought of) for failure is with eval{}, which is not exactly elegant.

A lot happens during Indexer#commit() in a lot of subcomponents.  The call
stack is very deep.

If, theoretically, we were to establish an unbroken chain of reporting failure
from Indexer#commit all the way down into every last nook and cranny in the
library where something could go wrong, Lucy's source code would be a giant
mess.  In reality, such a scheme is not feasible.

If, instead, we were to return false for some subset of possible error
conditions, they you'd still need eval{} anyway to trap uncaught exceptions.

The impracticality of using return values to indicate failure in a large
library is one many reasons why Lucy usually prefers exceptions.

Another major rationale for Indexer#commit's current behavior is that throwing
an exception when something goes wrong is a failsafe default, while returning
false to indicate failure is not.

HTH,

Marvin Humphrey