Mailing List Archive

A suggestion(or question) about exception handling.
hi all,

in my Catalyst application, I'm using my own exception handling based on
"Error.pm", and used to ask myself what if Catalyst provided the facility.

I know there are several ways in error(or exception) handling in Perl, but,
anyway my favorite is "try/catch."

today I saw Catalyst::Exception. It wasn't what I expected.

so... is there already any extension based on Error.pm in Catalyst OR any
plan??
Re: A suggestion(or question) about exception handling. [ In reply to ]
On Sat, Jul 19, 2008 at 10:16:55PM -0400, Chae Lee wrote:
> hi all,
>
> in my Catalyst application, I'm using my own exception handling based on
> "Error.pm", and used to ask myself what if Catalyst provided the facility.
>
> I know there are several ways in error(or exception) handling in Perl, but,
> anyway my favorite is "try/catch."
>
> today I saw Catalyst::Exception. It wasn't what I expected.
>
> so... is there already any extension based on Error.pm in Catalyst OR any
> plan??

Extension for what?

There's nothing to stop you using Error.pm in the Catalyst app - I don't
really see what Catalyst needs to do about it?

--
Matt S Trout Need help with your Catalyst or DBIx::Class project?
Technical Director http://www.shadowcat.co.uk/catalyst/
Shadowcat Systems Ltd. Want a managed development or deployment platform?
http://chainsawblues.vox.com/ http://www.shadowcat.co.uk/servers/

_______________________________________________
Catalyst-dev mailing list
Catalyst-dev@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst-dev
Re: A suggestion(or question) about exception handling. [ In reply to ]
On Mon, Jul 21, 2008 at 5:46 PM, Matt S Trout <dbix-class@trout.me.uk>
wrote:

> On Sat, Jul 19, 2008 at 10:16:55PM -0400, Chae Lee wrote:
> > hi all,
> >
> > in my Catalyst application, I'm using my own exception handling based on
> > "Error.pm", and used to ask myself what if Catalyst provided the
> facility.
> >
> > I know there are several ways in error(or exception) handling in Perl,
> but,
> > anyway my favorite is "try/catch."
> >
> > today I saw Catalyst::Exception. It wasn't what I expected.
> >
> > so... is there already any extension based on Error.pm in Catalyst OR any
> > plan??
>
> Extension for what?
>
> There's nothing to stop you using Error.pm in the Catalyst app - I don't
> really see what Catalyst needs to do about it?
>

I thought my email was sucked up. Good job Matt ^^

Well... using Error.pm in Catalyst doesn't depend on Catalyst, but it can
save some extra work if provided.
Re: A suggestion(or question) about exception handling. [ In reply to ]
--- On Mon, 7/21/08, Chae Lee <yuand2@gmail.com> wrote:

> From: Chae Lee <yuand2@gmail.com>
> Subject: Re: [Catalyst-dev] A suggestion(or question) about exception handling.
> To: "Development of the elegant MVC web framework" <catalyst-dev@lists.scsys.co.uk>
> Date: Monday, July 21, 2008, 7:32 PM
> On Mon, Jul 21, 2008 at 5:46 PM, Matt S Trout
> <dbix-class@trout.me.uk>
> wrote:
>
> > On Sat, Jul 19, 2008 at 10:16:55PM -0400, Chae Lee
> wrote:
> > > hi all,
> > >
> > > in my Catalyst application, I'm using my own
> exception handling based on
> > > "Error.pm", and used to ask myself what
> if Catalyst provided the
> > facility.
> > >
> > > I know there are several ways in error(or
> exception) handling in Perl,
> > but,
> > > anyway my favorite is "try/catch."
> > >
> > > today I saw Catalyst::Exception. It wasn't
> what I expected.
> > >
> > > so... is there already any extension based on
> Error.pm in Catalyst OR any
> > > plan??
> >
> > Extension for what?
> >
> > There's nothing to stop you using Error.pm in the
> Catalyst app - I don't
> > really see what Catalyst needs to do about it?
> >
>
> I thought my email was sucked up. Good job Matt ^^
>
> Well... using Error.pm in Catalyst doesn't depend on
> Catalyst, but it can
> save some extra work if
> provided.

