On Saturday 25 July 2009 11:10:03 Mike Friedman wrote:
> Hi,
>
> I'm working on a module to provide some syntactic sugar for creating
> exceptions with throw/catch semantics in a Moose-ish way. The basic
> idea is:
>

Before I comment on your code, let me ask - how will your effort differ from 
these previous efforts:

* http://search.cpan.org/dist/Error/

* http://search.cpan.org/dist/Exception-Class/

* http://search.cpan.org/dist/TryCatch/

I should note that I have once used Error.pm and have adopted it and now am 
one of its maintainers. However, I no longer recommend using it because its 
syntactic sugar breaks often. Just today I found the following code in an old 
codebase of mine:

{{{{{{{{{{{{{{{{{{{{

sub run
{
    my $self = shift;

    try {
        return $self->real_run();
    }
    catch $error_class with {
        my $e = shift;
        print STDERR "Quad-Pres Error: ", $e->text(), "\n";
        return (-1);        
    }
    otherwise {
        my $e = shift;
        print STDERR "$e\n"; 
        return (-1);
    };
}

}}}}}}}}}}}}}}}}}}}}

The problem here is that I return inside the clauses which are sub {...}'s in 
disguise and so it's not very correct.

Recently, I've started using Exception::Class and can recommend it. Less 
syntactic sugar, but it's still convenient, and it doesn't break as often.

Now for your code.

> package MyApp;
>
> use Moose;
> use MooseX::Exceptions;
>
> exception 'MyApp::Ex::Foo' => ( error => 'A foobar exploded' );
> exception 'MyApp::Ex::Bar' => ( error => 'barf', extends =>
> 'MyApp::Ex::Foo' );
>

I feel these too lines are two wordy. You may wish to have a construct that 
accepts a list of exceptions so you can omit the "exception" keywords. This 
would be similar to Exception::Class. "error" is too generic. How about "msg"?

Make sure the exceptions contain a record of the status of caller() and 
everything when thrown.

> sub do_something {
>     ...
>     throw MyApp::Ex::Foo;
>     # or
>     throw MyApp::Ex::Foo => 'custom error message';
> }
>

Again, you may wish to specify several parameters other that an error message.

> sub catcher {
>     my $self = shift;
>     eval {
>         $self->do_something;
>     }
>
>     if ( my $e = catch 'MyApp::Ex::Foo' ) {    #  exception isa
> MyApp::Ex::Foo # or
>     if ( my $e = catch ) {    # catch anything
>     ....
>     throw $e;      # rethrow exception object;
> }
>

$@ may be clobbered. I suggest Doing something like:

{{{{
my $ExceptionHandler = MooseX::Exception->handle($@);
}}}}

And then call methods on $ExceptionHandler which will have its own safe copy 
of $...@.

>
> I haven't implemented all the features that I want just yet, and I'm
> pretty new to Moose and Class::MOP. So I'd love to hear any
> suggestions from the community regarding feature ideas and ways to
> improve my code.
>
> The woefully incomplete code and POD as it currently exists can be found
> here:
>
> code: http://www.friedo.com/MooseX-Exceptions/lib/MooseX/Exceptions.pm
> POD: http://www.friedo.com/MooseX-Exceptions/lib/MooseX/Exceptions.html
>
> There aren't any real unit tests or anything yet. I hope to have
> something CPAN-worthy in a week or two, depending on how much time I
> have to work on it.
>
> Thanks for any comments.
>

I hope you found mine to be insightful.

Regards,

        Shlomi Fish


>
>
> Mike

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
What Makes Software Apps High Quality -  http://xrl.us/bkeuk

God gave us two eyes and ten fingers so we will type five times as much as we
read.

Reply via email to