source: LMDZ6/branches/Amaury_dev/tools/fcm/lib/FCM1/Exception.pm @ 5099

Last change on this file since 5099 was 5095, checked in by abarral, 4 months ago

Revert cosp*/ from the trunk, as it's external code
Add missing bits from FCM2 source

File size: 2.8 KB
Line 
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# ------------------------------------------------------------------------------
19use strict;
20use warnings;
21
22package FCM1::Exception;
23use overload (q{""} => \&as_string);
24
25use Scalar::Util qw{blessed};
26
27# ------------------------------------------------------------------------------
28# Returns true if $e is a blessed instance of this class.
29sub caught {
30    my ($class, $e) = @_;
31    return (blessed($e) && $e->isa($class));
32}
33
34# ------------------------------------------------------------------------------
35# Constructor
36sub new {
37    my ($class, $args_ref) = @_;
38    return bless(
39        {message => q{unknown problem}, ($args_ref ? %{$args_ref} : ())},
40        $class,
41    );
42}
43
44# ------------------------------------------------------------------------------
45# Returns a string representation of this exception
46sub as_string {
47    my ($self) = @_;
48    return sprintf("%s: %s\n", blessed($self), $self->get_message());
49}
50
51# ------------------------------------------------------------------------------
52# Returns the message of this exception
53sub get_message {
54    my ($self) = @_;
55    return $self->{message};
56}
57
581;
59__END__
60
61=head1 NAME
62
63FCM1::Exception
64
65=head1 SYNOPSIS
66
67    use FCM1::Exception;
68    eval {
69        croak(FCM1::Exception->new({message => $message}));
70    };
71    if ($@) {
72        if (FCM1::Exception->caught($@)) {
73            print({STDERR} $@);
74        }
75    }
76
77=head1 DESCRIPTION
78
79This exception is raised when there is a generic problem in FCM.
80
81=head1 METHODS
82
83=over 4
84
85=item $class->caught($e)
86
87Returns true if $e is a blessed instance of this class.
88
89=item $class->new({message=E<gt>$message})
90
91Returns a new instance of this exception. Its first argument must be a
92reference to a hash containing the detailed I<message> of the exception.
93
94=item $e->as_string()
95
96Returns a string representation of this exception.
97
98=item $e->get_message()
99
100Returns the detailed message of this exception.
101
102=back
103
104=head1 COPYRIGHT
105
106Copyright (C) 2006-2021 British Crown (Met Office) & Contributors.
107
108=cut
Note: See TracBrowser for help on using the repository browser.