I wonder if there are not two issues here. If you just want Error.pm for your Controllers, you can make one that contains it. You'll just have to export Error's namespace into your base controller and then all your custom controllers would inherit from that instead of Catalyst::Controller. However if what you are saying is that Catalyst should have a more fine grained error hierarchy (something like Java Spring comes to mind when I think of a web development framework with something like this) that's a totally different question. Since Catalyst is somewhat minimal, I don't really see the value of that. It doesn't really play into the way most Perl programmer's go about things. Although a CatalystX package to provide default methods for 404, 5xx style http errors might be welcomed. Like "CatalystX::Rescue" or similar? I know Apache also provides this.

JohnN
_______________________________________________
> Catalyst-dev mailing list
> Catalyst-dev@lists.scsys.co.uk
> http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst-dev




_______________________________________________
Catalyst-dev mailing list
Catalyst-dev@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst-dev
Re: A suggestion(or question) about exception handling. [ In reply to ]
On Mon, Jul 21, 2008 at 9:16 PM, John Napiorkowski <jjn1056@yahoo.com>
wrote:

>
>
>
> --- On Mon, 7/21/08, Chae Lee <yuand2@gmail.com> wrote:
>
> > From: Chae Lee <yuand2@gmail.com>
> > Subject: Re: [Catalyst-dev] A suggestion(or question) about exception
> handling.
> > To: "Development of the elegant MVC web framework" <
> catalyst-dev@lists.scsys.co.uk>
> > Date: Monday, July 21, 2008, 7:32 PM
> > On Mon, Jul 21, 2008 at 5:46 PM, Matt S Trout
> > <dbix-class@trout.me.uk>
> > wrote:
> >
> > > On Sat, Jul 19, 2008 at 10:16:55PM -0400, Chae Lee
> > wrote:
> > > > hi all,
> > > >
> > > > in my Catalyst application, I'm using my own
> > exception handling based on
> > > > "Error.pm", and used to ask myself what
> > if Catalyst provided the
> > > facility.
> > > >
> > > > I know there are several ways in error(or
> > exception) handling in Perl,
> > > but,
> > > > anyway my favorite is "try/catch."
> > > >
> > > > today I saw Catalyst::Exception. It wasn't
> > what I expected.
> > > >
> > > > so... is there already any extension based on
> > Error.pm in Catalyst OR any
> > > > plan??
> > >
> > > Extension for what?
> > >
> > > There's nothing to stop you using Error.pm in the
> > Catalyst app - I don't
> > > really see what Catalyst needs to do about it?
> > >
> >
> > I thought my email was sucked up. Good job Matt ^^
> >
> > Well... using Error.pm in Catalyst doesn't depend on
> > Catalyst, but it can
> > save some extra work if
> > provided.
>
> I wonder if there are not two issues here. If you just want Error.pm for
> your Controllers, you can make one that contains it. You'll just have to
> export Error's namespace into your base controller and then all your custom
> controllers would inherit from that instead of Catalyst::Controller.
> However if what you are saying is that Catalyst should have a more fine
> grained error hierarchy (something like Java Spring comes to mind when I
> think of a web development framework with something like this) that's a
> totally different question. Since Catalyst is somewhat minimal, I don't
> really see the value of that. It doesn't really play into the way most Perl
> programmer's go about things. Although a CatalystX package to provide
> default methods for 404, 5xx style http errors might be welcomed. Like
> "CatalystX::Rescue" or similar? I know Apache also provides this.
>
> JohnN



and the "minimal" is a buzz word, right? ( I've gotten what Matt and you
meant of :-)

Thanks a lot man
Re: A suggestion(or question) about exception handling. [ In reply to ]
On Mon, Jul 21, 2008 at 07:32:24PM -0400, Chae Lee wrote:
> Well... using Error.pm in Catalyst doesn't depend on Catalyst, but it can
> save some extra work if provided.

Try again, please. I asked -how- it would help, not just for you to say "it
would help" again.

You talk about a nonexistant extension/feature and say it would be a good
thing, but you've given no explanation of what such a feature/extension
would actually -do-.

How can we tell whether it would be useful to anybody else if the list of
what it does only exists in your head? :)

--
Matt S Trout Need help with your Catalyst or DBIx::Class project?
Technical Director http://www.shadowcat.co.uk/catalyst/
Shadowcat Systems Ltd. Want a managed development or deployment platform?
http://chainsawblues.vox.com/ http://www.shadowcat.co.uk/servers/

