| 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::Exception; |
|---|
| 24 | |
|---|
| 25 | use Data::Dumper qw{Dumper}; |
|---|
| 26 | use Scalar::Util qw{blessed}; |
|---|
| 27 | use overload (q{""} => \&Dumper); |
|---|
| 28 | |
|---|
| 29 | use constant { |
|---|
| 30 | DEFAULT => 'DEFAULT', |
|---|
| 31 | }; |
|---|
| 32 | |
|---|
| 33 | # Returns true if $e is a blessed instance of $class. |
|---|
| 34 | sub caught { |
|---|
| 35 | my ($class, $e) = @_; |
|---|
| 36 | return (blessed($e) && $e->isa($class)); |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | # Throws the exception. |
|---|
| 40 | sub 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. |
|---|
| 49 | for my $name (qw{code ctx exception}) { |
|---|
| 50 | no strict qw{refs}; |
|---|
| 51 | *{"get_$name"} = sub {$_[0]->{$name}}; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | # ------------------------------------------------------------------------------ |
|---|
| 55 | 1; |
|---|
| 56 | __END__ |
|---|
| 57 | |
|---|
| 58 | =head1 NAME |
|---|
| 59 | |
|---|
| 60 | FCM::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 | |
|---|
| 81 | Exception associated with an FCM operation. |
|---|
| 82 | |
|---|
| 83 | =head1 METHODS |
|---|
| 84 | |
|---|
| 85 | =over 4 |
|---|
| 86 | |
|---|
| 87 | =item $class->caught($e) |
|---|
| 88 | |
|---|
| 89 | Returns true if $e is a blessed object of $class. |
|---|
| 90 | |
|---|
| 91 | =item $class->throw($code,$ctx,$e) |
|---|
| 92 | |
|---|
| 93 | Creates an instance and die() with it. |
|---|
| 94 | |
|---|
| 95 | =item $e->get_code() |
|---|
| 96 | |
|---|
| 97 | Returns the code associated with this exception. |
|---|
| 98 | |
|---|
| 99 | =item $e->get_ctx() |
|---|
| 100 | |
|---|
| 101 | Returns the context associated with this exception. |
|---|
| 102 | |
|---|
| 103 | =item $e->get_exception() |
|---|
| 104 | |
|---|
| 105 | Returns the exception that generates this exception. |
|---|
| 106 | |
|---|
| 107 | =back |
|---|
| 108 | |
|---|
| 109 | =head1 COPYRIGHT |
|---|
| 110 | |
|---|
| 111 | Copyright (C) 2006-2021 British Crown (Met Office) & Contributors. |
|---|
| 112 | |
|---|
| 113 | =cut |
|---|