X.pm

filter: Statementstmt 100% 18 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 100% 25 / 25 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« X/Types.pm ↑ Summary
1 package Throwable::X 0.008;
2 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 # ABSTRACT: useful eXtra behavior for Throwable exceptions
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod Write an exception class:
8 #pod
9 #pod package X::BadValue;
10 #pod use Moose;
11 #pod
12 #pod with qw(Throwable::X StackTrace::Auto);
13 #pod
14 #pod use Throwable::X -all; # to get the Payload helper
15 #pod
16 #pod sub x_tags { qw(value) }
17 #pod
18 #pod # What bad value were we given?
19 #pod has given_value => (
20 #pod is => 'ro',
21 #pod required => 1,
22 #pod traits => [ Payload ],
23 #pod );
24 #pod
25 #pod # What was the value supposed to be used for?
26 #pod has given_for => (
27 #pod is => 'ro',
28 #pod isa => 'Str',
29 #pod traits => [ Payload ],
30 #pod );
31 #pod
32 #pod Throw the exception when you need to:
33 #pod
34 #pod X::BadValue->throw({
35 #pod ident => 'bad filename',
36 #pod tags => [ qw(filename) ],
37 #pod public => 1,
38 #pod message => "invalid filename %{given_value}s for %{given_for}s",
39 #pod given_value => $input,
40 #pod given_for => 'user home directory',
41 #pod });
42 #pod
43 #pod ...and when catching:
44 #pod
45 #pod } catch {
46 #pod my $error = $_;
47 #pod
48 #pod if ($error->does('Throwable::X') and $error->is_public) {
49 #pod
50 #pod # Prints something like:
51 #pod # invalid filename \usr\local\src for user home directory
52 #pod
53 #pod print $error->message, "\n\n", $error->stack_trace->as_string;
54 #pod }
55 #pod }
56 #pod
57 #pod =head1 DESCRIPTION
58 #pod
59 #pod Throwable::X is a collection of behavior for writing exceptions. It's meant to
60 #pod provide:
61 #pod
62 #pod =for :list
63 #pod * means by which exceptions can be identified without string parsing
64 #pod * a structure that can be serialized and reconstituted in other environments
65 #pod * maximum composability by dividing features into individual roles
66 #pod
67 #pod Throwable::X composes the following roles. Each one is documented, but an
68 #pod overview of the features is also provided below so you don't need to hop around
69 #pod in a half dozen roles to understand how to benefit from Throwable::X.
70 #pod
71 #pod =for :list
72 #pod * L<Throwable>
73 #pod * L<Role::HasPayload::Merged>
74 #pod * L<Role::HasMessage::Errf>
75 #pod * L<Role::Identifiable::HasIdent>
76 #pod * L<Role::Identifiable::HasTags>
77 #pod
78 #pod Note that this list does I<not> include L<StackTrace::Auto>. Building a stack
79 #pod isn't needed in all scenarios, so if you want your exceptions to automatically
80 #pod capture a stack trace, compose StackTrace::Auto when building your exception
81 #pod classes.
82 #pod
83 #pod =head2 Features for Identification
84 #pod
85 #pod Every Throwable::X exception has a required C<ident> attribute that contains a
86 #pod one-line string with printable characters in it. Ideally, the ident doesn't
87 #pod try to describe everything about the error, but serves as a unique identifier
88 #pod for the kind of exception being thrown. Exception handlers looking for
89 #pod specific exceptions can then check the ident for known values. It can also be
90 #pod used for refinement or localization of the message format, described below.
91 #pod This feature is provided by L<Role::Identifiable::HasIdent>.
92 #pod
93 #pod For less specific identification of classes of exceptions, the exception can be
94 #pod checked for what roles it performs with C<does>, or its tags can be checked
95 #pod with C<has_tag>. All the tags reported by the C<x_tags> methods of every class
96 #pod and role in the exception's composition are present, as well as per-instance
97 #pod tags provided when the exception was thrown. Tags as simple strings consisting
98 #pod of letters, numbers, and dashes. This feature is provided by
99 #pod L<Role::Identifiable::HasTags>.
100 #pod
101 #pod Throwable::X exceptions also have a message, which (unlike the C<ident>) is
102 #pod meant to be a human-readable string describing precisely what happened. The
103 #pod C<message> argument given when throwing an exception uses a C<sprintf>-like
104 #pod dialect implemented (and described) by L<String::Errf>. It picks data out of
105 #pod the C<payload> (described below) to produce a filled-in string when the
106 #pod C<message> method is called. (The L<synopsis|/SYNOPSIS> above gives a very
107 #pod simple example of how this works, but the String::Errf documentation is more
108 #pod useful, generally.) This feature is provided by
109 #pod L<Role::HasMessage::Errf>.
110 #pod
111 #pod =head2 Features for Serialization
112 #pod
113 #pod The C<payload> method returns a hashref containing the name and value of every
114 #pod attribute with the trait L<Role::HasPayload::Meta::Attribute::Payload>, merged
115 #pod with the hashref (if any) provided as the C<payload> entry to the constructor.
116 #pod There's nothing more to it than that. It's used by the message formatting
117 #pod facility descibed above, and is also useful for serializing exceptions.
118 #pod Assuming no complex values are present in the payload, the structure below
119 #pod should be easy to serialize and use in another program, for example in a web
120 #pod browser receiving a serialized Throwable::X via JSON in response to an
121 #pod XMLHTTPRequest.
122 #pod
123 #pod {
124 #pod ident => $err->ident,
125 #pod message => $err->message_fmt,
126 #pod tags => [ $err->tags ],
127 #pod payload => $err->payload,
128 #pod }
129 #pod
130 #pod There is no specific code present to support doing this, yet.
131 #pod
132 #pod The C<payload> method is implemented by L<Role::HasPayload::Merged>.
133 #pod
134 #pod The C<public> attribute, checked with the C<is_public> method, is meant to
135 #pod indicate whether the exception's message is safe to display to end users or
136 #pod send across the wire to remote clients.
137 #pod
138 #pod =head2 Features for Convenience
139 #pod
140 #pod The C<throw> (or C<new>) method on a Throwable::X exception class can be passed
141 #pod a single string, in which case it will be used as the exception's C<ident>.
142 #pod This is (of course) only useful if no other attribute of the exception is
143 #pod required. This feature is provided by L<MooseX::OneArgNew>.
144 #pod
145 #pod =cut
146  
147 1 ▶use Throwable::X::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
148  
149 1 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
150  
151 # Does this belong elsewhere? -- rjbs, 2010-10-18
152 1 ▶use Sub::Exporter -setup => {
Subroutine BEGIN: called CC=1 SCAR=0.0
153 exports => { Payload => \'__payload' },
154 1 };
155 1 ▶sub __payload { sub { 'Role::HasPayload::Meta::Attribute::Payload' } }
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __payload: called CC=1 SCAR=0.0
156  
157 with(
158 'Throwable',
159 'Role::HasPayload::Merged',
160 'Role::Identifiable::HasIdent',
161 'Role::Identifiable::HasTags',
162  
163 'Role::HasMessage::Errf' => {
164 1 ▶ default => sub { $_[0]->ident },
Subroutine __ANON__: called CC=1 SCAR=0.0
165 1 lazy => 1,
166 },
167  
168 'MooseX::OneArgNew' => {
169 type => 'Throwable::X::_VisibleStr',
170 init_arg => 'ident',
171 },
172 );
173  
174 # Can't do this because we can't +attr in roles. Can't use methods with type,
175 # because methods are too late to parameterize roles. Would rather not add
176 # MXRP as a prereq to all the subroles. -- rjbs, 2010-10-28
177 # has '+ident' => (isa => 'Throwable::X::_Ident');
178 # has '+message_fmt' => (isa => 'Throwable::X::_VisibleStr');
179  
180 1 has is_public => (
181 is => 'ro',
182 isa => 'Bool',
183 init_arg => 'public',
184 default => 0,
185 );
186  
187 1 1;
188  
189 __END__
« X/Types.pm ↑ Summary