New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow replacing Error::TypeTiny::Assertion #133
Comments
A place you theoretically could use it would be subclassing Type::Params::Signature to override Under what circumstances were you hoping to override the class? On a per-type-constraint basis? In a particular lexical scope? |
We have a very weird edge case where we're doing things like this with Moo(se): has some_value => (
is => 'ro',
isa => SomeConstraint,
required => 1,
); This is in some OpenAPI code. It would be nice if we could have around 'BUILDARGS' => sub {
my ( $orig, $class, @args ) = @_;
my $arg_for = $class->$orig(@args);
unless ( SomeConstraint->check($arg_for->{some_value}) ) {
... throw the exception we want, not the exception we'd get
}
... So we have to write a bunch of fragile boilerplate when all we want is a different exception. |
Oh, and many of these are in-house constraints, not just default ones shipped with |
I'd suggest blessing I guess the best option for your use case would be for my $exception_class = delete( $attrs{exception_class} )
|| $self->{exception_class}
|| "Error::TypeTiny::Assertion"; |
With the above patch, something like this should work:
|
This is included in Type::Tiny 2.003_000 on CPAN. I should release a stable version some time this month. |
Upstream changes: 2.004000 2023-04-05 [ Documentation ] - Document that the `BoolLike` type is unstable. - Minor pod changes to Types::Standard. [ Packaging ] - Summarized the change log for versions prior to Type::Tiny 2.000000. If you need more information, see the Changes file included with Type::Tiny 2.002001. <https://metacpan.org/release/TOBYINK/Type-Tiny-2.002001/source/Changes> 2.003_000 2023-04-02 [ Documentation ] - Add SYNOPSIS for Type::Tiny::Class. - Add SYNOPSIS for Type::Tiny::Duck. - Add SYNOPSIS for Type::Tiny::Enum. - Add SYNOPSIS for Type::Tiny::Intersection. - Add SYNOPSIS for Type::Tiny::Role. - Add SYNOPSIS for Type::Tiny::Union. - Add documentation and tests for the combination of the `goto_next` and `multiple` options when used with `signature_for`. - Add example of `signature_for` applying a signature to multiple functions at once. - Document changes to `make_immutable` in Type::Library v2.x. <tobyink/p5-type-tiny#129> [ Other ] - Added: Type::Tiny now has an `exception_class` attribute, allowing a type to throw exceptions using a custom class. These classes should usually be a subclass of Error::TypeTiny::Assertion. <tobyink/p5-type-tiny#133> - Added: Type::Tiny::Bitfield class. <tobyink/p5-type-tiny#131> - Added: Types::TypeTiny::BoolLike type constraint. <tobyink/p5-type-tiny#137>
In
lib/Type/Tiny.pm
, we have this (some of it truncated to show structure):However, every place that calls this does so like this:
$self->_failed_check( "$self", $_ );
. So that attributes are not passed to_failed_check
and we can't replace the exception class.(Of course,
exception_class
isn't documented, so this is for some future code?)The text was updated successfully, but these errors were encountered: