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 29     29   194 use strict;
  29         53  
  29         848  
4 29     29   147 use warnings;
  29         47  
  29         956  
5              
6             use overload
7 29         235 q{""} => 'as_string',
8 29     29   148 fallback => 1;
  29         53  
9              
10             our $VERSION = '0.47';
11              
12 29     29   17928 use Devel::StackTrace;
  29         95468  
  29         955  
13 29     29   209 use Scalar::Util qw( blessed );
  29         56  
  29         1298  
14 29     29   169 use Specio::OO;
  29         54  
  29         6581  
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 58     58   128 return $attrs;
37             }
38             }
39              
40             sub BUILD {
41 2866     2866 0 66958 my $self = shift;
42              
43             $self->{stack_trace}
44 2866         9331 = Devel::StackTrace->new( ignore_package => __PACKAGE__ );
45              
46 2866         2709758 return;
47             }
48              
49             sub as_string {
50 5731     5731 1 257867 my $self = shift;
51              
52 5731         17948 my $str = $self->message;
53 5731         27735 $str .= "\n\n" . $self->stack_trace->as_string;
54              
55 5731         9396544 return $str;
56             }
57              
58             sub throw {
59 2866     2866 0 5340 my $self = shift;
60              
61 2866 50       7576 die $self if blessed $self;
62              
63 2866         9597 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.47
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 - 2021 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