| 1 | package Froody::Error; | ||||||||||||||||||||||||||||
| 2 | 60 | ▶ | use base qw(Exporter Error); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 60 | our @EXPORT_OK = qw(err); | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 60 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 60 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 60 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 60 | ▶ | use Storable; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 60 | ▶ | use YAML::Syck; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | Froody::Error - Froody error class | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | use Froody::Error qw(err); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | # throw an error | ||||||||||||||||||||||||||||
| 21 | eval { | ||||||||||||||||||||||||||||
| 22 | Froody::Error->throw("user.problem", "user '$userid' not known", $data); | ||||||||||||||||||||||||||||
| 23 | }; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # "user.problem, user '9BC6DD8C-1E25-11DA-98F1-DDB51046DF9C' not known, " | ||||||||||||||||||||||||||||
| 26 | print $@->code .", ". $@->msg | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | # and a stacktrace | ||||||||||||||||||||||||||||
| 29 | print $@->stacktrace | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # print the data section | ||||||||||||||||||||||||||||
| 32 | print Dumper $@->data; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # check if the error is the right class | ||||||||||||||||||||||||||||
| 35 | if (err("user")) | ||||||||||||||||||||||||||||
| 36 | { | ||||||||||||||||||||||||||||
| 37 | ... | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | Froody::Error is the Froody error class. It's designed to be powerful, yet | ||||||||||||||||||||||||||||
| 43 | simple to use. It's a subclass of Error.pm. | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | Often, the easiest way to create an error response from within Froody is | ||||||||||||||||||||||||||||
| 46 | to throw a Froody::Error. This will be caught by the server, and it'll | ||||||||||||||||||||||||||||
| 47 | be turned into an error response before dispatching it to the client. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | Standard Froody::Error errors that are thrown by Froody itself indicating | ||||||||||||||||||||||||||||
| 50 | that something is wrong with Froody (e.g. you've asked for a method that | ||||||||||||||||||||||||||||
| 51 | doesn't exist, or you've ommited a required parameter) are listed in | ||||||||||||||||||||||||||||
| 52 | L<Froody::Error::Standard>. | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head2 throw | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | To throw an error with Froody::Error you just have to call the throw method | ||||||||||||||||||||||||||||
| 57 | with up to three arguments: | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # throw an error with its code, reporting the default message | ||||||||||||||||||||||||||||
| 60 | Froody::Error->throw("what"); | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # throw an error with its code, reporting a custom message | ||||||||||||||||||||||||||||
| 63 | Froody::Error->throw("what", "bang"); | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # throw an error with code, custome message, and some data | ||||||||||||||||||||||||||||
| 66 | Froody::Error->throw("what", bang", { thingy => "broken" }); | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | In each case this creates a new Froody::Error object and then C<die>s with it. | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | The first argument is the C<code> that defines the I<type> of error that we're | ||||||||||||||||||||||||||||
| 71 | throwing. The second argument is a C<message>, a string which describes the | ||||||||||||||||||||||||||||
| 72 | error. If this error is translated to a Froody error response then these will | ||||||||||||||||||||||||||||
| 73 | be mapped to the C<code> and C<message> attributes of the C<< <err> >> | ||||||||||||||||||||||||||||
| 74 | repectivly. The third argument is the data, a set of parameters that decribe | ||||||||||||||||||||||||||||
| 75 | in a computer understandable format what causes the error. This data | ||||||||||||||||||||||||||||
| 76 | potentially will be transformed into the children of the C<< <err> >> tag based | ||||||||||||||||||||||||||||
| 77 | on what specification is in the C<errortypes> of the repository. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head2 Hierarchal Error Messages | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Error messages use 'dot' notation to indicate what error messages are | ||||||||||||||||||||||||||||
| 82 | sub-types of other error messages. | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | For example: | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | Froody::Error->throw("io", "Bad IO"); | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | And, a more particular error: | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Froody::Error->throw("io.file", "There was a problem with the file"); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | And an even more particular error | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Froody::Error->throw("io.file.notfound", "File not found"); | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | But all these methods can be detected with isa_err | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | if ($@->isa_err("io")) { ... } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Or even better with the err functions (as this won't go "bang" if $@ is a | ||||||||||||||||||||||||||||
| 101 | hashref) | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | use Froody::Error qw(err); | ||||||||||||||||||||||||||||
| 104 | if (err("io")) { ... } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # err is documented | ||||||||||||||||||||||||||||
| 109 | sub err | ||||||||||||||||||||||||||||
| 110 | 44 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
err: called CC=6 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 60 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 44 | my $code = shift; | |||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 44 | ▶ | return unless length $@; # no err? Not an error | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 42 | ▶ | return 1 unless length $code; # empty spec? Always true | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | # Froody::Error? Of the right code? | ||||||||||||||||||||||||||||
| 119 | 39 | ▶ | if (blessed($@) && $@->isa("Froody::Error")) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($@) and $@->isa("Froody::Error")
MC/DC50%1/2
builtin::blessed($@) and $@->isa("Froody::Error")
builtin::blessed($@)
$@->isa("Froody::Error")
Truth table66%2/3
A and B
A= builtin::blessed($@)B= $@->isa("Froody::Error")
| |||||||||||||||||||||||||||||
| 120 | 36 | { return $@->isa_err($code) } | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # It's an unknown error, were we looking for that? | ||||||||||||||||||||||||||||
| 123 | # There's no easy way I can see to make this functionality overridable | ||||||||||||||||||||||||||||
| 124 | # after all, we're in a *function* here, not a method. | ||||||||||||||||||||||||||||
| 125 | 3 | ▶ | return $code eq "unknown" || $code eq "999"; | ||||||||||||||||||||||||||
|
Condition100%3/3
$code eq 'unknown' || $code eq '999'
MC/DC100%2/2
$code eq 'unknown' || $code eq '999'
$code eq 'unknown'
$code eq '999'
Truth table100%3/3
A || B
A= $code eq 'unknown'B= $code eq '999'
| |||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | # isa_err is documented | ||||||||||||||||||||||||||||
| 129 | sub isa_err | ||||||||||||||||||||||||||||
| 130 | 46 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
isa_err: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 131 | 60 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 46 | my $self = shift; | |||||||||||||||||||||||||||
| 134 | 46 | my $err = shift; | |||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | # empty string === root node === always match | ||||||||||||||||||||||||||||
| 137 | 46 | ▶ | return 1 unless length $err; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | # if we've no code then we must fail, as we can't possibly match it | ||||||||||||||||||||||||||||
| 140 | 44 | ▶ | return 0 unless length $self->code; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | # do we match? | ||||||||||||||||||||||||||||
| 143 | 44 | $err = quotemeta($err); | |||||||||||||||||||||||||||
| 144 | 44 | return $self->code =~ /^$err(?:\.|$)/ | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | =head1 METHODS | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =over | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =item new( message ) | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | define | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | =cut | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | 55 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 55 | my $self = shift; | |||||||||||||||||||||||||||
| 159 | 55 | local $Error::Debug = 1; | |||||||||||||||||||||||||||
| 160 | 55 | local $Error::Depth = $Error::Depth + 1; | |||||||||||||||||||||||||||
| 161 | 55 | my $code = shift; | |||||||||||||||||||||||||||
| 162 | 55 | my $text = shift; | |||||||||||||||||||||||||||
| 163 | 55 | my $data = shift; | |||||||||||||||||||||||||||
| 164 | 55 | $self->SUPER::new( | |||||||||||||||||||||||||||
| 165 | -text => $text, | ||||||||||||||||||||||||||||
| 166 | -code => $code, | ||||||||||||||||||||||||||||
| 167 | -data => $data, | ||||||||||||||||||||||||||||
| 168 | ); | ||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =item code | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | Return the error code. Read only. | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | =cut | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 173 | ▶ | sub code { | ||||||||||||||||||||||||||
|
Subroutine
code: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 178 | 60 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 179 | 173 | ▶ | length($_[0]->{-code}) ? $_[0]->{-code} : "unknown"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | =item message / msg / text | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Returns the error message. Read only. | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | =cut | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 1 | ▶ | sub text { shift->{-text} } | ||||||||||||||||||||||||||
|
Subroutine
text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 189 | 1 | ▶ | sub msg { shift->{-text} } | ||||||||||||||||||||||||||
|
Subroutine
msg: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 141 | ▶ | sub message { shift->{-text} } | ||||||||||||||||||||||||||
|
Subroutine
message: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =item data | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | Return (a copy of) the error data. Read only. | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | =cut | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 11 | ▶ | sub _dclone { | ||||||||||||||||||||||||||
|
Subroutine
_dclone: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 199 | 11 | ▶ | return $_[0] unless ref $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 200 | 11 | return Storable::dclone $_[0]; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 11 | ▶ | sub data { _dclone(shift->{-data}) } | ||||||||||||||||||||||||||
|
Subroutine
data: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =item stringify | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | Returns a string containing both the error code and the error text, and | ||||||||||||||||||||||||||||
| 208 | a stacktrace. This is what is returned if you try and use a Froody::Error | ||||||||||||||||||||||||||||
| 209 | in string context. | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | =cut | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | 67 | ▶ | sub stringify { | ||||||||||||||||||||||||||
|
Subroutine
stringify: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 214 | 60 | ▶ | no warnings 'uninitialized'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 215 | 67 | my $self = shift; | |||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | 67 | my $strace = $self->stacktrace; | |||||||||||||||||||||||||||
| 218 | 67 | $strace =~ s/^/ /mg; | |||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 67 | my $return = $self->code; | |||||||||||||||||||||||||||
| 221 | 67 | ▶ | $return .= " - " . $self->message if length($self->message); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | # don't call the ->data method here, as we don't really need to clone the data | ||||||||||||||||||||||||||||
| 223 | 67 | ▶ | $return .= "\nData:\n".Dump($self->{-data}) if defined $self->{-data}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 67 | return $return . "\nStack trace:\n$strace" | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =back | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head1 BACKWARDS COMPATIBILITY | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | We used to have a different sort of error. Rather than having hierarchal | ||||||||||||||||||||||||||||
| 232 | error codes that looked like this: | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | <err code="file.rwerr" message="unable to write to disk" /> | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | We used to use simple numbers and have errors that looked like this: | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | <err code="12" message="unable to write to disk" /> | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | This is fine. The key here to remember is that numbered error codes are a | ||||||||||||||||||||||||||||
| 241 | subset of the hierarchal error codes. The numbers are all just top level | ||||||||||||||||||||||||||||
| 242 | errors that are made up of alphanumerics that are just digits. It's just | ||||||||||||||||||||||||||||
| 243 | a not very hierarchal hierarchal error code. | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | =head1 BUGS | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | None known. | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | Please report any bugs you find via the CPAN RT system. | ||||||||||||||||||||||||||||
| 250 | L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Froody> | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | Copyright Fotango 2005. All rights reserved. | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | Please see the main L<Froody> documentation for details of who has worked | ||||||||||||||||||||||||||||
| 257 | on this project. | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | This module is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 260 | the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | L<Froody::Error::Standard>, L<Froody::Response::Error> | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =cut | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 60 | 1; | |||||||||||||||||||||||||||