New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
Exception.pm in vendors/lib/FCM/Class – NEMO

source: vendors/lib/FCM/Class/Exception.pm @ 10669

Last change on this file since 10669 was 10669, checked in by nicolasmartin, 5 years ago

Import latest FCM release from Github into the repository for testing

File size: 3.2 KB
Line 
1#-------------------------------------------------------------------------------
2# (C) British Crown Copyright 2006-17 Met Office.
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#-------------------------------------------------------------------------------
19use strict;
20use warnings;
21
22#-------------------------------------------------------------------------------
23package FCM::Class::Exception;
24
25use constant {
26    CODE_TYPE => 'CODE_TYPE',
27};
28
29sub caught {
30    my ($class, $e) = @_;
31    blessed($e) && $e->isa($class);
32}
33
34sub 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
48for my $key (qw{caller code key package type value}) {
49    no strict qw{refs};
50    *{"get_$key"} = sub {$_[0]->{$key}};
51}
52
53#-------------------------------------------------------------------------------
541;
55__END__
56
57=head1 NAME
58
59FCM::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
84This exception is thrown on incorrect usage of an instance of a sub-class. An
85instance of this exception has the following attributes, which can be accessed
86via $e->get_$attrib():
87
88=head1 ATTRIBUTES
89
90=over 4
91
92=item caller
93
94Returns an ARRAY reference containing the caller (as returned by the built-in
95function in ARRAY context) that triggers the exception. Note: for a CODE-based
96class, this is always the caller when the instance is created.
97
98=item code
99
100The error code, which can be one of the following:
101
102=over 4
103
104=item $e->CODE_TYPE
105
106Attempt to set the value of an attribute to an incorrect type.
107
108=back
109
110=item key
111
112The key of the attribute that triggers this exception.
113
114=item type
115
116The expected data type (for an attempt to set the value of an attribute to an
117incorrect type).
118
119=item value
120
121The value of the attribute that triggers this exception.
122
123=back
124
125=head1 SEE ALSO
126
127L<FCM::Class::CODE|FCM::Class::CODE>
128L<FCM::Class::HASH|FCM::Class::HASH>
129
130=head1 COPYRIGHT
131
132(C) Crown copyright Met Office. All rights reserved.
133
134=cut
Note: See TracBrowser for help on using the repository browser.