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 – NEMO

source: vendors/lib/FCM/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: 2.6 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::Exception;
24
25use Data::Dumper qw{Dumper};
26use Scalar::Util qw{blessed};
27use overload (q{""} => \&Dumper);
28
29use constant {
30    DEFAULT => 'DEFAULT',
31};
32
33# Returns true if $e is a blessed instance of $class.
34sub caught {
35    my ($class, $e) = @_;
36    return (blessed($e) && $e->isa($class));
37}
38
39# Throws the exception.
40sub throw {
41    my ($class, $code, $ctx, $e) = @_;
42    if (defined($e) && !ref($e) && $e =~ qr{\A\s*\z}msx) {
43        $e = undef;
44    }
45    die(bless({code => $code, ctx => $ctx, exception => $e}, $class));
46}
47
48# Attribute accessors.
49for my $name (qw{code ctx exception}) {
50    no strict qw{refs};
51    *{"get_$name"} = sub {$_[0]->{$name}};
52}
53
54# ------------------------------------------------------------------------------
551;
56__END__
57
58=head1 NAME
59
60FCM::Exception
61
62=head1 SYNOPSIS
63
64    use FCM::Exception;
65    my $E = 'FCM::Exception';
66    eval {
67        # ...
68        if ($some_error_condition) {
69            return $E->throw($code, $ctx);
70        }
71        # ...
72    };
73    if (my $e = $@) {
74        if ($E->caught($e)) {
75            # ...
76        }
77    }
78
79=head1 DESCRIPTION
80
81Exception associated with an FCM operation.
82
83=head1 METHODS
84
85=over 4
86
87=item $class->caught($e)
88
89Returns true if $e is a blessed object of $class.
90
91=item $class->throw($code,$ctx,$e)
92
93Creates an instance and die() with it.
94
95=item $e->get_code()
96
97Returns the code associated with this exception.
98
99=item $e->get_ctx()
100
101Returns the context associated with this exception.
102
103=item $e->get_exception()
104
105Returns the exception that generates this exception.
106
107=back
108
109=head1 COPYRIGHT
110
111(C) Crown copyright Met Office. All rights reserved.
112
113=cut
Note: See TracBrowser for help on using the repository browser.