Skip Menu |

This queue is for tickets about the Syntax-Feature-Try CPAN distribution.

Report information
The Basics
Id: 99388
Status: resolved
Priority: 0/
Queue: Syntax-Feature-Try

People
Owner: Nobody in particular
Requestors: perl [...] toby.ink
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: 1.001



Subject: overriding _exception_match_args
Syntax::Feature::Try seems to have been on CPAN for a while now, but somehow I've only just noticed it. It seems very good. I'd like to use it to replace Try::Tiny in Moops (see https://metacpan.org/release/Moops for more info), however it has one missing feature before I can do that. Moops provides syntactic sugar for OO programming in Moose, Mouse, or Moo. You don't have to choose just one - you can write one class with Moose, and one with Moo, and one with Mouse. Because each of these OO frameworks has different type constraint systems, I'd need _exception_match_args to behave differently based on the caller. A simple implementation of the kind of thing I mean would be: our %TYPE_HANDLER; sub _exception_match_args { my ($exception, $className) = @_; return 1 if not defined $className; # without args catch all exceptions my $caller = caller 2; return $TYPE_HANDLER{$caller}->($caller, $exception, $className) if $TYPE_HANDLER{$caller}; if (Moose::Util::TypeConstraints->can('find_type_constraint')) { my $type = Moose::Util::TypeConstraints::find_type_constraint($className); return $type->check($exception) if $type; } return blessed($exception) && $exception->isa($className); } Then I'd just have to ensure that Moops populated the %TYPE_HANDLER hash for each class it created. That's not the only way of providing this feature, but it seems the easiest. Whatever route you chose, I'd also need it to be officially documented, so I can be sure it's not going to disappear in the future.
Hi, This implementation based on "caller 2" do not looks so good. But I'am not sure if I understand requested feature. Do you mean following example?: package A; use syntax 'try' => { custom_exception_handler => sub { my ($exception, $className) = @_; # there should be custom exception handler for package A returning true/false. } }; try { ... } catch { } ... package B; use syntax 'try'; # (without parameters) use default exception handler in package B ; On Wed Oct 08 17:51:26 2014, TOBYINK wrote: Show quoted text
> Syntax::Feature::Try seems to have been on CPAN for a while now, but > somehow I've only just noticed it. It seems very good. I'd like to use > it to replace Try::Tiny in Moops (see > https://metacpan.org/release/Moops for more info), however it has one > missing feature before I can do that. > > Moops provides syntactic sugar for OO programming in Moose, Mouse, or > Moo. You don't have to choose just one - you can write one class with > Moose, and one with Moo, and one with Mouse. Because each of these OO > frameworks has different type constraint systems, I'd need > _exception_match_args to behave differently based on the caller. > > A simple implementation of the kind of thing I mean would be: > > our %TYPE_HANDLER; > sub _exception_match_args { > my ($exception, $className) = @_; > return 1 if not defined $className; # without args catch all > exceptions > > my $caller = caller 2; > return $TYPE_HANDLER{$caller}->($caller, $exception, $className) > if $TYPE_HANDLER{$caller}; > > if (Moose::Util::TypeConstraints->can('find_type_constraint')) { > my $type = > Moose::Util::TypeConstraints::find_type_constraint($className); > return $type->check($exception) if $type; > } > > return blessed($exception) && $exception->isa($className); > } > > Then I'd just have to ensure that Moops populated the %TYPE_HANDLER > hash for each class it created. > > That's not the only way of providing this feature, but it seems the > easiest. > > Whatever route you chose, I'd also need it to be officially > documented, so I can be sure it's not going to disappear in the > future.
From: danschmidt5189 [...] gmail.com
Just wanted to throw my support behind this idea. I also opened a Github issue with interface samples. (https://github.com/tomas-zemres/syntax-feature-try/issues/5) On Fri Nov 21 03:33:30 2014, tnt wrote: Show quoted text
> Hi, > > This implementation based on "caller 2" do not looks so good. > > But I'am not sure if I understand requested feature. > > Do you mean following example?: > > package A; > > use syntax 'try' => { > custom_exception_handler => sub { > my ($exception, $className) = @_; > # there should be custom exception handler for package A returning > true/false. > } > }; > > try { > ... > } > catch { > } > > > ... > > package B; > > use syntax 'try'; # (without parameters) use default exception > handler in package B > ; > > > On Wed Oct 08 17:51:26 2014, TOBYINK wrote:
> > Syntax::Feature::Try seems to have been on CPAN for a while now, but > > somehow I've only just noticed it. It seems very good. I'd like to > > use > > it to replace Try::Tiny in Moops (see > > https://metacpan.org/release/Moops for more info), however it has one > > missing feature before I can do that. > > > > Moops provides syntactic sugar for OO programming in Moose, Mouse, or > > Moo. You don't have to choose just one - you can write one class with > > Moose, and one with Moo, and one with Mouse. Because each of these OO > > frameworks has different type constraint systems, I'd need > > _exception_match_args to behave differently based on the caller. > > > > A simple implementation of the kind of thing I mean would be: > > > > our %TYPE_HANDLER; > > sub _exception_match_args { > > my ($exception, $className) = @_; > > return 1 if not defined $className; # without args catch all > > exceptions > > > > my $caller = caller 2; > > return $TYPE_HANDLER{$caller}->($caller, $exception, $className) > > if $TYPE_HANDLER{$caller}; > > > > if (Moose::Util::TypeConstraints->can('find_type_constraint')) { > > my $type = > > Moose::Util::TypeConstraints::find_type_constraint($className); > > return $type->check($exception) if $type; > > } > > > > return blessed($exception) && $exception->isa($className); > > } > > > > Then I'd just have to ensure that Moops populated the %TYPE_HANDLER > > hash for each class it created. > > > > That's not the only way of providing this feature, but it seems the > > easiest. > > > > Whatever route you chose, I'd also need it to be officially > > documented, so I can be sure it's not going to disappear in the > > future.
Custom exception matchers being global makes this unusable for me. If class A registers a custom matcher, this should affect how exceptions are matched in class B. It's action at a distance, and it's really hard to debug in a large application.