1 | # ------------------------------------------------------------------------------ |
---|
2 | # (C) Crown copyright Met Office. All rights reserved. |
---|
3 | # For further details please refer to the file COPYRIGHT.txt |
---|
4 | # which you should have received as part of this distribution. |
---|
5 | # ------------------------------------------------------------------------------ |
---|
6 | use strict; |
---|
7 | use warnings; |
---|
8 | |
---|
9 | package Fcm::Exception; |
---|
10 | use overload (q{""} => \&as_string); |
---|
11 | |
---|
12 | use Scalar::Util qw{blessed}; |
---|
13 | |
---|
14 | # ------------------------------------------------------------------------------ |
---|
15 | # Returns true if $e is a blessed instance of this class. |
---|
16 | sub caught { |
---|
17 | my ($class, $e) = @_; |
---|
18 | return (blessed($e) && $e->isa($class)); |
---|
19 | } |
---|
20 | |
---|
21 | # ------------------------------------------------------------------------------ |
---|
22 | # Constructor |
---|
23 | sub new { |
---|
24 | my ($class, $args_ref) = @_; |
---|
25 | return bless( |
---|
26 | {message => q{unknown problem}, ($args_ref ? %{$args_ref} : ())}, |
---|
27 | $class, |
---|
28 | ); |
---|
29 | } |
---|
30 | |
---|
31 | # ------------------------------------------------------------------------------ |
---|
32 | # Returns a string representation of this exception |
---|
33 | sub as_string { |
---|
34 | my ($self) = @_; |
---|
35 | return sprintf("%s: %s\n", blessed($self), $self->get_message()); |
---|
36 | } |
---|
37 | |
---|
38 | # ------------------------------------------------------------------------------ |
---|
39 | # Returns the message of this exception |
---|
40 | sub get_message { |
---|
41 | my ($self) = @_; |
---|
42 | return $self->{message}; |
---|
43 | } |
---|
44 | |
---|
45 | 1; |
---|
46 | __END__ |
---|
47 | |
---|
48 | =head1 NAME |
---|
49 | |
---|
50 | Fcm::Exception |
---|
51 | |
---|
52 | =head1 SYNOPSIS |
---|
53 | |
---|
54 | use Fcm::Exception; |
---|
55 | eval { |
---|
56 | croak(Fcm::Exception->new({message => $message})); |
---|
57 | }; |
---|
58 | if ($@) { |
---|
59 | if (Fcm::Exception->caught($@)) { |
---|
60 | print({STDERR} $@); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | =head1 DESCRIPTION |
---|
65 | |
---|
66 | This exception is raised when there is a generic problem in FCM. |
---|
67 | |
---|
68 | =head1 METHODS |
---|
69 | |
---|
70 | =over 4 |
---|
71 | |
---|
72 | =item $class->caught($e) |
---|
73 | |
---|
74 | Returns true if $e is a blessed instance of this class. |
---|
75 | |
---|
76 | =item $class->new({message=E<gt>$message}) |
---|
77 | |
---|
78 | Returns a new instance of this exception. Its first argument must be a |
---|
79 | reference to a hash containing the detailed I<message> of the exception. |
---|
80 | |
---|
81 | =item $e->as_string() |
---|
82 | |
---|
83 | Returns a string representation of this exception. |
---|
84 | |
---|
85 | =item $e->get_message() |
---|
86 | |
---|
87 | Returns the detailed message of this exception. |
---|
88 | |
---|
89 | =back |
---|
90 | |
---|
91 | =head1 COPYRIGHT |
---|
92 | |
---|
93 | E<169> Crown copyright Met Office. All rights reserved. |
---|
94 | |
---|
95 | =cut |
---|