home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl5 / Net / DBus / Error.pm < prev    next >
Encoding:
Perl POD Document  |  2008-02-20  |  3.7 KB  |  171 lines

  1. # -*- perl -*-
  2. #
  3. # Copyright (C) 2004-2006 Daniel P. Berrange
  4. #
  5. # This program is free software; You can redistribute it and/or modify
  6. # it under the same terms as Perl itself. Either:
  7. #
  8. # a) the GNU General Public License as published by the Free
  9. #   Software Foundation; either version 2, or (at your option) any
  10. #   later version,
  11. #
  12. # or
  13. #
  14. # b) the "Artistic License"
  15. #
  16. # The file "COPYING" distributed along with this file provides full
  17. # details of the terms and conditions of the two licenses.
  18.  
  19. =pod
  20.  
  21. =head1 NAME
  22.  
  23. Net::DBus::Error - Error details for remote method invocation
  24.  
  25. =head1 SYNOPSIS
  26.  
  27.   package Music::Player::UnknownFormat;
  28.  
  29.   use base qw(Net::DBus::Error);
  30.  
  31.   # Define an error type for unknown track encoding type
  32.   # for a music player service
  33.   sub new {
  34.       my $proto = shift;
  35.       my $class = ref($proto) || $proto;
  36.       my $self = $class->SUPER::new(name => "org.example.music.UnknownFormat",
  37.                                     message => "Unknown track encoding format");
  38.   }
  39.  
  40.  
  41.   package Music::Player::Engine;
  42.  
  43.   ...snip...
  44.  
  45.   # Play either mp3 or ogg music tracks, otherwise
  46.   # thrown an error
  47.   sub play {
  48.       my $self = shift;
  49.       my $url = shift;
  50.  
  51.       if ($url =~ /\.(mp3|ogg)$/) {
  52.       ...play the track
  53.       } else {
  54.          die Music::Player::UnknownFormat->new();
  55.       }
  56.   }
  57.  
  58.  
  59. =head1 DESCRIPTION
  60.  
  61. This objects provides for strongly typed error handling. Normally
  62. a service would simply call
  63.  
  64.   die "some message text"
  65.  
  66. When returning the error condition to the calling DBus client, the
  67. message is associated with a generic error code or "org.freedesktop.DBus.Failed".
  68. While this suffices for many applications, occasionally it is desirable
  69. to be able to catch and handle specific error conditions. For such
  70. scenarios the service should create subclasses of the C<Net::DBus::Error>
  71. object providing in a custom error name. This error name is then sent back
  72. to the client instead of the genreic "org.freedesktop.DBus.Failed" code.
  73.  
  74. =head1 METHODS
  75.  
  76. =over 4
  77.  
  78. =cut
  79.  
  80. package Net::DBus::Error;
  81.  
  82. use strict;
  83. use warnings;
  84.  
  85.  
  86. use overload ('""' => 'stringify');
  87.  
  88. =item my $error = Net::DBus::Error->new(name => $error_name,
  89.                                         message => $description);
  90.  
  91. Creates a new error object whose name is given by the C<name>
  92. parameter, and long descriptive text is provided by the 
  93. C<message> parameter. The C<name> parameter has certain
  94. formatting rules which must be adhered to. It must only contain
  95. the letters 'a'-'Z', '0'-'9', '-', '_' and '.'. There must be
  96. at least two components separated by a '.', For example a valid
  97. name is 'org.example.Music.UnknownFormat'.
  98.  
  99. =cut
  100.  
  101. sub new {
  102.     my $proto = shift;
  103.     my $class = ref($proto) || $proto;
  104.     my $self = {};
  105.     my %params = @_;
  106.  
  107.     $self->{name} = $params{name} ? $params{name} : die "name parameter is required";
  108.     $self->{message} = $params{message} ? $params{message} : die "message parameter is required";
  109.  
  110.     bless $self, $class;
  111.  
  112.     return $self;
  113. }
  114.  
  115. =item $error->name
  116.  
  117. Returns the DBus error name associated with the object.
  118.  
  119. =cut
  120.  
  121. sub name {
  122.     my $self = shift;
  123.     return $self->{name};
  124. }
  125.  
  126. =item $error->message
  127.  
  128. Returns the descriptive text/message associated with the
  129. error condition.
  130.  
  131. =cut
  132.  
  133. sub message {
  134.     my $self = shift;
  135.     return $self->{message};
  136. }
  137.  
  138. =item $error->stringify
  139.  
  140. Formats the error as a string in a manner suitable for
  141. printing out / logging / displaying to the user, etc.
  142.  
  143. =cut
  144.  
  145. sub stringify {
  146.     my $self = shift;
  147.  
  148.     return $self->{name} . ": " . $self->{message} . ($self->{message} =~ /\n$/ ? "" : "\n");
  149. }
  150.  
  151.  
  152. 1;
  153.  
  154. =pod
  155.  
  156. =back
  157.  
  158. =head1 AUTHORS
  159.  
  160. Daniel P. Berrange
  161.  
  162. =head1 COPYRIGHT
  163.  
  164. Copyright (C) 2005-2006 Daniel P. Berrange
  165.  
  166. =head1 SEE ALSO
  167.  
  168. L<Net::DBus>, L<Net::DBus::Object>
  169.  
  170. =cut
  171.