| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Throwable::Error; | 
| 2 |  |  |  |  |  |  | # ABSTRACT: an easy-to-use class for error objects | 
| 3 |  |  |  |  |  |  | $Throwable::Error::VERSION = '1.001'; | 
| 4 | 1 |  |  | 1 |  | 94930 | use Moo 1.000001; | 
|  | 1 |  |  |  |  | 22 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 5 |  |  |  |  |  |  | with 'Throwable', 'StackTrace::Auto'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | #pod =head1 SYNOPSIS | 
| 8 |  |  |  |  |  |  | #pod | 
| 9 |  |  |  |  |  |  | #pod   package MyApp::Error; | 
| 10 |  |  |  |  |  |  | #pod   # NOTE: Moo can also be used here instead of Moose | 
| 11 |  |  |  |  |  |  | #pod   use Moose; | 
| 12 |  |  |  |  |  |  | #pod   extends 'Throwable::Error'; | 
| 13 |  |  |  |  |  |  | #pod | 
| 14 |  |  |  |  |  |  | #pod   has execution_phase => ( | 
| 15 |  |  |  |  |  |  | #pod     is  => 'ro', | 
| 16 |  |  |  |  |  |  | #pod     isa => 'MyApp::Phase', | 
| 17 |  |  |  |  |  |  | #pod     default => 'startup', | 
| 18 |  |  |  |  |  |  | #pod   ); | 
| 19 |  |  |  |  |  |  | #pod | 
| 20 |  |  |  |  |  |  | #pod ...and in your app... | 
| 21 |  |  |  |  |  |  | #pod | 
| 22 |  |  |  |  |  |  | #pod   MyApp::Error->throw("all communications offline"); | 
| 23 |  |  |  |  |  |  | #pod | 
| 24 |  |  |  |  |  |  | #pod   # or... | 
| 25 |  |  |  |  |  |  | #pod | 
| 26 |  |  |  |  |  |  | #pod   MyApp::Error->throw({ | 
| 27 |  |  |  |  |  |  | #pod     message         => "all communications offline", | 
| 28 |  |  |  |  |  |  | #pod     execution_phase => 'shutdown', | 
| 29 |  |  |  |  |  |  | #pod   }); | 
| 30 |  |  |  |  |  |  | #pod | 
| 31 |  |  |  |  |  |  | #pod =head1 DESCRIPTION | 
| 32 |  |  |  |  |  |  | #pod | 
| 33 |  |  |  |  |  |  | #pod Throwable::Error is a base class for exceptions that will be thrown to signal | 
| 34 |  |  |  |  |  |  | #pod errors and abort normal program flow.  Throwable::Error is an alternative to | 
| 35 |  |  |  |  |  |  | #pod L<Exception::Class|Exception::Class>, the features of which are largely | 
| 36 |  |  |  |  |  |  | #pod provided by the Moo object system atop which Throwable::Error is built. | 
| 37 |  |  |  |  |  |  | #pod | 
| 38 |  |  |  |  |  |  | #pod Throwable::Error performs the L<Throwable|Throwable> and L<StackTrace::Auto> | 
| 39 |  |  |  |  |  |  | #pod roles.  That means you can call C<throw> on it to create and throw an error | 
| 40 |  |  |  |  |  |  | #pod object in one call, and that every error object will have a stack trace for its | 
| 41 |  |  |  |  |  |  | #pod creation. | 
| 42 |  |  |  |  |  |  | #pod | 
| 43 |  |  |  |  |  |  | #pod =cut | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | use overload | 
| 46 | 1 |  |  |  |  | 5 | q{""}    => 'as_string', | 
| 47 | 1 |  |  | 1 |  | 1192 | fallback => 1; | 
|  | 1 |  |  |  |  | 790 |  | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | #pod =attr message | 
| 50 |  |  |  |  |  |  | #pod | 
| 51 |  |  |  |  |  |  | #pod This attribute must be defined and must contain a string describing the error | 
| 52 |  |  |  |  |  |  | #pod condition.  This string will be printed at the top of the stack trace when the | 
| 53 |  |  |  |  |  |  | #pod error is stringified. | 
| 54 |  |  |  |  |  |  | #pod | 
| 55 |  |  |  |  |  |  | #pod =cut | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | has message => ( | 
| 58 |  |  |  |  |  |  | is       => 'ro', | 
| 59 |  |  |  |  |  |  | isa      => Sub::Quote::quote_sub(q{ | 
| 60 |  |  |  |  |  |  | die "message must be a string" | 
| 61 |  |  |  |  |  |  | unless defined($_[0]) && !ref($_[0]); | 
| 62 |  |  |  |  |  |  | }),, | 
| 63 |  |  |  |  |  |  | required => 1, | 
| 64 |  |  |  |  |  |  | ); | 
| 65 |  |  |  |  |  |  |  | 
| 66 |  |  |  |  |  |  | #pod =attr stack_trace | 
| 67 |  |  |  |  |  |  | #pod | 
| 68 |  |  |  |  |  |  | #pod This attribute, provided by L<StackTrace::Auto>, will contain a stack trace | 
| 69 |  |  |  |  |  |  | #pod object guaranteed to respond to the C<as_string> method.  For more information | 
| 70 |  |  |  |  |  |  | #pod about the stack trace and associated behavior, consult the L<StackTrace::Auto> | 
| 71 |  |  |  |  |  |  | #pod docs. | 
| 72 |  |  |  |  |  |  | #pod | 
| 73 |  |  |  |  |  |  | #pod =method as_string | 
| 74 |  |  |  |  |  |  | #pod | 
| 75 |  |  |  |  |  |  | #pod This method will provide a string representing the error, containing the | 
| 76 |  |  |  |  |  |  | #pod error's message followed by the its stack trace. | 
| 77 |  |  |  |  |  |  | #pod | 
| 78 |  |  |  |  |  |  | #pod =cut | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | sub as_string { | 
| 81 | 1 |  |  | 1 | 1 | 334 | my ($self) = @_; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 1 |  |  |  |  | 6 | my $str = $self->message; | 
| 84 | 1 |  |  |  |  | 20 | $str .= "\n\n" . $self->stack_trace->as_string; | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 1 |  |  |  |  | 252 | return $str; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | around BUILDARGS => sub { | 
| 90 |  |  |  |  |  |  | my $orig = shift; | 
| 91 |  |  |  |  |  |  | my $self = shift; | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | return {} unless @_; | 
| 94 |  |  |  |  |  |  | return {} if @_ == 1 and ! defined $_[0]; | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | if (@_ == 1 and (!ref $_[0]) and defined $_[0] and length $_[0]) { | 
| 97 |  |  |  |  |  |  | return { message => $_[0] }; | 
| 98 |  |  |  |  |  |  | } | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | return $self->$orig(@_); | 
| 101 |  |  |  |  |  |  | }; | 
| 102 |  |  |  |  |  |  |  | 
| 103 |  |  |  |  |  |  | 1; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | __END__ | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | =pod | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | =encoding UTF-8 | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | =head1 NAME | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | Throwable::Error - an easy-to-use class for error objects | 
| 114 |  |  |  |  |  |  |  | 
| 115 |  |  |  |  |  |  | =head1 VERSION | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | version 1.001 | 
| 118 |  |  |  |  |  |  |  | 
| 119 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | package MyApp::Error; | 
| 122 |  |  |  |  |  |  | # NOTE: Moo can also be used here instead of Moose | 
| 123 |  |  |  |  |  |  | use Moose; | 
| 124 |  |  |  |  |  |  | extends 'Throwable::Error'; | 
| 125 |  |  |  |  |  |  |  | 
| 126 |  |  |  |  |  |  | has execution_phase => ( | 
| 127 |  |  |  |  |  |  | is  => 'ro', | 
| 128 |  |  |  |  |  |  | isa => 'MyApp::Phase', | 
| 129 |  |  |  |  |  |  | default => 'startup', | 
| 130 |  |  |  |  |  |  | ); | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | ...and in your app... | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | MyApp::Error->throw("all communications offline"); | 
| 135 |  |  |  |  |  |  |  | 
| 136 |  |  |  |  |  |  | # or... | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | MyApp::Error->throw({ | 
| 139 |  |  |  |  |  |  | message         => "all communications offline", | 
| 140 |  |  |  |  |  |  | execution_phase => 'shutdown', | 
| 141 |  |  |  |  |  |  | }); | 
| 142 |  |  |  |  |  |  |  | 
| 143 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | Throwable::Error is a base class for exceptions that will be thrown to signal | 
| 146 |  |  |  |  |  |  | errors and abort normal program flow.  Throwable::Error is an alternative to | 
| 147 |  |  |  |  |  |  | L<Exception::Class|Exception::Class>, the features of which are largely | 
| 148 |  |  |  |  |  |  | provided by the Moo object system atop which Throwable::Error is built. | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | Throwable::Error performs the L<Throwable|Throwable> and L<StackTrace::Auto> | 
| 151 |  |  |  |  |  |  | roles.  That means you can call C<throw> on it to create and throw an error | 
| 152 |  |  |  |  |  |  | object in one call, and that every error object will have a stack trace for its | 
| 153 |  |  |  |  |  |  | creation. | 
| 154 |  |  |  |  |  |  |  | 
| 155 |  |  |  |  |  |  | =head1 PERL VERSION | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | This library should run on perls released even a long time ago.  It should work | 
| 158 |  |  |  |  |  |  | on any version of perl released in the last five years. | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | Although it may work on older versions of perl, no guarantee is made that the | 
| 161 |  |  |  |  |  |  | minimum required version will not be increased.  The version may be increased | 
| 162 |  |  |  |  |  |  | for any reason, and there is no promise that patches will be accepted to lower | 
| 163 |  |  |  |  |  |  | the minimum required perl. | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | =head1 ATTRIBUTES | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | =head2 message | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | This attribute must be defined and must contain a string describing the error | 
| 170 |  |  |  |  |  |  | condition.  This string will be printed at the top of the stack trace when the | 
| 171 |  |  |  |  |  |  | error is stringified. | 
| 172 |  |  |  |  |  |  |  | 
| 173 |  |  |  |  |  |  | =head2 stack_trace | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | This attribute, provided by L<StackTrace::Auto>, will contain a stack trace | 
| 176 |  |  |  |  |  |  | object guaranteed to respond to the C<as_string> method.  For more information | 
| 177 |  |  |  |  |  |  | about the stack trace and associated behavior, consult the L<StackTrace::Auto> | 
| 178 |  |  |  |  |  |  | docs. | 
| 179 |  |  |  |  |  |  |  | 
| 180 |  |  |  |  |  |  | =head1 METHODS | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | =head2 as_string | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | This method will provide a string representing the error, containing the | 
| 185 |  |  |  |  |  |  | error's message followed by the its stack trace. | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | =head1 AUTHORS | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | =over 4 | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | =item * | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | Ricardo SIGNES <cpan@semiotic.systems> | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | =item * | 
| 196 |  |  |  |  |  |  |  | 
| 197 |  |  |  |  |  |  | Florian Ragwitz <rafl@debian.org> | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | =back | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | This software is copyright (c) 2022 by Ricardo SIGNES. | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | This is free software; you can redistribute it and/or modify it under | 
| 206 |  |  |  |  |  |  | the same terms as the Perl 5 programming language system itself. | 
| 207 |  |  |  |  |  |  |  | 
| 208 |  |  |  |  |  |  | =cut |