_______________________________________________
Catalyst-dev mailing list
Catalyst-dev@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst-dev
Re: A suggestion(or question) about exception handling. [ In reply to ]
On Jul 21, 2008, at 5:46 PM, Matt S Trout wrote:
> On Sat, Jul 19, 2008 at 10:16:55PM -0400, Chae Lee wrote:
>> hi all,
>>
>> in my Catalyst application, I'm using my own exception handling
>> based on
>> "Error.pm", and used to ask myself what if Catalyst provided the
>> facility.
>>
>> I know there are several ways in error(or exception) handling in
>> Perl, but,
>> anyway my favorite is "try/catch."
>>
>> today I saw Catalyst::Exception. It wasn't what I expected.
>>
>> so... is there already any extension based on Error.pm in Catalyst
>> OR any
>> plan??
>
> Extension for what?
>
> There's nothing to stop you using Error.pm in the Catalyst app - I
> don't
> really see what Catalyst needs to do about it?

I use Error extensively in Catalyst, but I understand the problem he
raises. Right now Catalyst traps errors itself Catalyst::execute().
What Chae Lee probably wants (I know I do) is a way to insert an Error
handler that traps those errors, or at the very least, uses try/catch
so that what gets stored in $c->error are Error objects instead of
strings. You can't fix this with a Plugin because you have no way of
knowing when in the process your plugin will be called. As a result,
I tend to end up with actions that look like:

sub send_tweet : Local {
my ($self, $c) = @_;

try {
...
} otherwise {
push(@errors, shift);
};

return $c->cleanup(\@errors);
}

And "cleanup" is a method I added (see below), that gathers up any
Catalyst errors that are hanging around, and optionally sets the
template to an error template.

Then finally in my View I gather up what's there, convert them all to
Error objects, and deal with displaying them if appropriate. It's not
the cleanest solution in the world.





=item cleanup($errors, [$error_template], [$good_template])

Call this at the end of any Component that needs to handle any
errors. It will
consolidate any Catalyst errors with those passed in, store them in
the stash,
and clear the Catalyst errors. It will return 1 if there are no
errors, 0 if
there are errors, so it can be safely used as the return value from
Controller
Actions.

The optional template arguments will be set as appropriate if passed.

=cut

sub cleanup {
my $c = shift;
my ($errors, $error_template, $good_template) = @_;

# store any that are hanging around
unshift(@$errors, @{ $c->error() });

if (@$errors) {
push(@{ $c->stash->{Errors} }, @$errors);
$c->clear_errors();
$c->template($error_template) if ($error_template);
@$errors = ();
return 0;
} else {
$c->template($good_template) if ($good_template);
return 1;
}
}


_______________________________________________
Catalyst-dev mailing list
Catalyst-dev@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst-dev
Re: A suggestion(or question) about exception handling. [ In reply to ]
Kee Hinckley wrote:
> On Jul 21, 2008, at 5:46 PM, Matt S Trout wrote:
>> On Sat, Jul 19, 2008 at 10:16:55PM -0400, Chae Lee wrote:
>>> hi all,
>>>
>>> in my Catalyst application, I'm using my own exception handling based on
>>> "Error.pm", and used to ask myself what if Catalyst provided the
>>> facility.
>>>
>>> I know there are several ways in error(or exception) handling in
>>> Perl, but,
>>> anyway my favorite is "try/catch."
>>>
>>> today I saw Catalyst::Exception. It wasn't what I expected.
>>>
>>> so... is there already any extension based on Error.pm in Catalyst OR
>>> any
>>> plan??
>>
>> Extension for what?
>>
>> There's nothing to stop you using Error.pm in the Catalyst app - I don't
>> really see what Catalyst needs to do about it?
>
> I use Error extensively in Catalyst, but I understand the problem he
> raises. Right now Catalyst traps errors itself Catalyst::execute().
> What Chae Lee probably wants (I know I do) is a way to insert an Error
> handler that traps those errors, or at the very least, uses try/catch so
> that what gets stored in $c->error are Error objects instead of
> strings. You can't fix this with a Plugin because you have no way of
> knowing when in the process your plugin will be called. As a result, I
> tend to end up with actions that look like:


For me, it's wishing for an exception_action just like DBIC has, and way
I can subvert exceptions at the app leve without tinkering with
errors/setting/clearing/resetting, etc.

-=Chris
Re: A suggestion(or question) about exception handling. [ In reply to ]
On 23 Jul 2008, at 16:59, Kee Hinckley wrote:
> I use Error extensively in Catalyst, but I understand the problem
> he raises. Right now Catalyst traps errors itself Catalyst::execute
> (). What Chae Lee probably wants (I know I do) is a way to insert
> an Error handler that traps those errors, or at the very least,
> uses try/catch so that what gets stored in $c->error are Error
> objects instead of strings.

