File Coverage

blib/lib/Specio/Exception.pm
Criterion Covered Total %
statement 29 29 100.0
branch 1 2 50.0
condition n/a
subroutine 10 10 100.0
pod 1 3 33.3
total 41 44 93.1


line stmt bran cond sub pod time code
1             package Specio::Exception;
2              
3 28     28   162 use strict;
  28         46  
  28         713  
4 28     28   118 use warnings;
  28         44  
  28         761  
5              
6             use overload
7 28         193 q{""} => 'as_string',
8 28     28   121 fallback => 1;
  28         40  
9              
10             our $VERSION = '0.46';
11              
12 28     28   14291 use Devel::StackTrace;
  28         80388  
  28         842  
13 28     28   186 use Scalar::Util qw( blessed );
  28         55  
  28         1113  
14 28     28   140 use Specio::OO;
  28         90  
  28         5486  
15              
16             {
17             my $attrs = {
18             message => {
19             isa => 'Str',
20             required => 1,
21             },
22             type => {
23             does => 'Specio::Constraint::Role::Interface',
24             required => 1,
25             },
26             value => {
27             required => 1,
28             },
29             stack_trace => {
30             init_arg => undef,
31             },
32             };
33              
34             ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
35             sub _attrs {
36 56     56   108 return $attrs;
37             }
38             }
39              
40             sub BUILD {
41 2866     2866 0 61141 my $self = shift;
42              
43             $self->{stack_trace}
44 2866         8350 = Devel::StackTrace->new( ignore_package => __PACKAGE__ );
45              
46 2866         2494812 return;
47             }
48              
49             sub as_string {
50 5731     5731 1 226723 my $self = shift;
51              
52 5731         15002 my $str = $self->message;
53 5731         25455 $str .= "\n\n" . $self->stack_trace->as_string;
54              
55 5731         8662631 return $str;
56             }
57              
58             sub throw {
59 2866     2866 0 5033 my $self = shift;
60              
61 2866 50       7265 die $self if blessed $self;
62              
63 2866         8700 die $self->new(@_);
64             }
65              
66             __PACKAGE__->_ooify;
67              
68             1;
69              
70             # ABSTRACT: An exception class for type constraint failures
71              
72             __END__
73              
74             =pod
75              
76             =encoding UTF-8
77              
78             =head1 NAME
79              
80             Specio::Exception - An exception class for type constraint failures
81              
82             =head1 VERSION
83              
84             version 0.46
85              
86             =head1 SYNOPSIS
87              
88             use Try::Tiny;
89              
90             try {
91             $type->validate_or_die($value);
92             }
93             catch {
94             if ( $_->isa('Specio::Exception') ) {
95             print $_->message, "\n";
96             print $_->type->name, "\n";
97             print $_->value, "\n";
98             }
99             };
100              
101             =head1 DESCRIPTION
102              
103             This exception class is thrown by Specio when a type check fails. It emulates
104             the L<Throwable::Error> API, but doesn't use that module to avoid adding a
105             dependency on L<Moo>.
106              
107             =for Pod::Coverage BUILD throw
108              
109             =head1 API
110              
111             This class provides the following methods:
112              
113             =head2 $exception->message
114              
115             The error message associated with the exception.
116              
117             =head2 $exception->stack_trace
118              
119             A L<Devel::StackTrace> object for the exception.
120              
121             =head2 $exception->type
122              
123             The type constraint object against which the value failed.
124              
125             =head2 $exception->value
126              
127             The value that failed the type check.
128              
129             =head2 $exception->as_string
130              
131             The exception as a string. This includes the method and the stack trace.
132              
133             =head1 OVERLOADING
134              
135             This class overloads stringification to call the C<as_string> method.
136              
137             =head1 SUPPORT
138              
139             Bugs may be submitted at L<https://github.com/houseabsolute/Specio/issues>.
140              
141             I am also usually active on IRC as 'autarch' on C<irc://irc.perl.org>.
142              
143             =head1 SOURCE
144              
145             The source code repository for Specio can be found at L<https://github.com/houseabsolute/Specio>.
146              
147             =head1 AUTHOR
148              
149             Dave Rolsky <autarch@urth.org>
150              
151             =head1 COPYRIGHT AND LICENSE
152              
153             This software is Copyright (c) 2012 - 2020 by Dave Rolsky.
154              
155             This is free software, licensed under:
156              
157             The Artistic License 2.0 (GPL Compatible)
158              
159             The full text of the license can be found in the
160             F<LICENSE> file included with this distribution.
161              
162             =cut