1package WebService::GData::Error::Entry; 2use WebService::GData 'private'; 3use base 'WebService::GData'; 4 5our $VERSION = 1.02; 6 7WebService::GData::install_in_package([qw(internalreason domain code location)], 8 sub { 9 my $func=shift; 10 return sub { 11 my $this = shift; 12 if(@_==1){ 13 $this->{$func}=$_[0]; 14 } 15 return $this->{$func}; 16 }; 17}); 18 19 sub __init { 20 my ($this,$xmlerror) = @_; 21 $this->_parse($xmlerror); 22 } 23 24 sub serialize { 25 my $this = shift; 26 my $xml='<error>'; 27 $xml.='<internalreason>'.$this->internalreason.'</internalreason>' if($this->internalreason); 28 $xml.='<code>'.$this->code.'</code>' if($this->code); 29 $xml.='<domain>'.$this->domain.'</domain>' if($this->domain); 30 $xml.="<location type='".$this->location->{type}."'>".$this->location->{content}.'</location>' if($this->location); 31 $xml.='</error>'; 32 return $xml; 33 } 34 35 private _parse => sub { 36 my ($this,$error) = @_; 37 if($error){ 38 my ($domain) = $error=~m/<domain>(.+?)<\/domain>/; 39 my ($code) = $error=~m/<code>(.+?)<\/code>/; 40 my $location = {}; 41 ($location->{type}) = $error=~m/<location\s+type='(.+?)'>/gmxi; 42 43 ($location->{content}) = $error=~m/'>(.+?)<\/location>/gmxi; 44 my ($internalreason) = $error=~m/<internalreason>(.+?)<\/internalreason>/gmxi; 45 $this -> code($code); 46 $this -> internalreason($internalreason); 47 $this -> domain($domain); 48 $this -> location($location); 49 } 50 }; 51 52"The earth is blue like an orange."; 53 54__END__ 55 56=pod 57 58=head1 NAME 59 60WebService::GData::Error::Entry - Wrap an xml error sent back by Google data APIs v2. 61 62 63=head1 SYNOPSIS 64 65 use WebService::GData::Error; 66 67 #parse an error from a Google data API server... 68 my $entry = new WebService::GData::Error::Entry($xmlerror); 69 $entry->code; 70 $entry->internalreason; 71 $entry->domain; 72 $entry->location->{type};#this is just a hash 73 $entry->location->{content};#this is just a hash 74 75 #create an error from a Google data API server... 76 my $entry = new WebService::GData::Error::Entry(); 77 $entry->code('too_long'); 78 $entry->domain('your_domain'); 79 $entry->location({type=>'header',content=>'Missing Version header'}); 80 print $entry->serialize()#return <error>...</error> 81 82 83 84 85=head1 DESCRIPTION 86 87I<inherits from L<WebService::GData>> 88 89This package can parse error response from Google APIs service. You can also create your own basic xml error. 90All WebService::GData::* classes die a WebService::GData::Error object when something went wrong. 91 92XML error Example: 93 94 <error> 95 <domain>yt:validation</domain> 96 <code>invalid_character</code> 97 <location type='xpath'>media:group/media:title/text()</location> 98 </error> 99 100Example: 101 102 use WebService::GData::Error; 103 104 #parse an error from a Google data API server... 105 my $entry = new WebService::GData::Error::Entry($xmlerror); 106 $entry->code; 107 $entry->internalreason; 108 $entry->domain; 109 $entry->location->{type};#this is just a hash 110 $entry->location->{content};#this is just a hash 111 112 #create an error from a Google data API server... 113 my $entry = new WebService::GData::Error::Entry(); 114 $entry->code('too_long'); 115 $entry->domain('your_domain'); 116 $entry->location({type=>'header',content=>'Missing Version header'}); 117 118 119 120=head2 Constructor 121 122=head3 new 123 124=over 125 126Create a L<WebService::GData::Error::Entry> instance. 127If the content is an xml following the Google data API format, it will get parse. 128 129 130B<Parameters> 131 132=over 4 133 134=item C<content:Scalar>(optional) 135 136=back 137 138B<Returns> 139 140=over 4 141 142=item L<WebService::GData::Error::Entry> 143 144=back 145 146=back 147 148 149=head2 GET/SET METHODS 150 151These methods return their content if no parameters is passed or set their content if a parameter is set. 152 153=head3 code 154 155=over 156 157Get/set an error code. 158 159B<Parameters> 160 161=over 4 162 163=item C<none> - Work as a getter 164 165=item C<content:Scalar> - Work as a setter 166 167=back 168 169B<Returns> 170 171=over 4 172 173=item C<content:Scalar> (as a getter) 174 175=back 176 177=back 178 179 180=head3 location 181 182=over 183 184Get/set the error location as an xpath.It requires a ref hash with type and content as keys. 185 186B<Parameters> 187 188=over 4 189 190=item C<none> - Work as a getter 191 192=item C<content:HashRef> - Work as a setter. the hash must be in contain the following : {type=>'...',content=>'...'}. 193 194=back 195 196B<Returns> 197 198=over 4 199 200=item C<content:HashRef> (as a getter) 201 202=back 203 204Example: 205 206 $error->location({type=>'invalid_character',content=>'The string contains an unsupported character.'}); 207 208=back 209 210=head3 domain 211 212=over 213 214Get/set the type of error. Google data API has validation,quota,authentication,service errors. 215 216B<Parameters> 217 218=over 4 219 220=item C<none> - Work as a getter 221 222=item C<content:Scalar> - Work as a setter. 223 224=back 225 226B<Returns> 227 228=over 4 229 230=item C<content:Scalar> (as a getter) 231 232=back 233 234=back 235 236 237=head3 serialize 238 239=over 240 241Send back an xml representation of an error. 242 243B<Parameters> 244 245=over 4 246 247=item C<none> 248 249=back 250 251B<Returns> 252 253=over 4 254 255=item C<content:Scalar> - xml representation of the error. 256 257=back 258 259=back 260 261=head2 SEE ALSO 262 263XML format overview and explanation of the different kind of errors you can encounter: 264 265L<http://code.google.com/intl/en/apis/youtube/2.0/developers_guide_protocol_error_responses.html> 266 267 268=head1 BUGS AND LIMITATIONS 269 270If you do me the favor to _use_ this module and find a bug, please email me 271i will try to do my best to fix it (patches welcome)! 272 273=head1 AUTHOR 274 275shiriru E<lt>shirirulestheworld[arobas]gmail.comE<gt> 276 277=head1 LICENSE AND COPYRIGHT 278 279This library is free software; you can redistribute it and/or modify 280it under the same terms as Perl itself. 281 282=cut