Putting:

BEGIN { $Catalyst::Exception::CATALYST_EXCEPTION_CLASS = 'Error'; }

in MyApp.pm should do what you want, shouldn't it?

I'd also refer you to Bricas' blog post about using classful
exceptions in Catalyst:
http://bricas.vox.com/library/post/catalyst-exceptionclass.html

Whilst I don't use this, I think that this is about what you want..

It'd be good to see this made this easier and/or for it to be more
'officially' documented however. :)

Cheers
Tom


_______________________________________________
Catalyst-dev mailing list
Catalyst-dev@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst-dev
Re: A suggestion(or question) about exception handling. [ In reply to ]
On Wed, Jul 23, 2008 at 11:59:22AM -0400, Kee Hinckley wrote:
> I use Error extensively in Catalyst, but I understand the problem he
> raises. Right now Catalyst traps errors itself Catalyst::execute().

If you throw Error objects, Catalyst will trap them just like any other
exception, and stuff them in $c->error for you to process later.

I don't honestly see how keeping an array of errors yourself is any
different? You could use a SIGDIE to inflate them to an exception
object on the way past if that's what you want? (or a custom Action
class with its own execute() method that would run within the Catalyst
execute() eval cage)

> What Chae Lee probably wants (I know I do) is a way to insert an Error
> handler that traps those errors, or at the very least, uses try/catch
> so that what gets stored in $c->error are Error objects instead of
> strings. You can't fix this with a Plugin because you have no way of
> knowing when in the process your plugin will be called.

What do you mean "when in the process" ? What hooks do you think you need
that aren't already there? Something like $c->run_action_with_eval that
has just the eval { ... } part so you can infalte an exception object?
More than that? Something completely different?

More importantly, can one of you -please- show me a sketch of before -and-
after code with this feature you're imagining in place? I still really
don't get what this feature would achieve for you, all I'm seeing is
"this is awful, look at how ugly this code is" but no explanation of what
would be prettier and how you'd like to be able to get there.

I'd really like to make sure whatever you're trying to achieve, we add
the hooks so you can make a plugin that achieves it - and preferably
so anybody using another exception system can write one that works for
them, too (for e.g. I'd tend to use Exception::Class rather than Error
since it's (a) Moose (b) not maintained by an asocial idiot who's currently
trying to fork the catalyst IRC community over his banning from irc.perl.org)

--
Matt S Trout Need help with your Catalyst or DBIx::Class project?
Technical Director http://www.shadowcat.co.uk/catalyst/
Shadowcat Systems Ltd. Want a managed development or deployment platform?
http://chainsawblues.vox.com/ http://www.shadowcat.co.uk/servers/

_______________________________________________
Catalyst-dev mailing list
Catalyst-dev@lists.scsys.co.uk
http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/catalyst-dev
Re: A suggestion(or question) about exception handling. [ In reply to ]
> Putting:
>
> BEGIN { $Catalyst::Exception::CATALYST_EXCEPTION_CLASS = 'Error'; }
>
> in MyApp.pm should do what you want, shouldn't it?


right after I looked at the source code(Catalyst::Exception), I thought the
way above. Before trying some code, I wanted to see examples in Catalyst
core and core modules, but I couldn't find.

Error and exception handling is the most fundamental part of a system, here
Catalyst framework, so it MUST be guaranteed. I'd like to use the way above
only if the core uses the way.
Re: A suggestion(or question) about exception handling. [ In reply to ]
>
> Try again, please. I asked -how- it would help, not just for you to say "it
> would help" again.
>
> You talk about a nonexistant extension/feature and say it would be a good
> thing, but you've given no explanation of what such a feature/extension
> would actually -do-.
>
> How can we tell whether it would be useful to anybody else if the list of
> what it does only exists in your head? :)


I think Catalyst::Model::DBIC::Schema is the best example of Catalyst
extensions. (who wrote that? :)

I want something like these for example: Catalyst::Exception::Error,
Catalyst::Exception::Class. This would be a scenario: I stick to one of the
exception glues, say Error.pm, and you use Exception::Class with the glue
Catalyst::Exception::Class, but you and I use same interface in any Catalyst
app without any touch on existing code.

By the way, I wonder this scenario is possible in current Catalyst. ( I
actually need more time for Catalyst)

Ah, folks showed several ways. Thanks!