Exception.pm 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. package Fcm::Exception;
  9. use overload (q{""} => \&as_string);
  10. use Scalar::Util qw{blessed};
  11. # ------------------------------------------------------------------------------
  12. # Returns true if $e is a blessed instance of this class.
  13. sub caught {
  14. my ($class, $e) = @_;
  15. return (blessed($e) && $e->isa($class));
  16. }
  17. # ------------------------------------------------------------------------------
  18. # Constructor
  19. sub new {
  20. my ($class, $args_ref) = @_;
  21. return bless(
  22. {message => q{unknown problem}, ($args_ref ? %{$args_ref} : ())},
  23. $class,
  24. );
  25. }
  26. # ------------------------------------------------------------------------------
  27. # Returns a string representation of this exception
  28. sub as_string {
  29. my ($self) = @_;
  30. return sprintf("%s: %s\n", blessed($self), $self->get_message());
  31. }
  32. # ------------------------------------------------------------------------------
  33. # Returns the message of this exception
  34. sub get_message {
  35. my ($self) = @_;
  36. return $self->{message};
  37. }
  38. 1;
  39. __END__
  40. =head1 NAME
  41. Fcm::Exception
  42. =head1 SYNOPSIS
  43. use Fcm::Exception;
  44. eval {
  45. croak(Fcm::Exception->new({message => $message}));
  46. };
  47. if ($@) {
  48. if (Fcm::Exception->caught($@)) {
  49. print({STDERR} $@);
  50. }
  51. }
  52. =head1 DESCRIPTION
  53. This exception is raised when there is a generic problem in FCM.
  54. =head1 METHODS
  55. =over 4
  56. =item $class->caught($e)
  57. Returns true if $e is a blessed instance of this class.
  58. =item $class->new({message=E<gt>$message})
  59. Returns a new instance of this exception. Its first argument must be a
  60. reference to a hash containing the detailed I<message> of the exception.
  61. =item $e->as_string()
  62. Returns a string representation of this exception.
  63. =item $e->get_message()
  64. Returns the detailed message of this exception.
  65. =back
  66. =head1 COPYRIGHT
  67. E<169> Crown copyright Met Office. All rights reserved.
  68. =cut