1 | #------------------------------------------------------------------------------- |
---|
2 | # Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
3 | # |
---|
4 | # This file is part of FCM, tools for managing and building source code. |
---|
5 | # |
---|
6 | # FCM is free software: you can redistribute it and/or modify |
---|
7 | # it under the terms of the GNU General Public License as published by |
---|
8 | # the Free Software Foundation, either version 3 of the License, or |
---|
9 | # (at your option) any later version. |
---|
10 | # |
---|
11 | # FCM is distributed in the hope that it will be useful, |
---|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | # GNU General Public License for more details. |
---|
15 | # |
---|
16 | # You should have received a copy of the GNU General Public License |
---|
17 | # along with FCM. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | #------------------------------------------------------------------------------- |
---|
19 | use strict; |
---|
20 | use warnings; |
---|
21 | |
---|
22 | #------------------------------------------------------------------------------- |
---|
23 | package FCM::Class::Exception; |
---|
24 | |
---|
25 | use constant { |
---|
26 | CODE_TYPE => 'CODE_TYPE', |
---|
27 | }; |
---|
28 | |
---|
29 | sub caught { |
---|
30 | my ($class, $e) = @_; |
---|
31 | blessed($e) && $e->isa($class); |
---|
32 | } |
---|
33 | |
---|
34 | sub throw { |
---|
35 | my ($class, $attrib_ref) = @_; |
---|
36 | my %e = ( |
---|
37 | 'caller' => [], |
---|
38 | 'code' => undef, |
---|
39 | 'key' => undef, |
---|
40 | 'package' => undef, |
---|
41 | 'type' => undef, |
---|
42 | 'value' => undef, |
---|
43 | (defined($attrib_ref) ? %{$attrib_ref} : ()), |
---|
44 | ); |
---|
45 | die(bless(\%e, $class)); |
---|
46 | } |
---|
47 | |
---|
48 | for my $key (qw{caller code key package type value}) { |
---|
49 | no strict qw{refs}; |
---|
50 | *{"get_$key"} = sub {$_[0]->{$key}}; |
---|
51 | } |
---|
52 | |
---|
53 | #------------------------------------------------------------------------------- |
---|
54 | 1; |
---|
55 | __END__ |
---|
56 | |
---|
57 | =head1 NAME |
---|
58 | |
---|
59 | FCM::Class::Exception |
---|
60 | |
---|
61 | =head1 SYNOPSIS |
---|
62 | |
---|
63 | eval { |
---|
64 | FCM::Class::Exception->throw({ |
---|
65 | 'caller' => [caller()], |
---|
66 | 'code' => $code, |
---|
67 | 'key' => $key, |
---|
68 | 'package' => $package, |
---|
69 | 'type' => $type, |
---|
70 | 'value' => $value, |
---|
71 | }); |
---|
72 | }; |
---|
73 | if (my $e = $@) { |
---|
74 | if (FCM::Class::Exception->caught($e)) { |
---|
75 | # ... handle this exception class |
---|
76 | } |
---|
77 | else { |
---|
78 | # ... do something else |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | =head1 DESCRIPTION |
---|
83 | |
---|
84 | This exception is thrown on incorrect usage of an instance of a sub-class. An |
---|
85 | instance of this exception has the following attributes, which can be accessed |
---|
86 | via $e->get_$attrib(): |
---|
87 | |
---|
88 | =head1 ATTRIBUTES |
---|
89 | |
---|
90 | =over 4 |
---|
91 | |
---|
92 | =item caller |
---|
93 | |
---|
94 | Returns an ARRAY reference containing the caller (as returned by the built-in |
---|
95 | function in ARRAY context) that triggers the exception. Note: for a CODE-based |
---|
96 | class, this is always the caller when the instance is created. |
---|
97 | |
---|
98 | =item code |
---|
99 | |
---|
100 | The error code, which can be one of the following: |
---|
101 | |
---|
102 | =over 4 |
---|
103 | |
---|
104 | =item $e->CODE_TYPE |
---|
105 | |
---|
106 | Attempt to set the value of an attribute to an incorrect type. |
---|
107 | |
---|
108 | =back |
---|
109 | |
---|
110 | =item key |
---|
111 | |
---|
112 | The key of the attribute that triggers this exception. |
---|
113 | |
---|
114 | =item type |
---|
115 | |
---|
116 | The expected data type (for an attempt to set the value of an attribute to an |
---|
117 | incorrect type). |
---|
118 | |
---|
119 | =item value |
---|
120 | |
---|
121 | The value of the attribute that triggers this exception. |
---|
122 | |
---|
123 | =back |
---|
124 | |
---|
125 | =head1 SEE ALSO |
---|
126 | |
---|
127 | L<FCM::Class::CODE|FCM::Class::CODE> |
---|
128 | L<FCM::Class::HASH|FCM::Class::HASH> |
---|
129 | |
---|
130 | =head1 COPYRIGHT |
---|
131 | |
---|
132 | Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
133 | |
---|
134 | =cut |
---|