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 | package FCM1::Exception; |
---|
23 | use overload (q{""} => \&as_string); |
---|
24 | |
---|
25 | use Scalar::Util qw{blessed}; |
---|
26 | |
---|
27 | # ------------------------------------------------------------------------------ |
---|
28 | # Returns true if $e is a blessed instance of this class. |
---|
29 | sub caught { |
---|
30 | my ($class, $e) = @_; |
---|
31 | return (blessed($e) && $e->isa($class)); |
---|
32 | } |
---|
33 | |
---|
34 | # ------------------------------------------------------------------------------ |
---|
35 | # Constructor |
---|
36 | sub 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 |
---|
46 | sub 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 |
---|
53 | sub get_message { |
---|
54 | my ($self) = @_; |
---|
55 | return $self->{message}; |
---|
56 | } |
---|
57 | |
---|
58 | 1; |
---|
59 | __END__ |
---|
60 | |
---|
61 | =head1 NAME |
---|
62 | |
---|
63 | FCM1::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 | |
---|
79 | This 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 | |
---|
87 | Returns true if $e is a blessed instance of this class. |
---|
88 | |
---|
89 | =item $class->new({message=E<gt>$message}) |
---|
90 | |
---|
91 | Returns a new instance of this exception. Its first argument must be a |
---|
92 | reference to a hash containing the detailed I<message> of the exception. |
---|
93 | |
---|
94 | =item $e->as_string() |
---|
95 | |
---|
96 | Returns a string representation of this exception. |
---|
97 | |
---|
98 | =item $e->get_message() |
---|
99 | |
---|
100 | Returns the detailed message of this exception. |
---|
101 | |
---|
102 | =back |
---|
103 | |
---|
104 | =head1 COPYRIGHT |
---|
105 | |
---|
106 | Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
---|
107 | |
---|
108 | =cut |
---|